How to get HTML5 working in IE and Firefox 2

HTML5 may be the latest and greatest technology, but some browsers don’t have native support for the new semantic elements. Let’s forget for a minute about the even sexier functionality that the elements can bring, such as full control over the <video> element, and just focus on getting the elements to be rendered.

The problematic a-grade browsers are: IE8 and below, and Firefox 2 and Camino 1 (these two browsers use the same rendering engine, which is why they’re both affected).

IE doesn’t style HTML5 elements

IE has it’s own set of issues compared to the Firefox issues which I’ll discuss next. IE quite simply doesn’t see the elements for them to be styled.

This means if you have an <aside> that floats to the right, IE isn’t going to style the element what so ever, because it just doesn’t acknowledge it’s existence.

In fact, this is exactly the same issue that we had before HTML5, where the <abbr> element couldn’t be styled in IE6 – so all manner of work arounds were created (let me add that we’ll also fix the <abbr> element while we fix IE to recognise HTML5).

The fix

There is hope! We can use JavaScript to trigger those elements under IE’s nose so that you’re able to style. This fix works in IE6 and above (the a-grade browsers).

This is achieved by using an HTML5 shiv posted by Sjoerd Visscher.

The fix is to simply create the new element using JavaScript, and voilà, IE is able to style the elements:

document.createElement('header');

For example, say you wanted to style the <time> element to italics, here’s the problem we see:

<!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>

In IE before we apply the fix:

IE without HTML5 style

And now by including the fix:

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

And now after we have the fix – it’s correctly styled:

IE with HTML5 style

One hit solution

For convenience, I wrote a single JavaScript file that can be included to trigger all the HTML5 elements (and the <abbr> element) in to existence for IE.

Download the IE HTML5 enabling script

Include the script in your <head> tag and it will allow you 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 IE8 and below. It’s my hope that IE9 and onwards will support HTML5 elements – but when that day comes, make sure to double check the conditional!

Conditions & Gotchas

JavaScript required

Of course this means that you design now depends on JavaScript. My personal feeling on this, is if you’ve used semantic markup for your site, then when the elements can’t be styled, the content is still completely readable.

Here’s a screenshot of the Full Frontal web site, written using HTML5 elements, with and without JavaScript enabled in IE – you can see the content is still readable because it cascades down correctly (similarly as if CSS was disabled):

IE with and without JavaScript to fix HTML5

A little head is always good

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 <head> and <body> tags in your markup. Leif Halvard explains further with demos

Firefox 2 and Camino rendering bug

Both Firefox 2 and Camino run have a bug in the render engine, specifically the 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 like p, h1, div, and so forth.

According to the W3 Schools stats, Firefox 2 only has around 3% of the market – which may be low enough to justify ignoring.

However, without addressing this issue, a site can look really quite bad in these rendering engines.

The bug is to do with how the Gecko rendering engine doesn’t recognise the new elements. Roughly explained: when Gecko runs in to an unknown element, in removes it’s contents, and puts it next to the element.

For example, the following code:

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

Is rendered as if the markup were:

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

The visual result is similar to IE running without JavaScript – though subtly different, as the DOM tree is actually in a different order to how you intended as an author.

The fix

There’s two approaches to fixing this issue, and so far I’ve only successfully been able to use the non-JavaScript approach.

The JavaScript approach is to cycle through the DOM tree rearranging the elements as the issues are encountered.

Simon Pieters has a small working example of how this can be done (towards the bottom of the page), however in practise I personally found it didn’t work for my markup. It’s definitely solvable using JavaScript, but it still needs work to have something that works for all permutations of markup.

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 make use of mod_rewrite.

XHTML

The first change in the markup you need to include is the <html> tag needs the xmlns attribute – so add this to all your pages:

<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 for our 3% of Firefox users). If the Gecko build is less than 1.9 then we need to serve the content type as 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 xhtml content-type 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 the 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 the first thing that’s printed by your script – i.e. as early as possible in your code.

Gotcha: Yellow Screen of Death

The Yellow Screen of Death is where there’s an XML error on the page. If we’re serving markup as XML and telling the browser to strictly interpret this as XML, if we serve any characters it doesn’t know about, it’s going to bork:

Yellow Screen of Death

There’s a few places where this can be a problem.

Strict markup

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

HTML entities

HTML entities are a no-no. i.e. &bull; isn’t going to fly. You need to use XML entities – which are the numerical representation of these characters.

I built a tool a while back that shows you the HTML entity and the XML value of that entity so you can look them up: HTML entity lookup.

For example: &bull; is 8226, so the XML entity is &#8226;.

User generated content

If your site relies on any user generated content, for example: blog comments, then you need to sanitise the output to ensure there’s no validation issues to trigger the Yellow Screen of Death.

This last issue alone may be justification to investigate the JavaScript solution further.

That all said and done, Firefox has a very good automated upgrade path, and looking at the stats on W3Schools, I think it’s safe to say the market share with this Gecko bug is going to deplete very quickly to nothing.

Further reading

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

52 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?

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.