且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 CSS 文件中使用相对 URL,它相对于哪个位置?

更新时间:2022-12-25 21:20:03

根据 W3:

部分 URL 是相对于样式表的来源解释的,而不是相对于文档

Partial URLs are interpreted relative to the source of the style sheet, not relative to the document

因此,在回答您的问题时,它将与 /stylesheets/ 相关.

Therefore, in answer to your question, it will be relative to /stylesheets/.

如果您考虑一下,这是有道理的,因为可以将 CSS 文件添加到不同目录中的页面,因此将其标准化为 CSS 文件意味着 URL 将在链接样式表的任何地方工作.

If you think about this, this makes sense, since the CSS file could be added to pages in different directories, so standardising it to the CSS file means that the URLs will work wherever the stylesheets are linked.