The <base> element sets the base URL that the browser uses to resolve every relative URL in a document, and a default target for links. You place a single <base href="https://example.com/app/"> in the <head>, and links such as <a href="page.html"> then resolve against that address. Use the html base tag sparingly, because it changes how every relative link, image and script path behaves.
It is helpful for pages served from a different path than where their assets live, and it is a common source of broken in-page links in single-page applications.
Normally a relative URL resolves against the address of the current page. With <base href>, it resolves against the base instead.
The link resolves to https://example.com/docs/intro.html and the image to https://example.com/docs/images/diagram.png, no matter what URL the page itself was loaded from.
The specification gives strict rules, and browsers follow them.
<base> element; only the first one with an href counts.<head>, and it should come before any element that uses a URL, such as <link> or <script>.href for the base URL and target for the default browsing context. At least one must be present.href can be relative, but it is then resolved against the document's own URL.Placing it first in the <head> after the character set declaration avoids surprises, because elements before it are resolved with the old base.
<base target="_blank"> makes every link open in a new tab unless the link has its own target.
This can annoy users and hide the real intent of each link, so prefer setting target on individual links only where it helps.
The base URL also affects fragment-only links. With <base href="https://example.com/docs/">, the link <a href="#setup"> points to https://example.com/docs/#setup, not to the section on the current page. If the page is not at that address, the browser navigates away.
If the page lives at /guide/start, clicking this link leaves the page and loads /docs/#setup. The fix is to avoid the base element, or to write fragment links with the full path of the current page.
The base also applies to SVG references such as url(#gradient) in some cases, form action values without a path, and <a> links in dynamically inserted content.
| Situation | Recommendation |
|---|---|
Static site hosted under a sub-path such as /project/ | Reasonable: one <base href="/project/"> |
| Single-page app with client-side routing | Common, but test fragment links and the <link> paths |
| Email or offline copies of a page | Use absolute URLs instead |
| Content inserted by a CMS from several authors | Avoid; a stray base changes every link |
| A site with many pages at different depths | Prefer root-relative paths such as /images/logo.png |
A root-relative path (starting with /) or an absolute URL often solves the same problem without a global side effect. Frameworks usually provide a configuration option, so check whether the tool writes the <base> for you instead of adding it by hand.
Because <base> rewrites every relative URL, an attacker who can inject one into a page can redirect scripts, forms and links to their own server. A strict Content Security Policy with the base-uri directive limits where a base may point.
No. Only the first <base> with an href is used, and the rest are ignored.
Relative URLs passed to fetch() or new URL() without a second argument resolve against the document base URL, so yes, they are affected. You can read it with document.baseURI.
No. It is part of the HTML living standard and works in all browsers.
A fragment-only link resolves against the base URL, so it leaves the current page. Remove the base or write the full path in those links.
The <base> element sets one base URL and one default target for the whole document. Put a single one first in the <head>, remember that it changes fragment links, script paths and fetch() calls, and prefer root-relative or absolute URLs when you only need to fix a few paths. Add base-uri to your Content Security Policy for protection.