Container for document metadata where base elements must be placed. Learn more →
<base> ElementThe base element specifies a default URL and target for all relative URLs in a document. It acts as a foundation that all relative links, images, scripts, and stylesheets build upon.
<base href="url"><base target="target"><base href="url" target="target">The <base> element must be placed inside the <head> section and appears before any elements that use URLs. It’s a void element, meaning it has no closing tag.
| Attribute | Description | Values |
|---|---|---|
href | Base URL for all relative URLs in the document | Absolute URL |
target | Default browsing context for all links and forms | _self, _blank, _parent, _top, or frame name |
A document can contain only one <base> element. If multiple <base> elements exist, only the first href and first target are used, and the rest are ignored.
<head> <base href="https://example.com/" target="_blank"> <!-- Other head elements --></head><head> <base href="https://example.com/"> <base target="_blank"> <!-- Ignored! --> <base href="https://other.com/"> <!-- Ignored! --></head>The <base> element must appear inside the <head> section, before any elements with URLs or links.
When present, the base element affects every relative URL in the document, including links (<a>), images (<img>), scripts (<script>), stylesheets (<link>), forms (<form>), iframes (<iframe>), and more.
Perfect for documents served from different locations or for single-page applications:
Make all links open in a new tab by default:
Set both the base URL and default target behavior:
Serve static assets from a content delivery network:
Fragment identifiers (anchor links starting with #) are resolved against the base URL, not the current document URL. This can break in-page navigation.
<base href="https://example.com/docs/">
<!-- This tries to go to https://example.com/docs/#section2 --><!-- NOT the #section2 in the current page! --><a href="#section2">Jump to Section 2</a>
<h2 id="section2">Section 2</h2><base href="https://example.com/docs/">
<!-- Use the full current URL for fragment links --><a href="/current-page.html#section2">Jump to Section 2</a>
<!-- Or use JavaScript to get the current URL --><a href="" onclick="location.hash='section2'; return false;"> Jump to Section 2</a>You cannot change the base URL or target partway through a document. Only the first base element counts.
<head> <base href="https://cdn1.example.com/"> <!-- This second base is completely ignored --> <base href="https://cdn2.example.com/"></head>JavaScript methods like new URL(relativeUrl, document.baseURI) respect the base element. Be aware when manipulating URLs in scripts.
// If <base href="https://example.com/app/">const url = new URL('page.html', document.baseURI);console.log(url.href); // "https://example.com/app/page.html"When using base elements with single-page applications or client-side routing, be careful about how routes are resolved. The base URL affects fetch requests, dynamic imports, and history API operations.
<!-- In an SPA, this affects router behavior --><base href="/app/">
<script> // fetch() uses the base URL fetch('data.json') // Fetches from /app/data.json
// Router pushState is affected history.pushState({}, '', 'page') // Goes to /app/page</script>The base element affects form submission URLs too:
<base href="https://example.com/api/">
<!-- Submits to https://example.com/api/submit --><form action="submit" method="POST"> <button>Submit</button></form>Consider avoiding the base element in these scenarios:
Multiple base URLs needed. If different sections of your page need different base URLs, the single base element limitation makes this impossible. Use absolute URLs instead.
In-page navigation is critical. Sites with extensive anchor link navigation (like documentation sites) may find the fragment link behavior problematic.
Third-party scripts. External scripts might not expect a base element and could break when resolving their own relative URLs.
Development vs production complexity. Managing different base URLs between environments can add configuration complexity. Environment variables in your build process might be simpler.
The base element has no direct accessibility impact since it doesn’t render visible content. However, its effects can indirectly affect accessibility:
Screen reader navigation. When links open in new tabs due to target="_blank", always inform users explicitly in the link text, not just through the base element.
Keyboard navigation. Users navigating by keyboard need consistent behavior. Unexpected new tab openings (from base target) can be disorienting.
Link purpose clarity. The base element can obscure where links actually go. Ensure link text and context make the destination clear, especially when the visual URL differs from the resolved URL.
<base target="_blank">
<a href="report.pdf"> Annual Report (opens in new tab, PDF)</a><base target="_blank">
<a href="report.pdf">Annual Report</a><!-- User doesn't know it opens in new tab -->| Browser | Version | Notes |
|---|---|---|
| Chrome | 1+ | Full support |
| Firefox | 1+ | Full support |
| Safari | 1+ | Full support |
| Edge | 12+ | Full support |
| IE | 5.5+ | Full support |
The <base> element has been supported since the earliest browsers. The target attribute is supported in all browsers that support frames and tabs.
Container for document metadata where base elements must be placed. Learn more →
Anchor elements for hyperlinks, affected by base href and target. Learn more →
Links to external resources, affected by base href for relative URLs. Learn more →