No doubt you’ve heard of HTML5; but you probably haven’t seen a clear, accurate explanation of what it is, what it contains and what it can do. The term HTML5 is widely misused as an umbrella term for all sorts of technologies, some of which have no connection to the standard at all. If this has left you a little confused, it’s understandable.
In the following article, we’ll give an introduction to HTML5, showing you what it consists of and what you can do with it, and we’ll create a simple web page using HTML5 mark-up. We’ll also look at other technologies mistakenly referred to as “HTML5 technologies”.
WHAT IS HTML5?
Unlike previous versions of HTML, HTML5 wasn’t created by the World Wide Web Consortium (the W3C). Its origins lie with the independent Web Hypertext Application Technologies Working Group (WHATWG), formed in 2004 by delegates from Apple, Mozilla and Opera who disagreed with the W3C’s vision of XHTML as the future of the web. The WHATWG created proposals to develop the web as a platform for applications – and within a few years the W3C adopted these proposals. The two organisations now develop and maintain HTML5 together.
It’s worth noting that each organisation has its own version: although the two are very similar, the WHATWG’s is a “living document” that’s regularly tweaked, while the W3C’s is maintained through more formal processes.
NEW ELEMENTS IN HTML5
Many of the updates HTML5 brings are intended to make life easier for developers. A case in point is the HTML5 use of DOCTYPE. Prior to HTML5, the syntax you had to use to tell the browser what sort of HTML you were using was unfriendly, to say the least; for example, the first line of an HTML 4.01 page might be:
<!DOCTYPE HTML PUBLIC “-//W3C/
To specify HTML5, you simply need to define:
HTML5 also includes a new “charset” element that defines the character encoding of your web page – a useful feature on the increasingly multilingual web. For example, to define the default character encoding of your page as UTF-8, you can simply place the following line within the head element:
NEW SEMANTIC ELEMENTS
HTML5 also focuses on making code easier to understand, navigate and customise. To this end, it includes several new semantic elements. Since many web pages have headers and footers, the HTML5 specification includes new <header> and <footer> elements specifically for enclosing content intended for these roles. This makes it easy to customise styles, and parse and process pages containing headers and footers.
Similarly, a common feature of web pages is a list of navigation links. Again, the HTML5 specification defines a new <nav> element to enclose lists of navigation links.
When it comes to the main content of a web page, the <article> and <section> elements can be used to enclose content and give it a particular semantic meaning. To get an idea of how these elements can be used, consider a newspaper with several sections, such as news, sport and entertainment. Each section contains a number of articles that are related by the section heading, but each of which can also stand on its own.
One of the most important – and most talked about – things that HTML5 allows you to do is build multimedia directly into your web pages. Previously, in order to add an audio or video file to a web page, you had to use an <object> or <embed> element to call a third-party plugin such as Adobe Flash to play the file. This required that the plugin in question was installed and working on the end user’s machine – something that can’t be taken for granted.
HTML5 introduces two new elements – <audio> and <video> – that you can use to play multimedia files natively in the user’s web browser. These elements can take a number of attributes that allow you to do such things as instruct the browser to use its default control set, to loop the audio/video, and to play the media automatically.
All the major browsers can handle these new media elements, but there are many media formats out there, and not every platform supports all of them. The HTML5 specification initially specified that the Ogg Vorbis audio and Ogg Theora video formats must be supported by all browsers, but this was dropped after objections by some browser developers. As a result, if you want to ensure your media files will work in multiple browsers, you must provide your audio and video files in multiple formats.
This isn’t as troublesome as it may sound. For audio, Firefox supports Ogg Vorbis and WAV; Safari supports MP3, AAC, WAV and MP4; Opera supports Ogg Vorbis and WAV; and Internet Explorer 9 supports MP3, AAC and MP4. To make HTML5 audio playable in all the major browsers, you need only provide your files in both Ogg Vorbis and MP3 formats.
It’s a similar situation with video. Firefox supports Ogg Theora and the royalty-free WebM format (from version 4+); Safari supports MP4/H.264; Opera supports WebM; and Internet Explorer 9 supports MP4/H.264. So again, you need only provide video files in two formats – WebM and MP4/H.264 – for support across all modern HTML5-capable browsers.
Since you need to provide multiple sources for your multimedia files, HTML5 provides a new element, <source>, which is used to specify a source for an embedded audio or video file. A section of code that embeds video in two different formats might read:
The Media API lets you create and style custom media player controls via standard HTML and CSS, and hook them up to the audio and video, to work in the same way as the browser’s default control set. For a detailed look at HTML5 multimedia, its capabilities and the Media API in particular, check out my book HTML5 Multimedia: Develop and Design, published by Peachpit.
Forms are a fundamental website element. Whether they’re used for contacting someone, providing feedback or ordering products, forms (and the input fields they contain) are key to web development. HTML5 hasn’t neglected forms by any means: it brings new features and enhancements that make form creation easier and more useful.
The HTML5 specification defines a number of new input types, in addition to the familiar text input field. Fields can now be given specific input types, with options including an email address, a telephone number, a date, a URL, a time, a number and even a colour. Most browsers currently do nothing special with these input types, but in the future it will be possible to handle these fields in different ways – for example, by offering a mini-calendar for fields whose input type is set to date (Opera already does this), or a colour selector for the colour input type.
New attributes also allow you to mark an input field as required; to fill with placeholder text; or even to specify a data type or regular expression pattern that the input field’s content must match. This makes it possible to perform in-browser input validation, where invalid input is instantly rejected, with an appropriate error message, before the form is submitted. This will greatly help developers, who currently have to create their own routines to validate user input.
BEYOND COOKIES: LOCAL AND SESSION STORAGE
In older versions of HTML, the normal way for a website to “remember” details about a user is by storing a cookie – a small packet of data – on the local PC, which can then be read back when the user returns to the site. For security reasons, some cookie values last only until the user logs out or closes the browser. Others are intended to persist every time a user visits a particular website; but these must still specify an expiry date.
It’s also possible to store more data through these APIs than was possible with cookies. A traditional HTML site is limited to storing 20 cookies of 4KB each in the user’s browser. With localStorage and sessionStorage, 5MB is available, so large amounts of data – such as email or calendar archives – can be stored locally, for local processing or offline reference.
This ties in to another new feature of HTML5 – offline applications. It’s possible for developers to define an offline cache of a site for the browser to store and manage. This way, web apps can remain available even when there’s no network connection – such as when you’re on a plane or an underground train.
DRAG AND DROP
Back in 1999 (believe it or not), Microsoft added drag-and-drop capabilities to Internet Explorer 5, so that a web page could react when one element was dragged over another. Apple subsequently added this API to Safari, and now it’s been incorporated into the HTML5 specification. Almost all HTML5-capable browsers support the drag-and-drop API, the notable exception being Opera.
WHAT ISN’T IN HTML5
CSS3 – the latest version of the Cascading Style Sheets specification – makes it easy to implement a wide range of visual styles and layouts for web page elements. It also includes the @font-face construct, which allows web designers to embed custom fonts in pages, to create striking and distinctive typographic layouts. Again, it isn’t part of HTML5, but a separate standard.
A final idea that’s sometimes conflated with HTML5 is “responsive web design” – a term coined by Ethan Marcotte to describe how a web design can be made to respond to users’ device size and capabilities. A site following this principle might indeed be implemented using HTML5; but that’s as close as the ideas get.
NEXT PAGE: HTML and Internet Explorer, Where to go from here, Further reading...