How to Use the HTML “base” Tag

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 <a>,<area>, and<link> elements will reference the base URL defined in the <base> element.

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:

1
<head>
2
    <base href="https://www.myepicsite.com/assets/">
3
</head>

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

1
<head>
2
    <base href="https://www.myepicsite.com/assets/">
3
    <link rel="stylesheet" href="style.css">
4
</head>
5
<body>
6
    <img src="image01.png" />
7
</body>

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:

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

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

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

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

Example 3: Default Link Target

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

1
 <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.

Drawbacks

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.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Kezz Bracey

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 <a>,<area>, and<link> elements will reference the base URL defined in the <base> element.

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:

1
<head>
2
    <base href="https://www.myepicsite.com/assets/">
3
</head>

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

1
<head>
2
    <base href="https://www.myepicsite.com/assets/">
3
    <link rel="stylesheet" href="style.css">
4
</head>
5
<body>
6
    <img src="image01.png" />
7
</body>

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:

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

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

1
<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:

1
 <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.

Drawbacks

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.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Kezz Bracey


Print Share Comment Cite Upload Translate Updates
APA

Kezz Bracey | Sciencx (2014-06-12T01:54:57+00:00) How to Use the HTML “base” Tag. Retrieved from https://www.scien.cx/2014/06/12/how-to-use-the-html-base-tag/

MLA
" » How to Use the HTML “base” Tag." Kezz Bracey | Sciencx - Thursday June 12, 2014, https://www.scien.cx/2014/06/12/how-to-use-the-html-base-tag/
HARVARD
Kezz Bracey | Sciencx Thursday June 12, 2014 » How to Use the HTML “base” Tag., viewed ,<https://www.scien.cx/2014/06/12/how-to-use-the-html-base-tag/>
VANCOUVER
Kezz Bracey | Sciencx - » How to Use the HTML “base” Tag. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2014/06/12/how-to-use-the-html-base-tag/
CHICAGO
" » How to Use the HTML “base” Tag." Kezz Bracey | Sciencx - Accessed . https://www.scien.cx/2014/06/12/how-to-use-the-html-base-tag/
IEEE
" » How to Use the HTML “base” Tag." Kezz Bracey | Sciencx [Online]. Available: https://www.scien.cx/2014/06/12/how-to-use-the-html-base-tag/. [Accessed: ]
rf:citation
» How to Use the HTML “base” Tag | Kezz Bracey | Sciencx | https://www.scien.cx/2014/06/12/how-to-use-the-html-base-tag/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.