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.”
<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
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.
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
_blank, like so.
<base href="http://hongkiat.maxcdn.com/assets/" target="_blank">
<base> tag, however, holds a couple of caveats in some circumstances:
<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.
<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
- DOM element as a bookmark makes Firefox’s bookmarks fully dynamic
- Document Sharing Sites
- Microsoft Improves Office Document Integration in Outlook for iOS
- LibreOffice Viewer for Android is a basic document reader
- Daminion is a professional photo, video and document management software
- How to Improve HTML Table Accessibility with Markup