An Introduction to HTML5
Join the DZone community and get the full member experience.Join For Free
it seems like everyone is talking about html5 now, but the discussion is spread out and seldom gives the background, explanation what html5 really is and if/when it’s usable.
therefore, my ambition here is to:
- give you a little history
- go into what html5 is and what it covers
- show code examples
- target the question whether you can start using it or not
the html5 work stems from the whatwg (web hypertext application technology working group), and their focus is the development of html and apis for web applications. the reason it came to life back in 2004, founded by people from apple, mozilla and opera, was a worry about the direction w3c were taking with xhtml, and no focus on html or the real-life needs for web developers.
it got really interesting in july of 2009, when w3c announced that xhtml2 would be cancelled in favor of html. this means that the only future development of html and xhtml is in the form of html5 – html5 is the future, whatever you think about it.
i should also mention right away that html5 is spelled just that, with no space between the the l and 5 – read more in spelling html5 .
what is html5
so what is html5, really? basically, it’s about extending html/xhtml with new more semantically rich elements, deprecating attributes, introducing new attributes and altering how some element and attributes are allowed to be used. hand in hand with that is the possibility to use attributes for wai-aria to make a web page more accessible, such as with landmark roles .
it also introduces a number of apis for making it easier to create web applications :
- 2d drawing api with the canvas element.
- api for playing of video and audio with the video and audio elements.
- api that enables offline web applications.
- api that allows a web application to register itself for certain protocols or media types.
- editing api in combination with a new global contenteditable attribute.
- drag & drop api in combination with a draggable attribute.
- api that exposes the history and allows pages to add to it to prevent breaking the back button.
- cross-document messaging with postmessage.
other things that initially was in the specification, but broken out into separate specifications are:
- api for geolocation
- web storage.
- web workers.
i think the best document to read to grasp the changes are html5 differences from html4 , which will cover:
- open issues
- backwards compatible
- development model
- impact on web architecture
- character encoding
- the doctype
- mathml and svg
- new elements
- new attributes
- changed elements
- changed attributes
- absent elements
- absent attributes
- extensions to htmldocument
- extensions to htmlelement
backwards compatibility & progressive enhancement
i think one of the main reasons behind the adoption of html5 is that it sets out to be backwards compatible and work with the web browsers there already are in the market, and have been for some time, too. this is done through new elements which, generally, have no particular look or behavior attached to them, but rather offering more semantic richness and then up to you style them via css according to your liking.
the other parts are new elements that have been implemented in some web browsers, and not in others, where the progressive enhancement approach that has been preached for a long time comes into play. meaning, use the new elements which will give a richer experience in some web browsers, whereas it will fall back to default content in others.
example in question: a cascade of new input element types:
so far, webkit and opera have been the most busy rendering engine to implement some of these, especially in relation with opera’s work with webforms 2.0 support, but in other web browsers it will fall back to a regular input type="text". that means, for instance, that input type="search" will will give subtle, but better user experience, in safari and google chrome, but will be a fully functional for all others.
the beauty of progressive enhancement.
html5 code introduction
let’s get down to some code introduction and what you need to be aware of when you start coding html5.
the doctype for html5 looks like this:
no versioning, no redundant information. all progressive enhancement on a feature-level, as opposed to complete releases that need to be implemented.
and, to be a bit more pragmatic than before, html5 will allow both quick-closing of empty tags (such as input, link etc), but you can use those elements just as well without quick-closing them. quotes for attributes are also optional, and you know what? you can even use upper-case letters for your element names of you will!
all of these examples are valid html5:
<p id=john>john's p</p>
<input type="text" />
this might be scary to some people, and i agree with the concerns the html5 super friends presented, where they want a way to, in a more stricter way, be able to validate the consistency of the code. however, with all different people’s coding styles and our legacy, allowing this freedom was probably the only pragmatic option to get people along, instead of fighting battles that will never be won.
and, a great thing is that you can now wrap entire blocks with an a element to make that entire block a link to somewhere!
html or xhtml
i should also mention that there are two options to serve html5 content: as html or xhtml. the somewhat confusingly named xhtml5 differs a bit from html5, though:
- no doctype is needed, just an xml prolog.
- it must have a namespace: <html xmlns="http://www.w3.org/1999/xhtml">
- it must be served with either of these mime types: application/xhtml+xml or application/xml.
- the noscript element can not be used.
lot’s of the research behind html5 has been how people name their elements, with ids and classes, and part of the result are new elements named/inspired by those. there are a lot of new elements in html5 , where these are probably the most interesting as you can use them right away in any web browser.
- mark up parts of the content that is independent, for instance blog post, article etc.
- used to mark up relevant additional information, like a sidebar.
- used for natively including audio in a web page.
- the counter-part to header; could be used for any footer section per context.
- used for headers in its context. note: not just for the header of a page, but also for each header part in section, article and similar.
- used for grouping several headers, for instance, a main heading and a sub-heading.
- used for marking up main navigation.
- mark up a generic document section. easily confused with article, and on top of that you nest either of them, in any order, with the other.
- used to mark up a time or date.
- used for natively including video in a web page – lots of interesting work is coming along here in terms of web browser support.
let’s get down to some actual code and see what an html5-coded web page could look like. i have put together a simple html5 example with new elements and wai-aria landmark roles as part of my html5 demos page (quite sparse now, i know, but i will incrementally add examples to it).
this is the complete source code of that web page:
<title>html5 example with new elements and wai-aria landmark roles</title>
<link rel="stylesheet" href="css/base.css" type="text/css" media="screen">
<!--[if lte ie 8]>
<h1>html5 example with new elements and wai-aria landmark roles</h1>
<p>this page has valid simple html5 markup complemented with wai-aria landmark roles for accessibility</p>
<li><a href="http://robertnyman.com/html5">html5 demos and samples' start page</a></li>
<li><a href="http://robertnyman.com/">robert's talk</a></li>
<div id="demo-main" role="main">
<h3>subtitle to the above title</h3>
<p>some content, created <time datetime="2009-10-14">october 14th 2009</time></p>
<p>some more content - i guess you get the drift by now</p>
<h2>the html code for this page</h2>
<aside id="demo-aside-content" role="complementary">
this is just a demo page to see html5 markup and wai-aria landmark roles in action in a simple context
<footer id="page-footer" role="contentinfo">
created by <a href="http://robertnyman.com/">robert nyman</a>
as you can see, it looks more or less than every other html web page you have ever seen, but with just a few new html elements being used and the role attribute with landmark roles for wai-aria to make it more accessible.
if you want a more real-world example, just view the source code of robertnyman.com : i have now rewritten it as html5!
can i use it today?
i would say yes!
there was some wild speculation about the date 2022, because one of the main men behind the work with html5 and the specification was asked in in an interview about html5 and its timeline, and the date 2022 was mentioned. then everyone assumed that html5 wouldn’t be ready until 2022, which is not was addressed, but rather when web browsers would have two releases with completely full support for html5 (compare that to css 2.1 support etc and the time it has taken, and you will probably get a more sane reaction). read more about in 2022, or when will html 5 be ready? if you want to know more.
but let’s go through the pros and cons, and i will explain why html5 is ready to be used.
- major players using it
- to begin with, a lot of major players have already started implementing it. google.com already have the html5 doctype (although they should really improve the markup accordingly), the new hyped google wave is revolving a lot around html5 and related apis (which ironically uses a html4 strict doctype…) and there is also a youtube html5 demo page .
- strict rendering
- the html5 doctype will trigger the strictest rendering in all web browsers. no almost standards mode, no quirks; strict all the way, which is the way we want, and the way it should be.
- progressive enhancement
- you can start today just by changing the doctype. then gradually move onto exchanging some structure elements, sprinkle it with some wai-aria etc and before you know it, you have a fantastic html5 page!
- validation available
- now even the w3c validator supports html5. just use the firefox web developer extension to validate your html against it. the html validator extension unfortunately doesn’t support it yet, but i know there are at least plans to include this – if you have any ides or input, please help him out.
- it probably doesn’t matter much right now, but in the future, i think that web sites that are marked up in a much more rich fashion with new html5 elements to give them the multitude they need, will benefit a lot from this.
ah, right, drawbacks. there are always some, isn’t there?
it probably doesn’t come as a shock to you, but internet explorer, all current versions (yes, including ie8 as well) has a little issue: it won’t apply any css to an unknown element (e.g. nav, section etc).
the gist of it is that you need to call document.createelement with the name of each new html5 element you use in the page. you don’t need to use it to append or place those element, just call it to make ie aware of them. remy sharp has written a little html5 enabling script for internet explorer, version 6 to 8 which works in all possible cases.
people eager for html5 has argued that we will need script dependency for ie to render styling for those elements, otherwise it will render unstyled, but with the content still fully accessible. while i agree in theory, i think it is, at times, an unnecessary requirement for using html5.
another option is the content negotiation approach, where we on the server exchange the new elements for old ones just for internet explorer. it’s simple: just have a regular expression to replace all block elements (header, footer, section etc) with div elements, and the inline ones (time, mark etc) with span elements.
together with that, always style you elements through their id attribute or class value, and you are good to go! all modern web browsers + screen readers + search engines will get rich html5 markup; ie will get plain old html.
there are still some discussions about some elements in html5 and how they should be specified, what existing options/combinations there are that will work in existing user agents (which is a must; it can’t break things). overall, though, i’d say use the new parts everyone agrees about and that will work, and hold on with the bells and whistles until it’s ready.
the last call for the html5 working draft is in october this year. the future is already here.
where we are today on the web
i for one is really happy that this is finally becoming true and usable. sure, as always there are a number of issues and things to discuss, but overall we really need this as web developers. we need new elements, apis and options to create dazzling web sites, and we have been waiting so long for something new at all.
i also think this is vital for the climate of the web, to have open standards to code after, and to compete with the ever-growing closed-in and proprietary technologies that try to claim the web as their own.
have me do a presentation
if you found all this to be immensely interesting, and would like to discuss it in real life or hear more, please contact me if you want me to talk at a conference, your company or similar!
Opinions expressed by DZone contributors are their own.