CSS文件中的相对路径是否相对于CSS文件?


Answers:


86

是的,这是相对于 .css

这是一个示例布局:

Page:  page.htm ... does not matter where
CSS:   /resources/css/styles.css  
Image: /resources/images/image.jpg

CSS中styles.css

div { background-image: url('../images/image.jpg');

感谢您的详细回答。在同一问题上,我真的很困惑,在这里您只是以更好的方式解决了它。
AbdulAziz

3
这确实是合理的,因为它允许从不同的文件(可能位于不同的位置)引用相同的 .css文件,.html而无需制动到外部对象的路径。
Davide

为什么在styles.css中div的括号没有关闭?
苏莱曼,

8

是。它相对于CSS文件。我还要补充一点,这也包括相对于CSS文件所在的域。

因此,如果CSS被引用为:

<link href="http://www.otherdomain.com/css/example.css" type="text/css" rel="stylesheet" />

它包含:

div { background-image: url('/images/image.jpg');

背景将是:

http://www.otherdomain.com/images/image.jpg


2
由于相同的来源政策,如果您将标头设置为允许外部包含,则来自外部域的包含仅在某些浏览器中有效
Jakob Alexander Eichler


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.