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 /stylesheets/base-styles.css
contains:
div#header { background-image: url('images/header-background.jpg'); }
If I include this style-sheet into different documents via <link ... />
will the relative URL in the CSS file be relative to the stylesheet document in /stylesheets/
or relative to the current document it's included to? Possible paths like:
/item/details.html /about/index.html /about/extra/other.html /index.html
Answer. A relative path is an address that points to the current directory or project folder. In the example illustrated in this lesson, the relative path is images/mountains. jpg and it assumes we have an images subfolder nested within a project folder.
The CSS url() function allows you to link to a resource, such as an image, web font, a filter, etc. The url() function accepts a url value as its parameter. This specifies the location of the file.
A style sheet can reference URLs or links to other files, almost always it is images that are linked as background or fonts.
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.
It's relative to the CSS file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With