Practical HTML 5

HTML 5 is currently a W3C Editor's Draft and will be the next major revision of HTML. It introduces tons of new browser features, practical new parsing rules and flexible error handling. Browser makers are heavily investing in this specification and have already started to implement HTML 5's features in their latest browsers [1][2].

Using HTML 5 today

The great thing about HTML 5 is that you can already start using it today and get used to the new syntax and functionality, because it's DOCTYPE is already supported cross-browser and - besides a few small differences - it is backwards compatible with HTML 4. It even standardizes existing proprietary features.

Unfortunately some browsers have issues interpreting the new HTML 5 elements. In Internet Explorer you cannot style HTML 5 elements, while Firefox 2 (and other browsers based on the same engine) contains a bug that automatically closes any unknown element. To solve these issues there are already a few workarounds available, however these either rely on JavaScript or a specific server-side configuration.

Personally, I have decided to start using the HTML 5 DOCTYPE for my personal projects, however I'm not particularly fond of using these workarounds. Relying on HTML 5's backwards compatibility instead, I basically mark up my new projects as if I was using HTML 4, with the difference that I:

  • use the HTML 5 DOCTYPE and MIME type definition
  • keep HTML 5's element names in mind when I name my id and class attribute values
  • insert HTML 5's new semantic elements inside this markup afterwards

The disadvantage is obvious: redundant markup that may force me to refactor my HTML templates and CSS files in a few years time. Still, this approach feels more natural to me, because it allows me to use HTML 5 in a fully functional and clean way today, while I postpone a part of the transition to the future (the part where things are not properly supported cross-brower yet).

Furthermore, - as a personal coding preference - I still use lowercase element names and quotes around all my attribute values, for better readability. I also explicitly use the optional head and body elements, for readability and because JavaScript libraries often expect that these elements are available inside your markup.

An example

Phase 1 (today)

Author a Web page with HTML4, like this HTML 4 example, e.g. with the following mark-up for the header:

<div id="header">
	<h1>HTML 4 example</h1>
</div>	

And apply CSS as you use it today:

#header { background-color:#eee; }

As a next step replace the HTML 4 DOCTYPE and MIME type definition with HTML 5 syntax and add semantic HTML 5 elements to your document, like in this updated practical HTML 5 example:

<div id="header">
    <header>
	    <h1>Practical HTML 5 example</h1>
    </header>
</div>

Note that you don't have to update your style sheets, because you've only added semantic elements. You basically postpone it's styling to phase 2 of your HTML 5 transition, when no ugly workarounds are needed anymore.

Phase 2 (somewhere in the future)

Refactor your HTML templates by removing the redundant markup like in this updated HTML 5 example:

<header>
	<h1>HTML 5 example</h1>
</header>	

And refactor your CSS files:

header { display:block; background-color:#eee; }

In any case, this is just my current train of thoughts. Please let me know what you think of this approach and why you think it is either good or bad.

Comments

Comment 1 by Dave
03 Jul 2009, 14:38
I had started writing an article very similar to this one Bobby but I have binned it as you have basically covered everything. I have used the exact thinking you employ of a 2 phased development on my own site. I must admit that your implementation of HTML semantic markup is alot better than my own, but at the time I was tortured with CSS styling and invalid markup errors, so i cut it back to the bare ( I mean bare) minimum. You have inspired me to revsit this though,as you have adopted the same approach of backward compatable HTML5. I had serious issues with my CSS in IE6 even though i did adopt the Javascript workaround! Any ideas were i may have gone wrong?
Comment 2 by Bobby
03 Jul 2009, 15:53
Good to hear you like it! There should be no difference between an HTML4 and practical HTML5 version in IE6. Maybe a bug you've overlooked?
Comment 3 by fritzthecat
03 Jul 2009, 17:36
I can't decide whether it's a lot of effort for nothing or whether it is a good way to get used to HTML 5... probably the former.

Interesting nevertheless.
Comment 4 by Bobby
04 Jul 2009, 16:21
The effort is merely using one workaround or the other:

A. Using a JavaScript based workaround to make things work in IE, adding a noscript style sheet for people with IE and JavaScript switched off, using a server-side config/JavaScript workaround for Firefox 2 , or:

B. Avoiding to style the new HTML5 elements. This uses redundant markup, however how bad is this really? It also makes your website more flexible for a possible future restyle, so you have less chance that you would have to update your markup.
Comment 5 by Darren
05 Jul 2009, 20:14
I've been putting off researching this html 5 stuff for a while, think I'll be gettin on top of it, cause it seems like it's going to be pretty cool. The markup is going to make sense like rails, which makes me :)
Comment 6 by Cyril Gupta
03 Aug 2009, 06:59
I am not worried about HTML5 right now cause it won't be out of the bin till the next year anyway. Let the major browsers support it, then I will worry about catching up speed on it. Right now I am pretty content not having to learn another new thing.
Comment 7 by mark rushworth
04 Aug 2009, 08:48
I read somewhere that theres some javascript you can use to set html5 objects up to work in ie6 etc!

... found it

http://html5doctor.com/how-to-get-html5-working-in-ie-and-firefox-2/