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

前端 未结 8 2284
名媛妹妹
名媛妹妹 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:10

    One issue that can occur, and seemingly break this is when using auto minimization of css. The request path for the minified bundle can have a different path than the original css. This may happen automatically so it can cause confusion.

    The mapped request path for the minified bundle should be "/originalcssfolder/minifiedbundlename" not just "minifiedbundlename".

    In other words, name your bundles to have same path (with the /) as the original folder structure, this way any external resources like fonts, images will map to correct URIs by the browser. The alternative is to use absolute url( refs in your css but that isn't usually desirable.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题