Specifying Document Base URL With HTML <base> Element

Websites are built with a series of links, pointing to pages and sources like images and stylesheets. There are two ways to specify the URL that links to these sources: either use an absolute path or relative path.

The absolute path refers to specific destination, typically it’s started with the domain name (along with HTTP) like www.domain.com/destination/source.jpg. The relative path is the opposite: the link destination depends on the root location or in most cases the domain name of your website.

A typical relative path would look like this below:

<img src="/assets/img/image.png" alt="">

If your website domain is, for example, hongkiat.com the image path would resolve to hongkiat.com/assets/img/image.png. You should understand this if you have been developing website for a while.

But most of you probably have not heard about the <base> element. This HTML tag has been around since HTML4, yet very little is seen of its implementation in the wild. W3C describes this element as:

“The base element allows authors to specify the document base URL for the purposes of resolving relative URLs, and the name of the default browsing context for the purposes of following hyperlinks.”

This <base> element basically decides the base URL for relative path in web pages. Instead of depending on the root location or the domain of your website, you can point it out to somewhere else, perhaps like the URL where your resources reside in CDN (Content Delivery Network). Let’s see how that actually works.

Using the Base Element

The <base> is defined along side the <meta> and <link> tags within the <head>. Given the following example, we set the base URL to Google.

<base href="http://hongkiat.maxcdn.com/assets/">

This specification will affect all the paths within the document, including one that is specified within the href attribute and the src of the images. So, assuming we have a stylesheet, images, and links in the document set with a relative path like this, for example:

<link rel="stylesheet" href="path/main.css">
<a href="path/page/sub-page/">Anchor Link</a>
<img src="path/image.jpg">

Even though our web page is under demo.hongkiat.com the relative path will refer to hongkiat.maxcdn.com, following the base path specified in the <base> tag. Try hovering over the link, and the browser will show you where the path is exactly heading to.

url path

All the relative paths will eventually be:

<link rel="stylesheet" href="http://hongkiat.maxcdn.com/assets/path/main.css">
<a href="http://hongkiat.maxcdn.com/assets/path/page/sub-page/">Anchor Link</a>
<img src="http://hongkiat.maxcdn.com/assets/path/image.jpg">

Setting the Default Link target

Aside from defining the base URL, the <base> tag can also set the default link target through the target attribute. Say you want all the link in the document to open in the browser new tab, set the target with _blank, like so.

<base href="http://hongkiat.maxcdn.com/assets/" target="_blank">
Limitations

The <base> tag, however, holds a couple of caveats in some circumstances:

First, the <base> browser support is great; it works in IE6. But, IE6 thinks that it requires a closing tag </base>. This could cause a hierarchy issue in the document, if the closing tag is left unspecified. A simple quick way to address this issue is by adding </base> closing within a comment, <!--[if lte IE 6]></base><![endif]-->.

If you are using # in conjunction with the <base> to link to sections within the document, you may potentially encounter an issue in Internet Explorer 9. Instead of jumping to the referred section, Internet Explorer 9 will reload the page.

Furthermore, a blank href will result in the base URL instead of linking to the current directory where the page resides (this is default browser behavior), which could cause unexpected referencing issues.

Wrap Up

The <base> is a handy HTML feature that may simplify link referencing in a web document. Use the tag considerately to minimize the pitfalls. Follow these reference below for more on the <base> tag:

WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail