How to use the HTML base tag to define a base URL
HTML <base>
is used to define the base URL of all relative URLs. This is done via the attribute href
. The second attribute target
defines where the respective reference targets are to be opened. The HTML <base>
tag is always stored within the <head>
element.
What is the HTML <base>
tag?
The HTML tag <base>
sets the base URL for all relative URLs on a website. It includes the two HTML attributes href and target.
The href attribute specifies the URL that should serve as the reference for all relative URLs, images, stylesheets and scripts in an HTML document. This base URL can itself be relative. The target attribute defines the name of the window where all linked targets should be opened. Additionally, the HTML <base>
tag can specify how links in the current document should be opened.
The HTML <base>
tag is always stored within the <head>
element and should be used as early as possible in the code. If there are several <base>
tags, only the first one is taken into account, and all others are ignored.
- 99.9% uptime and super-fast loading
- Advanced security features
- Domain and email included
What is the syntax of HTML <base>
?
The basic syntax of HTML <base>
is as follows:
<base href="URL" target="TARGET">
htmlExamples of how <base>
works
Storing the base URL
In the first example, we store the base URL https://www.example-website.com
in the header area of a document using the HTML <base>
tag:
<!DOCTYPE html>
<html>
<head>
<title>HTML: base Tag example</title>
<base href="https://www.example-website.com" />
</head>
<body>
<p>
Here’s your website content.
</p>
</body>
</html>
htmlDefining default destination for all URLs
In the next example, we’ll use the target
attribute to specify where URLs should open when they are clicked:
<!DOCTYPE html>
<html>
<head>
<title>HTML: base Tag example</title>
<base href="https://www.example-website.com" target="_self">
</head>
<body>
<p>
Here’s your website content.
</p>
</body>
</html>
htmlAccessing a base URL with multiple relative links
In the third example, we use HTML <base>
to create a base URL that is used as the start page by all relative links. This is what the code looks like:
<!DOCTYPE html>
<html>
<head>
<title>HTML: base Tag example</title>
<base href="https://www.example-website.com/">
</head>
<body >
<h2>Various subpages</h2>
<ul>
<li><a href="/first_subpage/index.htm">First Subpage</a></li>
<li><a href="/second_subpage/index.htm">Second Subpage</a></li>
</ul>
</body>
</html>
html- Free website builder with .co.uk
- Free website protection with one Wildcard SSL
- Free 2 GB email account
What attributes does the HTML <base>
tag support?
The <base>
tag only supports the attributes href
and target
.
href
: The base URL for all relative URLs on the page is determined viahref
. The value of this attribute is always specified in the form of a URL. In the examples above, this value ishttps://www.example-website.com
.target
: Thetarget
attribute specifies which window a URL should be opened in. It can have the values_blank
,_parent
,_self
and_top
._blank
opens the link in a new window,_parent
opens the link in a higher-level frame,_self
opens the link in the same window and_top
opens the link in the browser window, replacing the linking page instead of presenting it as a frame within the page.
In our Digital Guide, you can find more informative articles about HTML. Among other things, you’ll find a comprehensive beginners’ tutorial as well as an overview of the best HTML editors.