HTML Reference ยท Document and Metadata
The <base> element
Reference for the HTML <base> element: setting the base URL for relative links and a default target, its rules, pitfalls, examples and technical details.
The <base> element sets the base URL that every relative URL in the document is resolved against, and optionally a default browsing context (target) for links and forms. It lives in the <head>, and a document may contain only one.
<head> <base href="https://cdn.example.com/shop/" target="_blank"> </head>
With that base, <img src="logo.svg"> loads https://cdn.example.com/shop/logo.svg, and every link without its own target opens in a new tab.
Try it
Notice that even #faq now points to https://docs.example.com/guide/v2/#faq, not to a spot on the current page. That surprise is the most common reason people avoid <base>.
Attributes
A <base> element must have at least one of href or target.
| Attribute | Values | Description |
|---|---|---|
href |
A URL | The base URL for resolving relative URLs. It may itself be relative; it is then resolved against the document's own address. |
target |
_self, _blank, _parent, _top, or a name |
The default target for <a>, <area> and <form> elements that don't set their own. |
It also accepts the global attributes, although they rarely have a use here.
Usage notes
- Put it first.
<base>affects only URLs that are resolved after it, so place it before any<link>,<script>or<style>that uses a relative URL. Right after the<meta charset>is a good spot. - Only one counts. If there are several
<base>elements, browsers use the first one with anhrefand the first one with atarget. Validators report any extra ones as errors. - Fragment links change meaning.
href="#top"becomes an absolute URL to the base address, so in-page jumps may load another page. Write fragment links in full, or avoid<base>on pages that rely on them. - Prefer root-relative paths. Most sites never need
<base>: paths like/images/logo.svgalready work from any page. Learn the options in File Paths and URLs. - A default target is a usability choice.
target="_blank"on<base>makes every link open a new tab, which many visitors find annoying. Settargetper link instead; see Link Targets and Page Jumps. - Security. Browsers ignore a base URL that uses the
data:orjavascript:scheme. A Content Security Policy can restrict<base>further with thebase-uridirective.
Technical summary
| Content categories | Metadata content |
| Permitted content | Nothing; it is a void element |
| Tag omission | Must have a start tag and no end tag |
| Permitted parents | A <head> element that contains no other <base> element |
| Implicit ARIA role | No corresponding role |
| DOM interface | HTMLBaseElement |
| Default display | none |
Browser support
Supported in every browser.
Related
<head>โ the container for document metadata<link>โ external resources, whosehrefis resolved against the base URL<a>โ hyperlinks and theirtargetattribute- Lessons: File Paths and URLs, Link Targets and Page Jumps, The head Element