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:
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
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.”
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.
is defined along side the
tags within the
. Given the following example, we set the base URL to Google.
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:
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
tag. Try hovering over the link, and the browser will show you where the path is exactly heading to.
All the relative paths will eventually be:
Aside from defining the base URL, the
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
_blank, like so.
tag, however, holds a couple of caveats in some circumstances:
browser support is great; it works in IE6. But, IE6 thinks that it requires a closing tag
. 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
closing within a comment,
If you are using
# in conjunction with the
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.
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