Sets the base URL that every relative URL in the document resolves against, and a default target for links and forms.
Description
The <base> element is a void element in <head> that sets two document-wide defaults:
href: the base URL that every relative URL in the document resolves against. That covers links, images, scripts, stylesheets, form actions, fetch() calls and fragment-only links like href="#id".
target: the default browsing context for links and forms that have no target of their own.
Most sites never need it. It is useful for HTML that will be served from a different location than its links were written for, such as an archived page, an email-style HTML preview or an <iframe srcdoc>. On Vanilla Breeze pages, href has side effects you should know about first: see VB components and fragment links.
Basic Example
A <base> must have href, target or both. A bare <base> is invalid and does nothing.
Base URL
<head> <meta charset="UTF-8"> <base href="https://example.com/docs/"> <title>Guide</title> <link rel="stylesheet" href="styles.css"></head><body> <a href="getting-started.html">Getting started</a> <a href="/about/">About</a></body>
Here styles.css loads from https://example.com/docs/styles.css, wherever the page itself is served.
Default target
Every link and form without its own target now opens in a new tab or window. Read the Accessibility notes before you use this.
Attributes
| Attribute |
Description |
href |
The document's base URL. It can itself be relative, in which case it resolves against the document's own URL. data: and javascript: URLs are ignored. |
target |
The default browsing context: _self, _blank, _parent, _top or a name. See target. |
<base> also supports global attributes, though none of them changes its behavior.
Pitfalls
Only the first one counts
A document may have only one <base>. If there are more, browsers use the first <base> with an href for the base URL and the first with a target for the default target, and ignore the rest.
Put it before anything that uses a URL
The spec requires <base href> to come before every other element with a URL attribute (<link>, <script src>, <meta property="og:url"> and so on). Place it right after <meta charset>.
<head> <link rel="stylesheet" href="styles.css"> <base href="https://example.com/docs/"></head> <head> <meta charset="UTF-8"> <base href="https://example.com/docs/"> <link rel="stylesheet" href="styles.css"></head>
Fragment links resolve against the base too
A link like <a href="#install"> is a relative URL, so it resolves to the base URL plus #install. If the base URL is not the current page's URL, the browser goes to that other page instead of scrolling. This breaks skip links, footnote references and hand-written tables of contents.
<base href="https://example.com/docs/"> <a href="#install">Installation</a>
What it does not affect
url() values in an external stylesheet resolve against the stylesheet's own URL. url() values in a <style> element or a style attribute do use the base URL.
- Absolute URLs (
https://…) are never changed. Root-relative URLs (/path) keep their path but take the base's origin.
VB components and fragment links
Avoid <base href> on pages that use VB's navigation components unless the base URL is exactly the page's own URL. Several components build plain #id links and let the browser follow them, so with a different base URL they leave the page:
| Component |
Generated links |
With a different <base href> |
<foot-notes> |
Reference links ([1]) to notes, and back-links from notes to references |
Breaks: the links go to the base URL |
<site-index> |
A–Z jump navigation (#index-a and so on) |
Breaks: the links go to the base URL |
<glossary-wc> |
A–Z jump bar and "See also" links between terms |
Breaks: the links go to the base URL |
<page-toc> |
Table of contents links to headings |
Works: clicks are intercepted and the page scrolls to the heading. The URL hash is built from the current page URL. |
<heading-links> |
Anchor links on headings |
Works: clicks are intercepted, and the copied link is built from the current page URL |
The skip-link pattern (<a href="#main">) and any #id links in your own content break the same way. If your pages are served from more than one location, write absolute or root-relative URLs at build time (for example with your static site generator's URL helper) instead of using <base href>.
<base target> on its own does not change URLs, so it does not break these components.
JavaScript
document.baseURI returns the document's current base URL: the resolved <base href> if there is one, otherwise the document URL. A <base> element's href property returns the resolved URL, while getAttribute('href') returns the value as written.
document.baseURI; new URL('images/logo.svg', document.baseURI).href; fetch('data/items.json');
Read location.href for the page's own URL. When building in-page links in script, start from location.href rather than writing a bare #id, as <page-toc> and <heading-links> do.
Accessibility
<base target="_blank"> opens every link in a new window without warning. Unexpected new windows disorient screen reader and magnifier users, and break the Back button. WCAG 3.2.5 Change on Request (Level AAA, technique G201) asks you to warn users before a link opens a new window. A document-wide default makes that impractical: the warning would have to be on every link. Set target="_blank" on the few links that need it and label them. See target and Accessibility Utilities › "Opens in new tab".
- Inherited targets are invisible to selectors. A link that gets
_blank from <base> has no target attribute, so CSS hints and scripts that look for a[target="_blank"] (including VB's rel="noopener" upgrade) skip it. Browsers still apply implicit noopener to _blank navigation.
- Broken in-page links (see above) affect keyboard users first: a skip link that loads another page instead of moving focus to
<main> fails WCAG 2.4.1 Bypass Blocks.
Security
An attacker who can inject markup can insert a <base href="https://evil.example/">. Every later relative <script src> and form action then points at their server, even when script injection is otherwise blocked. Restrict <base> with the Content Security Policy base-uri directive:
Content-Security-Policy: base-uri 'none';# or, if you use <base href> on your own origin:Content-Security-Policy: base-uri 'self';
base-uri does not fall back to default-src, so set it explicitly. A <base href> the policy blocks is ignored, and the document keeps its own URL as the base.
Related
<head> - Where <base> lives, and in what order
<link> - External resources whose relative URLs <base> rewrites
<a> - Links, including fragment links
target - Browsing-context values shared by <a>, <form> and <base>
- Link relations -
rel vocabulary, including noopener