Skip to: content, navigation

by Ian Lloyd

href (HTML attribute)

Browser support full matrix
IE5.5+ FF1+ Saf1.3+ Op9.2+
Full Full Full Full
Spec
Depr. Version
No HTML 2
href="base URL address"

Example

The base URL is defined here as a domain:

<base href="http://lloydi.com" />
…
I've published my <a href="/travel-writing/">holiday diaries</a>,
including the <a href="/travel-writing/prague/2006/day2.html">crazy
bone church at Kutna Hora</a>

Description

If all links (a element) or form submissions (action) are going to the same location, it may be preferable to define the base URL so as not to repeat yourself unnecessarily. As an example, this is the kind of saving that you could have:

I've published my
<a href="http://lloydi.com/travel-writing/">holiday diaries</a>,
including the
<a href="http://lloydi.com/travel-writing/prague/2006/day2.html">
crazy bone church at Kutna Hora</a>, and our recent trip to
<a href="http://lloydi.com/travel-writing/portugal/algarve/day1.html">
Portugal</a>

Given that all the links are in the directory "travel-writing" on the domain "lloydi.com", the base href can be used to reduce the markup as follows:

<base href="http://lloydi.com/travel-writing/" />
…
I've published my
<a href="./">holiday diaries</a>,
including the
<a href="./prague/2006/day2.html">
crazy bone church at Kutna Hora</a>, and our recent trip to
<a href="./portugal/algarve/day1.html">
Portugal</a>

Note the syntax in the a element "href" values begins with "./".

There is a flaw with using the base element for this purpose though. Consider this scenario - you set a base href to be a domain root:

<base href="http://example.org/">

Then, in the document you have a link to a fragment identifier foo, a section within the same page that you’d like the user to be able to jump to.

<a href="#foo">Foo</a> links to foo
…
<h1 id="foo">Foo</h1> here is the desired destination

The problem is that the browser will not jump to that section on the page but will instead load the URL http://example.org/#foo.

So while it may have its uses, you must be careful to avoid a situation like this - using a base href value may be somewhat akin to using a sledge hammer to knock in a nail.

Value

The value in the base href must be an absolute URI that acts as the base URI for resolving relative URIs, e.g. "http://www.domain-name.com/".

Compatibility

IE5.5Full
6.0Full
7.0Full
Firefox1.0Full
1.5Full
2.0Full
Safari1.3Full
2.0Full
3.0Full
Opera9.2Full
9.5Full

Causes no compatibility issues. It has excellent support across all tested browsers.

User-contributed notes

There are no comments yet.

Add a note

To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

Related Products

The Principles of Beautiful Web Design

Best Seller!

You don’t need to go to Art School to design great looking web sites!

Book Cover: The Principles of Beautiful Web Design

Download the FREE sample chapters