<base>

Sets the base address for all relative addresses in the document and the default target of links.

Display
none
Closing tag
None, void element
Permitted content
None, an empty element
On this page

Example

#
HTML
<head>
  <meta charset="utf-8">
  <base href="https://www.codeworlds.com/courses/">
  <title>Courses | CodeWorlds</title>
</head>
<body>
  <a href="html">HTML course</a>
  <a href="/pricing">Pricing</a>
  <img src="covers/html.webp" alt="HTML course cover" width="240" height="160">
</body>

The “HTML course” link leads to https://www.codeworlds.com/courses/html, and the image loads from https://www.codeworlds.com/courses/covers/html.webp. A path starting with /, as in the “Pricing” link, skips the directory and points to https://www.codeworlds.com/pricing.

Definition and usage

#

The <base> element is an empty head element that changes how relative addresses are resolved. The href attribute gives the base address from which all relative paths in the document are calculated: in links, images, scripts, and forms. The target attribute sets where links and form results open by default.

A document can have only one <base>, and it must come before all elements that use addresses, ideally right after <meta charset>. If you add several by mistake, the browser takes the first one into account.

Watch out for anchors. With <base> set, a link <a href="#faq"> leads to the base address with #faq appended, not to a section on the current page. If the base address points to another page, a click will take the user elsewhere.

Today <base> is rarely used. You’ll meet it in single-page applications, for example in Angular, which inserts <base href="/"> by default, and on sites published in a subdirectory of a server.

Syntax

#
Syntax
<base href="https://www.codeworlds.com/courses/">

Attributes

#
  • href

    URL

    The base address for all relative addresses in the document. Note the trailing slash: with https://www.codeworlds.com/courses/ the path python gives …/courses/python, while without the trailing slash it gives …/python, because the last segment of the address is treated as a file name.
  • target

    _self | _blank | _parent | _top | name

    The default place to open links and form results that don’t have their own target attribute. With _blank, every link opens in a new tab. An individual link can override this, e.g. with the target="_self" attribute.

The element also supports all global attributes, such as class, id, style and hidden.

Attributes in practice

#

href

URL

The base address for all relative addresses in the document. Note the trailing slash: with https://www.codeworlds.com/courses/ the path python gives …/courses/python, while without the trailing slash it gives …/python, because the last segment of the address is treated as a file name.

HTML
<base href="https://www.codeworlds.com/courses/">
<a href="python">Python course</a>

The link leads to https://www.codeworlds.com/courses/python.

target

_self | _blank | _parent | _top | name

The default place to open links and form results that don’t have their own target attribute. With _blank, every link opens in a new tab. An individual link can override this, e.g. with the target="_self" attribute.

HTML
<head>
  <base target="_blank">
</head>
<body>
  <a href="https://developer.mozilla.org">Opens in a new tab</a>
  <a href="/courses" target="_self">Opens in the same tab</a>
</body>

Default CSS

#

Most browsers display this element with these default styles:

CSS
base {
  display: none;
}

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • End the address in href with a slash if it points to a directory. Without it, the last segment of the address is dropped.
  • After adding <base>, check all anchors like #section. They may start leading to a different page.
  • Before setting <base target="_blank">, consider whether links really need to open new tabs. Users prefer to decide that themselves.

Related entries

#

Spotted a mistake or missing an example? Write to us.