HTML <base> Tag

The <base> tag defines a base URL to use for all relative URLs in a document.

If the <base> element has a target attribute, it will be used as the default attribute for all hyperlinks appearing in the document.

This tag must be located inside a <head> tag and maximum one <base> element can be used in a document.

Syntax

The <base> tag is an empty tag, which means that it has no end tag in HTML.

But in XHTML the <base> tag must be properly closed with the (<base />) closing tag.

Example

<!DOCTYPE html>
<html>
  <head>
    <title>HTML base tag</title>
    <base href="https://www.w3docs.com/" target="_blank">
  </head>
  <body>
    <a href="https://www.w3docs.com/css3-maker/border-radius">Check our CSS maker tool.
  </body>
</html>

The above-mentioned example shows a clickable text like "Check our CSS maker tool" and when you click on that text, you will see the page opened in a new tab, because target="_blank" attribute value is used.

Let's see another example where the <base> tag is used with the <img> tag as its source. That will be enough to show the pictures.

Example

<!DOCTYPE html>
<html>
  <head>
    <title>HTML base tag</title>
    <base href="https://www.w3docs.com/images/" target="_blank">
  </head>
  <body>
    <img src="/uploads/media/default/0001/01/25acddb3da54207bc6beb5838f65f022feaa81d7.jpeg" alt="Aleq" width="200" height="185" />
    <img src="/uploads/media/default/0001/01/910a300bcc9d9885ea035805ac6cac5d719bc43e.jpeg" alt="logo" width="200" height="150" />
  </body>
</html>

Here is the result:

< /amp-img>

Relative links

When creating an anchor link, the href attribute can specify an absolute URL, like http://w3docs.com, or a relative URL such as /, /page-name, or page-name.

With relative URLs a slash ( / ) indicates the root URL of the current page, so href="/page-name" would link to a page at currentdomain/page-name. Leaving off the slash links to a subpage of the current URL, so href="page-name" would link to current_url/page-name.

Attributes:

Attributes Value Description
href URL Defines a base URL for all the relative URLs in the document.
target _blank
_parent
_self
_top
Defines the frame name where all links in the document will be opened by default.

The <base> tag also supports the Global Attributes.