On this page
An unexpected <base href> can make a page request assets or open links from a different path or origin than you expect. When troubleshooting HTML base href relative links, inspect the document’s base URL before changing individual paths.
This article shows how to reproduce the behavior, check what the browser resolved, and fix common configuration errors. The HTML Living Standard is the governing specification for the element, but its current text could not be consulted for this article because source retrieval was unavailable.
Key Takeaways
- The first
<base>element with anhrefsets the document base URL used to resolve relative URLs in the document. - Links, image sources, script sources, and stylesheet links can therefore resolve somewhere other than the page’s own directory.
- A fragment-only link such as
#helpis resolved against the base URL too; it can navigate to another document. - A trailing slash changes whether the last part of a base URL is treated as a directory or a file-like segment.
- Inspect
document.baseURIand resolved element properties in DevTools. Do not rely on the literal attribute alone.
Reproduce the Problem
Suppose this document is served at https://example.com/docs/guide/index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<base href="https://cdn.example.net/releases/v2/">
<link rel="stylesheet" href="css/site.css">
<script src="js/app.js" defer></script>
<title>Base URL example</title>
</head>
<body>
<a href="next.html">Next page</a>
<img src="images/logo.png" alt="Example logo">
<a href="#help">Help</a>
<section id="help">Help content</section>
</body>
</html>
The browser resolves the relative attributes against the base, not against the document’s address:
| Attribute value | Resolved URL |
|---|---|
next.html | https://cdn.example.net/releases/v2/next.html |
images/logo.png | https://cdn.example.net/releases/v2/images/logo.png |
js/app.js | https://cdn.example.net/releases/v2/js/app.js |
css/site.css | https://cdn.example.net/releases/v2/css/site.css |
#help | https://cdn.example.net/releases/v2/#help |
The example illustrates URL resolution; the example domains are not intended to serve working assets. Successful loading also depends on the destination, server responses, and browser security policies.
Advertisement
Placement and Resolution Rules
Put <base> in the document’s <head>, before elements with URL-bearing attributes, especially stylesheet links and scripts. This lets the browser encounter the intended base before processing those resources. A document should have at most one <base> element. If multiple elements are present, the first applicable href is used; target is determined separately from the first <base> with a target attribute.
An absolute URL in an attribute does not need the document base. Other forms can still be affected:
| Attribute value | Resolution with base https://cdn.example.net/releases/v2/ |
|---|---|
/images/logo.png | https://cdn.example.net/images/logo.png |
?mode=print | https://cdn.example.net/releases/v2/?mode=print |
//static.example.org/app.js | https://static.example.org/app.js |
https://example.com/home | https://example.com/home |
A root-relative path starts at the base URL’s origin, not necessarily the page’s origin. A scheme-relative URL uses the base URL’s scheme. Without an applicable base href, the browser normally uses the document URL as the fallback base; special document contexts can have different fallback behavior.
The trailing slash matters
A base ending in / acts like a directory for relative resolution. Without it, the final path segment is replaced:
new URL("app.js", "https://example.com/releases/v2/").href;
// "https://example.com/releases/v2/app.js"
new URL("app.js", "https://example.com/releases/v2").href;
// "https://example.com/releases/app.js"
Likewise, ../app.js moves up from the base directory. Use the browser console to check the exact result rather than guessing from the path’s appearance.
Why #fragment Links Can Leave the Page
In the example above, href="#help" resolves to https://cdn.example.net/releases/v2/#help, not to the #help section on https://example.com/docs/guide/index.html. Clicking it can navigate to the base URL’s document and then look for its help fragment. If the resolved URL instead refers to the current document, the browser can navigate to the fragment within that document.
If the base must point elsewhere, use an explicit URL for a link that must return to the original document, for example https://example.com/docs/guide/index.html#help. Choose a canonical URL appropriate to your deployment; a hard-coded address may not work across staging and production.
Advertisement
Diagnose and Fix an Unexpected Path
-
Open browser DevTools on the affected page and inspect the
<head>for<base>elements. Also check the rendered DOM: a framework or injected markup may have changed it from the original response. -
In the console, inspect the effective base and compare an attribute with its resolved property:
document.baseURI; const link = document.querySelector('a[href="next.html"]'); if (link) { console.log("Attribute:", link.getAttribute("href")); console.log("Resolved:", link.href); } new URL("images/logo.png", document.baseURI).href;getAttribute()shows the value in the markup;link.hrefshows the browser’s resolved URL. Replace the selector with one that matches the element you are investigating. -
Check the Network panel for the actual request URL, status, redirects, and any console errors. Reload after changing the markup: editing a base in DevTools does not undo requests already made or rerun scripts that already executed.
-
Remove the base if the page does not need it. Otherwise, set it to the intended directory, include the trailing slash when appropriate, and put it before URL-bearing elements. Use absolute URLs selectively for resources that must use a different origin or path.
-
Check navigation and assets again, including fragment links and routes on nested pages. Changing the base can affect many elements at once.
The document base resolves the stylesheet’s <link href>, but relative url(...) references inside an external stylesheet are generally resolved relative to that stylesheet’s URL. Inspect the stylesheet request and its subsequent asset requests separately.
Security and Usability Risks
An unexpected base can direct links to an unintended site, break navigation, send users away from the page when they click an in-page anchor, or cause the browser to request assets from an unintended origin. If an attacker can inject or alter a <base> element, relative script and link URLs may be redirected too; the actual impact depends on the markup and security controls in place.
Prevent untrusted content from inserting or modifying document metadata. A Content Security Policy can add a defense: the response header Content-Security-Policy: base-uri 'self' restricts base URLs to the page’s own origin; base-uri 'none' disallows them. Choose a policy compatible with the application and test it. base-uri 'self' still permits an unintended same-origin base path, and CSP is not a replacement for preventing HTML injection.
Common Mistakes
- Assuming
/assets/app.jsalways uses the page’s origin. It uses the effective base URL’s origin. - Adding
<base>after stylesheet or script elements and assuming it reliably changes requests already processed. - Forgetting the trailing slash on a directory base.
- Assuming
#sectionalways targets the current document. - Treating a stylesheet’s own relative asset URLs as if they were resolved directly against the HTML document.
- Fixing each broken link independently without checking whether a shared base is responsible.
Best Practices
Use a base element only when its page-wide behavior is intentional. Keep its value early in the head and review links, assets, and fragment navigation together whenever it changes. In automated or manual checks, compare document.baseURI and resolved element URLs with the expected deployment paths. If a CSP is appropriate, configure base-uri alongside the application’s other defenses.
Conclusion
The key diagnostic is the browser’s effective base URL, not just the page’s address or the text inside an href. Check document.baseURI, verify the resolved destinations, and then remove or correct the base before rewriting individual paths.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.