HTML: the base element
The <base> element sets the base URL used to resolve relative URLs and can set the default target for links on a page.
What you will learn
- How to set a base URL for relative links with
<base> - How to set a default link target
- Why this element must be used carefully because it affects every relative URL
Set a base URL
<head>
<base href="https://example.com/docs/">
</head>
<body>
<a href="start.html">Start guide</a>
</body>
With this base URL, start.html resolves to https://example.com/docs/start.html. The base URL is not the same as the page's canonical URL; use the canonical link separately when search engines need it.
Set the default target
<base href="https://example.com/" target="_self">
The target value applies to links and forms that do not specify their own target. Prefer _self unless there is a clear reason to open content elsewhere, and make new-window behavior understandable to users.
Common mistakes
- Using more than one
<base>element. - Forgetting that a fragment such as
#detailsis resolved against the base URL. - Breaking local CSS or JavaScript paths by changing the base without checking them.
- Adding
target="_blank"globally when only one link needs it.