Using relative URL in CSS file, what location is it relative to?

前端 未结 8 2277
名媛妹妹
名媛妹妹 2020-11-22 03:29

When defining something like a background image URL in a CSS file, when using a relative URL, where is it relative to? For example:

Suppose the file /styleshe

8条回答
  •  孤街浪徒
    2020-11-22 04:11

    According to W3:

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

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

    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.

提交回复
热议问题