简体   繁体   English

CSS 外部样式表不起作用,但完全相同的 CSS 在内部样式表上工作

[英]CSS External Style Sheet isn't working, but the exact same CSS was woking on an internal Style Sheet

While making a personal website I've encountered a problem with adding a CSS Style Sheet.在制作个人网站时,我遇到了添加 CSS 样式表的问题。 That Style Sheet was a local one (in the same folder has the .htm file) called "Rodrigo.css".该样式表是一个名为“Rodrigo.css”的本地样式表(在同一文件夹中有 .htm 文件)。

Here is the HTML Link tag with the CSS in the hrc:这是在 hrc 中带有 CSS 的 HTML Link 标记:

Here is a screenshot of the CSS Style Sheet (only the beginning part):这是 CSS 样式表的屏幕截图(仅开头部分):

As you can see there are not HTML Tags.如您所见,没有 HTML 标签。

Note: The CSS that I'm using in the style sheet was previously in an internal style sheet and worked, so I don't know why it isn't working.注意:我在样式表中使用的 CSS 以前在内部样式表中并且有效,所以我不知道为什么它不起作用。

While making the website I used the w3schools tutorials.在制作网站时,我使用了 w3schools 教程。 To do this part specifically the "CSS How to... Three ways to incert CSS".专门做这部分的“CSS How to...三种方法来incert CSS”。


Solved解决了

I've found what the problem was, the HTML file was encoded in a format that wasn't supported by some browsers, and was a different format to the one of the external style sheet.我发现了问题所在,HTML 文件的编码格式不受某些浏览器的支持,并且与外部样式表的格式不同。

I downloaded both your HTML file and CSS file, placed them in the same folder on my desktop and the CSS file took proper effect on the HTML file.我下载了你的 HTML 文件和 CSS 文件,将它们放在我桌面上的同一个文件夹中,CSS 文件对 HTML 文件产生了正确的影响。 I confirmed this by removing the CSS file from the folder, observing the difference in appearance and verifying the appearance was back to normal when I copied the CSS back in.我通过从文件夹中删除 CSS 文件来确认这一点,观察外观的差异,并在我重新复制 CSS 时验证外观恢复正常。

Your code appears to be correct.您的代码似乎是正确的。 Try clearing your cache, trying a different browser or open the html file in incognito mode.尝试清除缓存、尝试其他浏览器或以隐身模式打开 html 文件。

Edit: Another thing you can try is replacing编辑:您可以尝试的另一件事是更换

<link rel="stylesheet" type="text/css" href="Rodrigo.css">

with

<link rel="stylesheet" type="text/css" href="./Rodrigo.css">

Notice the addition of ./ before your file name.请注意在文件名前添加了./ This forces the browser to look in the same directory as the CSS file.这会强制浏览器在与 CSS 文件相同的目录中查找。 I could be making this up but I think I remember having problems with either links, images or hyperlinks in older browsers when I omitted the ./我可以编造这个,但我想我记得当我省略./

So, I've found the problem.所以,我找到了问题所在。 It turned out I encoded it in UTF-8 Bom( I'm not sure that's the name of the enconding type) and that was causing problems with the browsers and the external style sheet, witch wasn't enconded in the same format.原来我用 UTF-8 Bom 编码它(我不确定那是编码类型的名称),这导致浏览器和外部样式表出现问题,女巫没有以相同的格式编码。 I'm going to mark this awnser had having solved my problem.我要标记这个 awnser 已经解决了我的问题。

The solution is enconding the HTML source file in UTP-8, or other files that are supported and enconding the HTML and CSS source files in the same format.解决方案是将 HTML 源文件编码为 UTP-8 或其他支持的文件,并将 HTML 和 CSS 源文件编码为相同格式。

Thank you for anwsering, tough.谢谢你的回答,辛苦了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM