On this page
A relative URL in CSS url() is resolved against the stylesheet’s URL, not necessarily the HTML page’s URL. This distinction often explains why a background image or font works in one stylesheet but returns a 404 in another.
This guide shows how to calculate the requested URL, diagnose failures in browser developer tools, and check whether a bundler has changed the paths in your deployed CSS.
Key Takeaways
- In an external stylesheet, a relative
url()starts from the stylesheet’s location. - In a
<style>block orstyleattribute, it resolves against the document’s base URL, which can be affected by a<base>element. - A leading
/starts at the current origin’s root; it does not mean “relative to the CSS file.” - Bundlers may rewrite or relocate assets. Diagnose the URLs in the built CSS and the browser’s Network panel, not just the source files.
Resolve a relative URL in CSS url()
Suppose a browser loads a stylesheet from:
https://example.com/assets/css/main.css
A reference in that stylesheet resolves as follows:
| CSS reference | Resolved URL |
|---|---|
url("../images/banner.svg") | https://example.com/assets/images/banner.svg |
url("./icons/check.svg") | https://example.com/assets/css/icons/check.svg |
url("/images/banner.svg") | https://example.com/images/banner.svg |
For ../images/banner.svg, start in the stylesheet’s directory, /assets/css/. The .. moves up to /assets/, then images/banner.svg selects the asset. The location of the HTML file that linked to main.css does not change that calculation.
Imported stylesheets have their own locations. For example, an asset reference inside /assets/css/components/card.css is resolved relative to that imported stylesheet, not the stylesheet that imported it.
Advertisement
Example: background image and font in nested folders
The following example uses a local HTTP server. Create this structure and supply your own SVG and WOFF2 files with the indicated names:
site/
├── index.html
└── assets/
├── css/
│ └── main.css
├── fonts/
│ └── example-regular.woff2
└── images/
└── banner.svg
In site/index.html, link to the stylesheet:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS asset example</title>
<link rel="stylesheet" href="assets/css/main.css">
</head>
<body>
<header class="banner">Welcome</header>
</body>
</html>
In site/assets/css/main.css, use paths relative to main.css:
@font-face {
font-family: "Example";
src: url("../fonts/example-regular.woff2") format("woff2");
font-weight: 400;
font-style: normal;
}
.banner {
min-height: 12rem;
font-family: "Example", sans-serif;
background-image: url("../images/banner.svg");
background-position: center;
background-size: cover;
}
From the site directory, start a server with Python 3:
python3 -m http.server 8000
Open http://localhost:8000/. The expected asset requests are /assets/images/banner.svg and /assets/fonts/example-regular.woff2. Stop the server with Ctrl+C. The example has not been executed in this environment, so check those requests in your browser.
If you instead wrote url("assets/images/banner.svg") in main.css, the browser would request /assets/css/assets/images/banner.svg: the path starts at the CSS directory, not at the site root.
Inline CSS and document base URLs
A relative URL in a <style> block or style attribute uses the document’s base URL. For a page at https://example.com/products/item.html with no <base> element, this declaration resolves to https://example.com/products/images/banner.svg:
<style>
.banner { background-image: url("images/banner.svg"); }
</style>
If the document specifies a different base URL with <base href="...">, that can change the result. Moving the same declaration into an external stylesheet can also change the result because the stylesheet becomes the base instead.
Advertisement
Diagnose missing assets in the browser
- Open developer tools and reload the page with the Network panel visible.
- Find the stylesheet request first. Confirm that it loaded and note its actual URL. If it failed, fix the
<link>or deployment path before troubleshooting its assets. - Find the image or font request. Inspect its full request URL and status. Compare the requested path with the location where the server actually serves the file.
- Inspect the relevant declaration in the Styles panel. Confirm that the rule applies and has not been overridden. A background may be loaded but hidden by another declaration; a font may fall back if the face or weight does not match.
- Check the console and response details for errors. A missing file often produces a 404, but access restrictions, cross-origin font policies, an incorrect content type, or a server returning an HTML error page can also prevent an asset from working.
Test the page over HTTP rather than relying on file:// behavior. If a URL works locally but fails after deployment, compare the exact capitalization and deployed path: some servers use case-sensitive file systems, and a site hosted beneath a path prefix may not serve root-relative URLs where you expect.
Check bundler output
Build tools may process CSS url() references, copy assets, generate new filenames, and rewrite the references. Exact behavior depends on the tool and its configuration; some references may instead be left unchanged.
When the source path looks right but the deployed asset fails:
- Run your project’s normal production build.
- Open the generated CSS and inspect the emitted
url()value. Check whether the referenced asset exists in the output directory. - Serve the build using the same path prefix and asset-hosting configuration as production, where practical.
- Compare the browser’s requested URL with the URL at which the deployed server actually serves the asset. Check the bundler’s public/base path and asset-processing configuration if they differ.
Do not assume a source-relative path will appear unchanged in the built CSS, or that an absolute path beginning with / will include your application’s deployment prefix.
Common Mistakes
- Calculating from the HTML file for external CSS. Use the external stylesheet’s URL instead.
- Forgetting a nested stylesheet directory. Each
..moves up one directory from that stylesheet’s location. - Confusing root-relative and relative paths.
/fonts/example.woff2starts at the origin root;../fonts/example.woff2starts at the CSS directory. - Checking only source files after a build. The browser uses the emitted stylesheet and its rewritten URLs.
- Treating every missing asset as a path problem. Check response status, content, cross-origin restrictions, and whether the CSS declaration actually applies.
Best Practices
- Keep related assets in a predictable directory structure and calculate paths from the stylesheet that contains each reference.
- Use browser developer tools to inspect actual requests before changing paths by trial and error.
- Check production builds under the real deployment prefix, especially when moving a site into a subdirectory or serving assets from another origin.
- Keep fallback fonts in
font-familyso text remains readable if a font cannot load.
Conclusion
For an external stylesheet, resolve each relative url() from that stylesheet’s URL. When an asset is missing, compare the browser’s actual request with the deployed file location and, if you use a bundler, inspect the generated CSS as well.
Research note: CSS specifications and browser documentation could not be consulted in this environment. The resolution guidance above is based on established browser behavior and should be checked against those sources when editorial verification is available.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.