HTML <base> Tag
When building a website, you may need to link to several pages, images, documents, and other resources. Some of these links use complete web addresses, whereas others contain only a relative path. The HTML <base> tag helps manage these relative paths by defining a common starting URL for the entire document.
It can also specify where hyperlinks and form submissions should open by default. This means you can establish a default destination and browsing context without repeating the same information in every link.
The <base> element belongs inside the <head> section of an HTML document. It does not display anything on the webpage, but its settings influence how the browser handles URLs throughout the document.
Syntax of the HTML <base> Tag
The general syntax is:
<base href="URL" target="browsing-context">
The element has two main attributes: href and target. You can specify either one or use both in the same element.
Understanding the href Attribute
The href attribute establishes the base URL for resolving relative URLs in an HTML document. Whenever the browser encounters a relative path, it uses this address as the starting point to determine the complete destination.
For example, suppose you want the relative links on your website to use the following address as their starting point:
https://sey8.blogspot.com/
You can define it in the document's head section as follows:
<head>
<base href="https://sey8.blogspot.com/">
</head>
Now consider these two elements:
<a href="tutorials/html-guide.html">HTML Guide</a>
<img src="images/html-cover.jpg" alt="HTML Guide Cover">
Because both paths are relative, the browser combines them with the base URL. Their resulting addresses are:
https://sey8.blogspot.com/tutorials/html-guide.htmlhttps://sey8.blogspot.com/images/html-cover.jpg
These are illustrative paths. The corresponding pages and image must exist at those locations for the links and image to work.
Without a base URL, the browser would resolve relative paths against the URL of the current document. By defining a base URL, you can change the starting point used for those paths.
Relative URLs and Absolute URLs
It is important to understand the difference between relative and absolute URLs when using the <base> element.
A relative URL contains a path that the browser must resolve against a base address. For example:
pages/contact.html
images/logo.png
../documents/manual.pdf
An absolute URL provides a complete address, including the scheme and domain. For example:
https://www.wikipedia.org/
https://www.google.com/
https://sey8.blogspot.com/
The href attribute of the <base> element affects relative URLs. It does not replace an absolute URL with the base address.
For example, if your document contains the following link:
<a href="https://www.wikipedia.org/">Wikipedia</a>
The browser still opens Wikipedia, even if the document has this base URL:
<base href="https://sey8.blogspot.com/">
Understanding the target Attribute
The target attribute determines the default browsing context for hyperlinks and form submissions. In simpler terms, it controls where the browser opens a linked page or displays the result of a form submission.
For example, you can set the default target to a new browser tab:
<head>
<base target="_blank">
</head>
After this setting, hyperlinks without their own target attribute will normally open in a new tab.
HTML provides four commonly used target keywords:
| Target Value | Purpose |
|---|---|
_self |
Opens the destination in the current browsing context. This is the default behaviour. |
_blank |
Opens the destination in a new browsing context, usually a new tab. |
_parent |
Opens the destination in the parent frame. If no parent frame exists, it behaves like _self. |
_top |
Opens the destination in the top-level browsing context, outside any frames. |
You can also use a custom name as the target value. This allows multiple links to navigate to the same named browsing context.
Overriding the Default Target
The target specified in an individual hyperlink takes priority over the default target established by the <base> element.
Consider the following example:
<head>
<base target="_blank">
</head>
<body>
<a href="https://sey8.blogspot.com/">Visit My Blog</a>
<a href="https://www.wikipedia.org/" target="_self">
Visit Wikipedia
</a>
</body>
The first link opens in a new tab because it follows the default target. The second link opens in the current tab because it explicitly specifies target="_self".
Using Both Attributes Together
You do not need separate <base> elements to define the default URL and target. Both attributes can be included in a single element.
For example:
<head>
<base href="https://sey8.blogspot.com/" target="_blank">
</head>
<body>
<a href="tutorials/css-guide.html">CSS Guide</a>
<a href="tutorials/javascript-guide.html">JavaScript Guide</a>
</body>
In this example, both relative links use your blog's root URL as their starting point. They also open in new tabs because the default target is set to _blank.
The browser resolves the destinations as follows:
https://sey8.blogspot.com/tutorials/css-guide.htmlhttps://sey8.blogspot.com/tutorials/javascript-guide.html
This approach can be useful when several links share the same base address and opening behaviour.
How the Base Tag Affects Other HTML Elements
The effect of the <base> element is not limited to hyperlinks. Its base URL is also used to resolve relative URLs in other elements that refer to external resources.
Here are some common examples:
- Images: The
srcattribute of an<img>element. - Stylesheets: The
hrefattribute of a<link>element. - JavaScript files: The
srcattribute of a<script>element. - Forms: The
actionattribute of a<form>element. - Other resources: URL references in elements such as
<audio>,<video>, and<iframe>.
For example, suppose you have defined this base URL:
<base href="https://sey8.blogspot.com/">
The following image and stylesheet references will use that address:
<img src="media/banner.jpg" alt="Website Banner">
<link rel="stylesheet" href="styles/main.css">
The browser will look for the files at:
https://sey8.blogspot.com/media/banner.jpg
https://sey8.blogspot.com/styles/main.css
This behaviour can save you from writing the same domain and directory path repeatedly. However, you must make sure that the referenced files actually exist at the resulting locations.
Important Rules When Using the <base> Element
Although the <base> element is simple to use, there are a few important rules to follow.
-
Only one base element is allowed.
An HTML document must not contain more than one
<base>element. If multiple elements are present, browsers use only the first applicablehrefand first applicabletargetvalues. -
It must be placed inside the head section.
The
<base>element belongs in the document's<head>, not in the<body>. -
At least one attribute is required.
You must provide an
hrefattribute, atargetattribute, or both. -
Position matters.
When the element has an
hrefortargetattribute, place it before other elements that use URL attributes or create hyperlinks, as applicable. -
It does not have a closing tag.
The
<base>element is a void element. Write it as a single tag rather than adding</base>. -
Individual targets can override the default.
A hyperlink or form with its own
targetattribute uses that value instead of the default target.
How Does the Base Tag Affect Links to Sections on the Same Page?
One detail that developers sometimes overlook is how the <base> element handles fragment links. A fragment link normally points to a particular section of the current page using an element's id attribute.
For example:
<a href="#features">View Features</a>
<h2 id="features">Website Features</h2>
Under normal circumstances, clicking the link moves the browser to the section identified by features.
However, if you have specified a base URL, the browser resolves the fragment against that address. For example:
<base href="https://sey8.blogspot.com/">
With this setting, the link #features resolves to:
https://sey8.blogspot.com/#features
This may take the visitor to a different document or location rather than the section they expected to reach in the current page.
If your page uses a base URL and you want a link to point to a section of the current document, use the complete URL of that document followed by the fragment identifier.
Can the Base Tag Have a Relative URL?
Yes. The href attribute does not always need to contain a complete web address. It can also use a relative URL.
For example:
<base href="../resources/">
In this case, the browser resolves the base URL against the document's original URL. Other relative paths in the document are then resolved using the resulting base address.
However, an absolute URL is often easier to understand because it clearly identifies the website and directory that should serve as the starting point.
Also, the href attribute must not use the data: or javascript: URL schemes.
Does the <base> Tag Support Global Attributes and Events?
The <base> element supports HTML global attributes, which are attributes that can be used on most HTML elements.
However, it does not provide specific event handler attributes for handling user interactions. It is a metadata element and does not represent a visible or interactive part of the webpage.
It also has no default CSS appearance because it does not render visible content.
Browser Compatibility
The HTML <base> element is supported by all major modern web browsers, including:
- Google Chrome
- Mozilla Firefox
- Microsoft Edge
- Apple Safari
- Opera
It is a well-established HTML feature and does not require JavaScript, CSS libraries, or any additional framework to work.
In Summary
The HTML <base> tag provides a convenient way to define the starting URL and default browsing context for an HTML document. Its href attribute controls how relative URLs are resolved, whereas its target attribute determines where links and form submissions open by default.
Although it can simplify URL management, you should use it carefully. A base URL can affect images, stylesheets, scripts, form destinations, and even links to sections within the same page. A small change to this element can therefore influence several parts of a website.
Once you understand how these two attributes work, you can decide when the <base> element is useful and avoid unexpected navigation or resource-loading problems in your HTML projects.



