How to get HTML5 working in IE and Firefox 2

HTML 5 may be the latest and greatest technology, but some browsers don’t have native support for the new semantic elements. Let’s momentarily forget about the really sexy functionality, like full control over the <video> element, and just focus on getting the elements rendered.

The problematic A-grade browsers include IE 8 and below, Firefox 2, and Camino 1 (these last two browsers both use the Gecko rendering engine, which is why they’re both affected).

Let’s start with Internet Explorer.

IE doesn’t believe in HTML 5 elements

Quite simply, IE doesn’t even see HTML 5 elements, much less style them.

This is actually the same issue that we had before HTML 5, where the <abbr> element couldn’t be styled in IE 6, resulting in all manner of workarounds. (Let me add that we’ll also fix the <abbr> element while we convince IE to recognise HTML 5 elements).

The fix

There is hope! The trick, discovered by Sjoerd Visscher, is simply to create the new element using JavaScript, and voilà, IE is able to style it:

document.createElement('header');

John Resig has also written about this HTML 5 shiv.

For example, say you wanted to style the <time> element in italics:

<!DOCTYPE html>
<html>
<head>
  <title>Header test</title>
  <style>
  time { font-style: italic; }
  </style>
</head>
<body>
  <time datetime="2009-09-13">my birthday</time>
</body>
</html>

This screenshot shows the rendering in IE before we apply the fix:

IE without HTML 5 shiv

To apply the fix, add the indicated line of code:

<!DOCTYPE html>
<html>
<head>
  <title>Header test</title>
  <style>
  time { font-style: italic; }
  </style>
  
  <!-- Add this line -->
  <script>document.createElement('time');</script>
</head>
<body>
  <time datetime="2009-09-13">my birthday</time>
</body>
</html>

Now after we’ve applied the fix, it’s correctly styled in IE:

IE with HTML 5 shiv

One hit solution

For everyone’s convenience, I wrote a single JavaScript file that can be included to create all the HTML 5 elements (and the <abbr> element) for IE.

Download the IE HTML 5 enabling script

Include the script in your <head> tag, and you’ll be able to style the elements appropriately in IE:

<!--[if lte IE 8]>
<script src="html5.js" type="text/javascript"></script>
<![endif]-->

Note that I’ve used a conditional comment to only apply this to IE 8 and below. It’s my hope that IE 9 and onwards will support HTML 5 elements, but when that day comes, make sure to double check the conditional!

Conditions & Gotchas

There are a couple of things to be aware of when using the HTML 5 shiv.

JavaScript required

This obviously means that your design now depends on JavaScript. Personally, I feel that if you’ve used semantic markup for your site and the elements can’t be styled, the content is still completely readable.

Here’s a screenshot of the Full Frontal web site, written using HTML 5 elements, rendered in IE with and without JavaScript enabled:

IE with and without JavaScript to fix HTML 5

You can see in the second screenshot that the content isn’t perfect, but it’s still readable — it cascades down correctly, much as if CSS were disabled.

A little head is always good

If you create the new element and don’t use a <body> tag (which is perfectly valid HTML 5), IE will put all those created elements inside the <head> tag. Pretty crazy, but you can easily avoid this by always using both the <head> and <body> tags in your markup. Leif Halvard explains further with demos.

Firefox 2 and Camino 1 rendering bug

Both Firefox 2 and Camino 1 have a bug in the Gecko rendering engine (specifically versions prior to 1.9b5):

Firefox 2 (or any other Gecko-based browser with a Gecko version pre 1.9b5) has a parsing bug where it will close an unknown element when it sees the start tag of a “block” element p, h1, div, and so forth.

According to the W3 Schools stats, Firefox 2 only has around 3% of the market — perhaps low enough to justify ignoring it. It’s safe to assume that Camino 1 commands an even smaller percentage of the market.

By ignoring this issue, however, a site can look quite bad in these browsers. So how can we fix it?

The bug surfaces when Gecko doesn’t recognise an element. Explained roughly, when Gecko parses an unrecognised element, it removes the element’s contents and puts them next to the element.

Take, for example, the following code:

<body>
  <header><h1>Title</h1></header>
  <article><p>...</p></article>
</body>

This will be parsed in Gecko (prior to version 1.9b5) as if the markup were actually as follows:

<body>
  <header></header>
  <h1>Title</h1>
  <article></article>
  <p>...</p>
</body>

The visual result is similar to the above screenshot of IE running without JavaScript (though subtly different, as the DOM tree is actually in a different order than you as the author intended).

The fix

There are two approaches to fixing this issue, and so far I've only successfully used the non-JavaScript approach.

The JavaScript solution

The first approach is to use JavaScript to traverse the DOM tree, rearranging elements as issues are encountered. Simon Pieters has a small working example of how this can be done (towards the bottom of the page). In practise, however, I personally found it didn't work for my markup. The problem is definitely solvable using JavaScript, but this solution still needs work to handle all permutations of markup.

The XHTML solution

The second approach is to serve Gecko XHTML. I've found this to be the easier approach if you're either generating a page dynamically (using something like PHP) or if you can create your own .htaccess file to use Apache's mod_rewrite.

The first change to your markup is to add the xmlns attribute to your <html> tag:

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

Next, we need to sniff the user agent string (typically a bad approach, but justifiable when targeting such a specific group of users). If the Gecko build is less than 1.9, then we need to set the Content-type header to application/xhtml+xml.

If you want to use mod_rewrite in an .htaccess file (or the httpd.conf file), you need the following rules:

RewriteCond %{REQUEST_URI} \.html$
RewriteCond %{HTTP_USER_AGENT} rv:1\.(([1-8]|9pre|9a|9b[0-4])\.[0-9.]+).*Gecko
RewriteRule .* - [T=application/xhtml+xml]

This rule sends the proper Content-type header to all Gecko based browsers where version is less than 1.9, or "rv:1.9pre" or "rv:1.9a" or "rv:1.9bx" where x is less than 5.

If you don't want to use the mod_rewrite approach, you'll need to manually send the header in your server side scripts. Here's a solution for a PHP script:

if (preg_match('/rv:1\.(([1-8]|9pre|9a|9b[0-4])\.[0-9.]+).*Gecko/', $_SERVER['HTTP_USER_AGENT'])) {
  header('Content-type: application/xhtml+xml');
}

This snippet needs to be included before anything has been printed by your script — i.e., as early as possible.

Gotcha: Yellow Screen of Death

The Yellow Screen of Death shows up whenever there's an XML error on the page. If we're serving markup as XML and telling the browser to interpret it strictly as XML, then we can't serve any characters it doesn't recognise or else it's going to bork:

Yellow Screen of Death

Below are a few ways to avoid XML parsing errors.

Create strict markup

You need to ensure your markup is squeaky clean — but that's easy, because you're already a Markup Jedi, right?

Use XML entities

HTML entities are a no-no. Sorry, but &bull; isn't going to fly anymore. You need to use XML entities, the numerical representation of these characters.

I've built an entity lookup tool that shows the HTML entity and the XML value of that entity. For example, &bull; is 8226, so the XML entity is &#8226;.

Sanitise user generated content

If your site relies on any user generated content (e.g., blog comments), then you need to sanitise the output to ensure there are no validation issues to trigger the Yellow Screen of Death.

This issue alone may justify further investigation of a JavaScript solution.

Worth the trouble?

All that said, Firefox has a very good automated upgrade path. Looking at the stats on W3Schools, it's safe to say that the number of users with this Gecko bug is rapidly diminishing.

Further reading

Share and Save:
  • Twitter
  • Digg
  • Sphinn
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Netvibes
  • Facebook
  • Google Bookmarks
  • FriendFeed
  • HackerNews
  • LinkedIn
  • NewsVine
  • Tumblr

58 Responses to “ How to get HTML5 working in IE and Firefox 2 ”

Comment by Ryan Roberts at

Great article, this has cleared up a few mind boggling issues I’ve had with Firefox 2. I already knew about the IE fix but your Javascript file looks even better.

Comment by Paul Lloyd at

After this weekend’s standrads.next meet-up, I wondered whether there was an alternative workaround to the IE issue, by using IE’s HTC (HTML Components) functionality. Quickly looking over the documentation I was able to find that you can create new elements with this tool, but they have to be name spaced. I only know enough to be dangerous however – is there any milage investigating a solution that follows this route at all?

Comment by Greg Hemphill at

This IE hack looks very promising… makes me think using html 5 is going to be a viable option sooner than I had thought.

Comment by Remy Sharp at

@Paul – I know what you mean about the HTC solution, and there are a couple of “solutions” to getting HTML5 elements to being styled without JavaScript.

First up – HTC, Dean Edwards’ <abbr> solution, but it requires the markup to be prefixed with html:. Frankly, if I have to prefix every element with html: it’s going to really mess with the markup, and having to maintain it :-(

The second solution was via the WHATWG blog, which does work for a certain amount of markup, but as Simon points out, it’s not particularly scalable, specifically, you still can’t style the HTML5 elements, only those elements within those new elements, i.e. you can style:

article + header + h1 + p { font-weight:bold }

using:

 body > * + * + h1 + p { font-weight:bold }

Comment by Dylan Parry at

I’m probably alone here, but I really don’t understand what you’re trying to say in the section “A LITTLE HEAD IS ALWAYS GOOD”. The link to a further explanation, with examples, left me even more confused than before I’d read it!

Comment by Ken at

“If you create the new element, and don’t use a tag (which is perfectly valid), IE will put all those created elements inside the tag.”

I’ve read this sentence 10 times and I still have no clue what it means. (There’s no tag, but it puts elements in … what?)

Comment by Remy Sharp at

@Dylan + @Ken – damn, some markup has been stripped from the post. It’s missing the word <head> before the word ‘tag’ in both instances. I’m updating the post now to get the code back in.

Comment by Sven at

Actually, & is a bad example for an invalid HTML entity because it is also a XML entity and every parser should understand it (the other XML entities are ", ', < and >). But you could always use the numerical entities, of course.

Comment by pligg.com at

Get HTML5 working in IE…

If your wanting to start using HTML5 and have your site work in IE also, this post will help you learn how to make that happen. Go HTML5!…

Comment by Torrance at

Erm, let me escape my tags. That should have read:

If you create the new element, and don’t use a <BODY> tag (which is perfectly valid), IE will put all those created elements inside the <head> tag. Pretty crazy, but something that’s easily avoided if you always use the <BODY> tag in your markup

Comment by eric at

ie hack, bleh.
I’m just going to make a HUGE red error div at the top of my website for IE viewers telling them basically “gg dipship, you’re using ie, here’s a link to firefox.”

Comment by zcorpan at

& does fly because it’s one of the five predefined entities in XML.

There are also another possible replacement for HTML entities in XML: just straight characters. There’s no need to escape non-ASCII if you’re using UTF-8.

Comment by Remy Sharp at

@Zcorpan – darn, I was going to use &bull; as the example, but I wasn’t sure if people would know what I meant. I’ll change the article to correct it.

Regarding leaving the characters as plain – I couldn’t get this to work – and it threw up the yellow screen of death (this was with http://full-frontal.org which uses UTF-8 encoding). If you have an example of this working, then I’ll update the post. It might be because my page is a polyglot document (works as both XHTML and HTML 5) but I doubt if this is the cause.

Thanks for the catch!

Comment by Adam at

Is there a way to stop IE complaining about the script activex security risk when defining the html5 elements it doens’t know yet?

Comment by Arlen at

@paul – You can create elements inside the htc, but the existence of those elements doesn’t get passed out to the rendering engine without namespacing. So, I thought, why not simply create the new elements for the doc tree *inside* the HTC. Can do that, *and* those new elements can be seen and styled as HTML5 elements by the rendering engine. Success? Not quite. Because the new elements lack content, and since the unknown elements don’t exist in the DOM tree, you can’t copy the content from them into your newly created and styled elements from the HTC.

Bottom Line: If you wrote the content using div class=”html5name” an HTC could walk the tree and replace all the classed divs with the HTML5 elements, including their content, and CSS selectors based on HTML5 elements would find and style them. A lot of work for an IE-only solution, and that forces yu to send it non-html5 content.

Comment by CodeJoust at

If you wish to streamline the creation of the xhtml5 elements consider putting them in a loop.
var el = ['section','date','aside']; for(x=0;x<el.count;x++){document.createElement(el[x]);}

Comment by HTML 5 at

[...] This solution seems like a good one, but it is not perfect, so you need to go a bit further if you want to achieve support in older browsers. The problem is that some browsers (such as Firefox 2, Camino 1, and all versions of Internet Explorer) don’t see the HTML 5 elements as unrecognised; they don’t see them at all! So text marked up with an HTML 5 element can’t be styled at all in these browsers, because the elements don’t exist in their eyes. There is a workaround for this, and it is explained in the HTML 5 doctor article How to get HTML5 working in IE and Firefox 2. [...]

Comment by andrew brundle at

<style>
HEADER, FOOTER, NAV, SECTION, ARTICLE{display:block}
SECTION, .section{width: 1000px}
</style>

<section>
<!–[if IE]><div class="section"><![endif] –>

<!–[if IE]></div><![endif] –>
</section>

Comment by andrew brundle at

I just posted the code snippet above to see how it looks on this blog. It seems okay!

Anyway, this is the sort of code I personally will use. No JS, and it will work in IE.

Messy, sure. But functional.

Comment by pieroxy at

While not exactly on topic, I would suggest that people start using the “ie no more” or the ‘IE awareness initiative” (my website) to try to get rid of the old and unmanageable versions of IE as soon as possible. While pretty intrusive, these initiatives can at least inform people that there are alternatives to using IE6 !

Comment by Lee at

For the Firefox 2 and Camino fix, does this then require all self closing elements to be written in an XHTML style with trailing slashes if it’s being served as XHTML?

Comment by Wiechu at

All the time – all about… IE. You are taking so much care for this app. It must be really good browser.

Comment by stoyv at

PHP Forms – The best form creator & processor solution since 2003
Create any kind of web form in several mouse clicks! Generate a code that can be easily copied and pasted to any web page

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <b> <blockquote cite=""> <cite> <del datetime=""> <em> <i> <q cite=""> <strong>

You can also use <code>, and remember to use &lt; and &gt; for brackets.