1. Web Design
  3. HTML

How to Use the HTML “base” Tag

Scroll to top
Read Time: 4 min

The <base> tag in HTML is a relatively little known element, but it enables you to do two things:

  1. Set any URL you choose as the base for all relative URLs.
  2. Set default link targets.

The Base-ics

The <base> element is defined in the <head> section and you can only use it once per document. You should place it as early as possible in your head section so you can use it from that point on. Its two possible attributes are href and target. You can use either of these attributes alone or both at once.

Relative URLs used in the href attributes of <a>,<area>, and<link> elements will reference the base URL defined in the <base> element, as will URLs found in the src attribute of (for example) <img> elements.

Example 1: Asset Loading Shortcut

Let’s say you have a site which stores all its images and CSS in a folder named assets. You might define your <base> tag like so:

    <base href="https://www.myepicsite.com/assets/">

This would then allow you to load in any images or CSS like this:

    <base href="https://www.myepicsite.com/assets/">
    <link rel="stylesheet" href="style.css">
    <img src="image01.png" />

The link to style.css would resolve to https://www.myepicsite.com/assets/style.css and image01.png would load from https://www.myepicsite.com/assets/image01.png.

Example 2: Internal Page URLs

What if you have a top level domain which redirects to something like https://thisrocks.com/app/ and all internal links need to include app/ in their URLs.

You might set your base URL like this:

<base href="https://thisrocks.com/app/">

From there whenever you wanted to link from one internal page to another you could simply use:

<a href="anotherpage.html">link</a> 

This link would resolve to https://thisrocks.com/app/anotherpage.html.

You can also use <base> to set the default target for all links on your page. If you were to use:

 <base target="_blank"> 

...any link that didn't explicitly set its own target would open in a new window. You can use the target attribute with or without an accompanying href attribute.

This functionality could potentially be useful for content loaded in via an iframe, as you would be able to automatically have all links therein targeted to the parent frame.


While the <base> url can be handy, its drawbacks center around the fact that after defining it once you’re stuck with it. You can only use it in a single way and it will then affect all URLs. Wherever you don't want to use the defaults set in your <base> tag you have to specifically override them.

Should you use it to resolve to your assets folder as we did above, and you later wanted to link from one page of your site to another, you wouldn’t be able to do so with the common HTML of <a href="page.html">Page</a>

This is because with the base URL being https://www.myepicsite.com/assets/ your visitors would be sent to https://www.myepicsite.com/assets/page.html.

As such you would have to override your <base> tag settings by using the absolute URL instead, i.e. <a href="https://www.myepicsite.com/page.html">Page</a>.

In-Page Anchors

When using <base> you can also run into problems linking to in-page anchors.

Normally, a link such as <a href="#top">Back to Top</a> would keep you on the same page but skip to the location of an element bearing id="top", e.g. it would resolve to https://thisrocks.com/app/article.html#top.

However if you’re using a <base> tag with an href attribute you’d instead be sent to the base URL with #top appended to the end, e.g. https://thisrocks.com/app/#top.

Again, in this case you'd have to override the defaults set in your <base> tag by specifying the page you want your link to be relative to, e.g. <a href="article.html#top">Back to Top</a>.

Where <base> Works Best

The <base> tag is best used in a scenario where you know you’ll be able to exert complete control over the use of all URLs, i.e. you know you’ll be able to override the defaults as required. If you’re creating a theme for a CMS where there are many unknown variables it’s probably a good idea to leave <base> out of the mix.

However, if you’re building a static HTML site <base> can be very helpful. This is the case even more so if you’re using a templating language such as Handlebars that will let you place shortcuts like <a href="{url}#top">Back to Top</a>, so if you need to override default settings it’s still quick and easy.

If you have a good idea what your project’s content will be, and its use of URLs and links is fully under your control, you might find the <base> tag saves you a lot of time.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.