Summary

  • HTML's <base href> sets a starting URL for resolving relative addresses in a document. The written link can remain chapter.html while its resulting destination changes with the base.
  • HTML 2.0 recorded the device for documents read out of context; later standards clarified priority, first-element behavior and a policy control called base-uri.

Imagine a map with ten signposts labeled only “next street.” The labels are unchanged. Someone picks up the map's compass and moves north to another city. Suddenly every “next street” leads somewhere new. An HTML page can play the same trick with <base href>. The links in the page keep their short addresses. The browser changes the address it starts from when it resolves them.

The mechanism had a sensible job before it had a sinister mood. HTML 2.0, published as RFC 1866 in November 1995, described the optional BASE element as an address for interpreting relative URLs when a document was read out of context. A page copied into a message or handled outside its usual web location still needed a place for images/logo.gif or chapter.html to begin. The RFC gave the document an address it could carry with it. It even showed an image reference resolved from a base URI. The page could travel without every short link forgetting home.

By HTML 4.01 in December 1999, the rule was direct: put BASE in the document head, ahead of elements that refer to external resources. Its address takes priority over protocol metadata and, failing that, the document's own location. Links, images, form handlers and stylesheets can all contain relative references. Move the common starting point and several kinds of destination may move together. The standard's own example sends ../cages/birds.gif to a different path by resolving it against a declared products page. The tiny tag had become the page's cartographer.

RFC 3986 in January 2005 described base URI precedence across formats. A base embedded in content outranks the address of an enclosing object or the URL used to retrieve a document. It explained why a document sent by email or another route might need to declare its own starting point. This was a general rule for relative references; HTML supplied the actual tag that let a page exercise it. The distinction matters because a page has both a location where it was fetched and a location it can name as the base for its relative paths. Those two addresses can disagree without the document itself moving.

The maintained HTML Standard makes the consequences concrete. A <base href="/book/"> can make <a href="chapter.html"> lead to /book/chapter.html. Without that base, the browser would resolve the short address from the document's fallback base URL. The same written chapter.html has acquired a different city. The standard expects no more than one base element in a document; if there are several with href, the first one in tree order wins for the base URL. A target attribute can separately set the default browsing destination for links and forms, with its own first-wins rule. One line near the top of a file can govern a surprising amount of what follows.

The browser does more than obey whatever string appears there. The current algorithm rejects an unusable URL and refuses data: or javascript: as base schemes, falling back to the document's own base. It also checks whether the document's content security policy allows the proposed base. Content Security Policy Level 2 introduced base-uri for exactly this control: the publisher can restrict the URLs allowed to set the document's base. base-uri must be stated for this purpose; the CSP2 text says it does not simply inherit default-src. The policy is a brake on a powerful shared setting.

What makes <base> elegant is also what gives it leverage. An editor can move a set of relative references by changing one address. That is much easier than rewriting every link after a document moves. It also means the visible relative strings are poor evidence of where a click will land. The browser resolves them in the context of the document, its first base address and any policy restriction. A reader sees “chapter.html”; the browser has already done the geography.

The 1995 purpose was portability. The 1999 rule gave the author an explicit priority. The modern standard defines the first declaration and its limits. Across those versions, the bargain is the same: compact links rely on an address chosen elsewhere. One line can save a document's directions when the document travels. It can also move every signpost while leaving the signs spotless.