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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2718532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 21:58:13  来源:igfitidea点击:

Is a relative path in a CSS file relative to the CSS file?

csspath

提问by Nathan Osman

When I reference an image or other file in a CSS file by a relative path, is the path relative to the CSS file or the HTML file using the CSS file?

当我通过相对路径引用 CSS 文件中的图像或其他文件时,路径是相对于 CSS 文件还是使用 CSS 文件的 HTML 文件?

回答by Nick Craver

Yes, it's relative to the .css

是的,它相对于 .css

Here's an example layout:

这是一个示例布局:

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

CSS in styles.css:

CSS 中styles.css

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

回答by jshoaf

Yes. It is relative to the CSS file. I'll add that this also includes relative to the domain the CSS file is on.

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

So if the CSS is referenced as:

因此,如果 CSS 被引用为:

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

and it contains:

它包含:

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

The background will be:

背景将是:

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

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

回答by emzero

To the CSS file.

到 CSS 文件。