Moving markup towards HTML5

Hav­ing read John Resig’s “HTML5 Shiv” arti­cle and Remy Sharp’s “HTML5 enabling script”, it felt like the right time to begin the full fledged migra­tion from XHTML to a cross browser com­pat­i­ble HTML5 blog. All in all the process of updat­ing the tem­plates was pain­less, tak­ing about an hour or so to mod­ify the Word­Press Sand­box theme.

To enable IE6 and IE7 sup­port for new HTML5 tags, which are not nat­u­rally styled, some JavaScript is nec­es­sary. As per the ‘shiv’ arti­cle, Remy Sharp has a small script that cre­ates DOM ele­ments, one for each type of new HTML5 tag, the sim­ple act of doing so leads Inter­net Explorer to apply styles to said tags. I slightly mod­i­fied the exist­ing script to add the recently pro­posed hgroup.

Even though these tags accept style they don’t come with their default ren­der­ings. For that we need a bit of CSS to make block ele­ments behave as they should.

(function(){
	if(!/*@cc_on!@*/0) return;
	var e = "abbr,article,aside,audio,bb,canvas,datagrid,datalist,details,dialog,
		eventsource,figure,footer,hgroup,header,mark,menu,meter,nav,output,
		progress,section,time,video".split(','),i=0,length=e.length;
	while(i<length){
		document.createElement(e[i++])
	}
})();
article, aside, dialog, footer, header, section, footer, nav, figure {
	display: block;
}

I’ve also updated the Eric Meyer reset script, remov­ing now dep­re­cated HTML 4 tags and apply­ing reset to the new ele­ments, so they do not unex­pect­edly inherit padding, mar­gin, etc. in the future. These changes are not yet exhaustive.

Mov­ing onto the page’s actual markup, the new DOCTYPE and char­ac­ter encod­ing set­tings are remark­ably sim­ple. Stan­dards based web devel­op­ment is get­ting eas­ier. For browsers that do not sup­port HTML5, the new DOCTYPE still trig­gers stan­dards mode. The xmlns HTML attribute is no longer nec­es­sary and the profile attribute has been dropped.

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
	<meta charset="UTF-8" />
	<title>FofR Online</title>

The header sec­tion has been placed in the appro­pri­ate <header> tags, and sim­i­larly with the footer. I’d hoped to include the ‘About Me’ sec­tion within this, but as part of the spec­i­fi­ca­tion you can­not include head­ings within a <footer> element.

Each of the posts comes wrapped in an <article> tag, i.e. an inde­pen­dent ele­ment with con­tent that could stand­alone. Within are the respec­tive <header> (con­tain­ing title and date) and <footer> (con­tain­ing meta links) ele­ments. Tech­ni­cally the meta links could be marked as <nav>, but the for­mer is more fit­ting and still accept­able use.

The date makes use of HTML5’s <time> ele­ment, with a datetime attribute that gives the pre­cise post­ing time, includ­ing time­zone offset.

The pre­vi­ous and next links that fol­low the arti­cle can com­fort­ably sit within a <nav> tag. Sim­i­larly, my side­bar region is pre­dom­i­nantly nav­i­ga­tion based with lists of archives and cat­e­gories, it’s been marked as such.

<article id="post-67" class="">
	<header>
		<h2 class="entry-title"><a href="" title="" rel="bookmark">POST TITLE</a></h2>
		<div class="entry-date">
			<time datetime="2009-04-30T15:54:28-07:00" class="published" title="2009-04-30T15:54:28-07:00">April 30, 2009 &#8211; 3:54 pm</time>
		</div>
	</header>
	<div class="entry-content">
		POST
	</div>
	<footer class="entry-meta">
		META LINKS
	</footer>
</article>

<nav id="nav-below" class="navigation clearfix">
	<div class="nav-previous"></div>
	<div class="nav-next"></div>
</nav>

One avenue I should explore is the inclu­sion of the <section> tag, which I’d like to break up indi­vid­ual posts, prob­a­bly by split­ting the con­tent at level three head­ings down­wards; thereby becom­ing the header of each new section.

It’ll be a while before the real ben­e­fits of HTML5 can be fully appre­ci­ated by every­one, but it feels good to make a start, how­ever small that step may be.

Paul Hayes

Paul Hayes is a developer at Last.fm. You should follow him on Twitter, where he talks about UX, HTML, CSS and JavaScript, amongst other cool stuff.