Is the HTML <base> tag also honored by scripting and CSS?

13,077

Solution 1

CSS paths are always relative to the stylesheet itself and have no dependence on the HTML location (except when IE6 is buggy and stupid and tries to load .htc files specified in CSS behavior attributes relative to the document). For other stuff, <base> will affect the perceived current directory of the HTML as if the file was located in the directory defined by base. Consequently, it does affect things like location.href=...;. By the way, inline styles and style information in <style> element are loaded relative to the document location. Those are affected by the <base> tag, of course.

Solution 2

The base tag is indeed only honoured by the relative links inside the HTML document itself.

There's however an IE6-specific bug which you really need to take into account when using <base> tag in HTML (not in XHTML). The <base> tag is in HTML documented as not having an end tag </base>, but IE6 incorrectly assumed it for true which will cause that the entire content after the <base> tag is placed as child of the <base> tag in its HTML DOM tree. This can cause at first sight unexplainable problems in Javascript/jQuery/CSS, i.e. the elements being completely unreachable in specific selections (e.g. html>body) until you discover that there's actually a base in between.

A normal IE6 fix is using conditional comments to include the end tag:

<base href="http://example.com/"><!--[if lte IE 6]></base><![endif]-->
Share:
13,077
John K
Author by

John K

Defining quotes: "The present letter is a very long one, simply because I had no leisure to make it shorter." — Blaise Pascal "The early bird gets the worm, but the second mouse gets the cheese." —Steven Wright "And yet what are we to do about this terribly significant business of other people." — Philip Roth

Updated on June 25, 2022

Comments

  • John K
    John K almost 2 years

    The base HTML element provides a base for relative URIs in the HTML. Must JavaScript and CSS also honor it for relative URIs issued in them:

    E.g.

    JavaScript:

    location.href = "mypage.htm"` 
    

    CSS:

    h4 { 
        background-image: url(myimage.gif) 
    }
    

    (in any browser?)

  • BalusC
    BalusC over 13 years
    @Marcus: It's not HTML4-valid (although browsers accept it). But in HTML5 you can perfectly do so.
  • BalusC
    BalusC over 13 years
    @Marcus: Keep in mind that using <base> affects anchors: stackoverflow.com/questions/1889076/…
  • Marcus Downing
    Marcus Downing over 13 years
    I know, I've changed a couple thousand of them :)