HTML <base> Tag
The HTML <base> tag defines a default URL and target for relative links in an HTML document. It helps browsers determine where relative URLs should point and where linked pages should open.
For example, if a website contains several links to pages, images, stylesheets, or other resources, you can use the <base> tag to define a common starting URL. The browser will then use that URL to resolve relative paths throughout the document.
The <base> tag must be placed inside the <head> section of an HTML document. A document can contain only one <base> element.
Syntax of the HTML <base> Tag
The basic syntax is:
<base href="URL" target="target">
The <base> tag supports two attributes:
- href: Specifies the base URL for relative URLs in the document.
- target: Defines the default browsing context where links and form submissions should open.
You can use either attribute or both together.
HTML <base> Tag Example
Consider the following example:
<!DOCTYPE html>
<html>
<head>
<title>HTML Base Tag Example</title>
<base href="https://www.w3schools.com/" target="_blank">
</head>
<body>
<h2>HTML Base Tag</h2>
<a href="tags/tag_base.asp">HTML Base Tag</a>
<img src="images/stickman.gif" width="24" height="39" alt="Stickman">
</body>
</html>
In this example, the <base> element specifies https://www.w3schools.com/ as the default URL and _blank as the default target.
As a result:
- The link
tags/tag_base.asppoints tohttps://www.w3schools.com/tags/tag_base.asp. - The image path
images/stickman.gifpoints tohttps://www.w3schools.com/images/stickman.gif. - The link opens in a new browser tab because its target is
_blank.
Without the <base> element, the browser would resolve these relative URLs using the current document's URL.
The href Attribute
The href attribute specifies the base URL that the browser uses to resolve relative URLs.
For example:
<head>
<base href="https://www.w3schools.com/">
</head>
<body>
<a href="tags/tag_base.asp">HTML Tutorial</a>
<a href="css/default.asp">CSS Tutorial</a>
</body>
The browser resolves the relative paths using the base URL:
tags/tag_base.aspbecomeshttps://www.w3schools.com/tags/tag_base.asp.css/default.aspbecomeshttps://www.w3schools.com/css/default.asp.
This is useful when a page contains several relative URLs that share the same starting location.
The href attribute can also contain a relative URL. In that case, the browser resolves it against the document's original URL.
The target Attribute
The target attribute specifies where a browser should open a linked resource by default.
For example:
<head>
<base target="_blank">
</head>
<body>
<a href="https://www.w3schools.com/">Visit W3Schools</a>
<a href="https://www.wikipedia.org/">Visit Wikipedia</a>
</body>
Both links open in new browser tabs because the base element sets _blank as the default target.
The main target values are:
| Target | Description |
|---|---|
_self |
Opens the linked resource in the current browsing context. This is the default behavior. |
_blank |
Opens the linked resource in a new browsing context, usually a new tab. |
_parent |
Opens the resource in the parent frame. If there is no parent frame, it behaves like _self. |
_top |
Opens the resource in the full browser window, outside any frames. |
You can also assign a custom browsing context name instead of using these predefined values.
Note: An individual link can have its own target attribute. When that happens, its specified target takes priority over the default target set by <base>.
Important Rules for Using the <base> Tag
There are several rules to remember when working with the HTML <base> element.
- Use only one base element: An HTML document should contain a maximum of one
<base>element. - Place it inside the head section: The element must appear within the document's
<head>. - Specify at least one attribute: The element must have either
href,target, or both. - Place it before URL-based elements: When the
hrefortargetattribute is specified, the<base>element must appear before other elements that use URL attributes, such as<link>. - It is a void element: The
<base>tag does not have a closing tag or any content between opening and closing tags. - Explicit targets take priority: A link with its own
targetattribute uses that value instead of the default target defined by<base>.
Although browsers ignore additional <base> elements, you should not include more than one in a document.
How Does the <base> Tag Affect Internal Page Links?
One important detail about the <base> tag is that it also affects links to sections within the same page.
For example:
<head>
<base href="https://www.w3schools.com/">
</head>
<body>
<a href="#contact">Go to Contact Section</a>
<h2 id="contact">Contact Us</h2>
</body>
You might expect the link to jump directly to the Contact Us section. However, the browser resolves the link against the base URL.
In this example, the resulting URL is:
https://www.w3schools.com/#contact
Therefore, the browser may navigate to that URL instead of simply jumping to the section in the current document.
If you use the <base> element, pay special attention to internal navigation links.
Does the <base> Tag Support Global Attributes?
Yes. The <base> element supports the standard HTML global attributes.
However, it does not support event handler attributes because it is a metadata element rather than an interactive element.
The <base> element also has no default CSS styling.
Browser Support
The HTML <base> element is supported by all modern web browsers, including Google Chrome, Mozilla Firefox, Microsoft Edge, Safari, and Opera.
It is an established HTML feature and does not require any additional library or framework.
Conclusion
The HTML <base> tag provides a simple way to define a common URL and default target for relative links in a document. It can make URL management easier, especially on pages that contain many relative paths.
However, you should use it carefully because it affects more than ordinary hyperlinks. Image paths, resource URLs, form actions, and even links to sections within the same page can be affected by its base URL.
Understanding how the href and target attributes work will help you use this element correctly in your HTML projects.



