Skip to content
Tutorials Center
My learning

Topics

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.

HTML
<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

How a base URL changes relative links

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 an href and the first one with a target. 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.svg already 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. Set target per link instead; see Link Targets and Page Jumps.
  • Security. Browsers ignore a base URL that uses the data: or javascript: scheme. A Content Security Policy can restrict <base> further with the base-uri directive.

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.