This excerpt is from the book HTML5 Developer's Cookbook, authored By Chuck Hudson and Tom Leadbetter. Published Dec 6, 2011 by Addison-Wesley Professional. Part of theDeveloper's Library series. Copyright 2012, ISBN-10: 0-321-76938-4. For more information, please visit the publisher site http://www.informit.com/store/product.aspx?isbn=0321769384 .

HTML5 is not just about interactive voodoo with JavaScript APIs and video coolness. There are more than 20 new elements you can use to author your web pages, adding semantics to deliver more accessible, reusable content.

In later chapters, you will learn about new HTML5 form controls and multimedia elements. In this chapter, you will learn about the new structural elements of header, hgroup, nav, footer, article, section, and aside, focusing on how, why, and when to use these new elements, both on their own and when combined. Essentially, you will be building a basic website template with the new elements, as shown in Figure 1.1.

Figure 1.1 Basic page structure with new HTML5 elements

Beginner Recipe: Building an HTML5 Starter Document

You are about to go HTML5, so let's go to the top of the HTML document. Although the content in this immediate section does not contain new elements, there is a new way to write them, so it is best to be aware before we start getting into the body.

doctype

Does this look familiar?

The doctype should be the very first line in an HTML document. Called a Document Type Definition (DTD), the doctype is a web standards requirement, and it tells the browser how to process the document, which is why it must be the first thing in your HTML document. If you didn't use adoctype or you put any other code before the doctype, then the browser would be in quirks mode, and chances are the code you have written will not work properly in some browsers.

It's unlikely that you would want to memorize the previous doctype. Why would you? It's horrible and clunky. In HTML5, you now have a nice, easy-to-remember doctype:

<!DOCTYPE html>

Honestly, that's all it is. This is all you need to tell the browser you are in standards mode. If a browser does not implement HTML5, the page will still work. If you used <!doctype html5>, it would trigger quirks mode as well. This doctype has been chosen so it will always work in browsers, no matter what the latest version of the language is.

NOTE

If you refer to http://infomesh.net/html/history/early/, you can see the earliest HTML document, from November 13, 1990. The markup is really simple, and its simplicity reminds us of the HTML5 doctype. In fact, if you added the new doctype to that page, it would validate!

Character Encoding

The first line you need inside the head is the charset declaration, which tells the browser how the file should be interpreted; in this case, you want to send it an HTML document.

In HTML 4, it looks like this:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

But like the doctype, in HTML5 it is now much simpler:

<meta charset="utf-8" />

Easy! Remember, you need this and the doctype on your page.

JavaScript and CSS Links

We can breeze through this little section as well. HTML5 helps you reduce lots of markup from your page, and you can simplify the calls to JavaScript (and other client-side scripting file) and CSS. In HTML4, the script and link elements needed a type attribute, as follows:

<script type="text/javascript" src="my-javascript-file.js"></script>
<link rel="stylesheet" type="text/css" href="my-css-file.css" />

But in HTML5, those lines now look like this:

<script src="my-javascript-file.js"></script>
<link rel="stylesheet" href="my-css-file.css" />

You may be wondering why you can now get away with doing this. Well, one of the intentions of HTML5 is to make things more sensible when you are coding. So, if you are linking to a script, the browser assumes it is a JavaScript file, and if you are using rel=stylesheet, it can only mean you are linking to a CSS file. And don't worry, not using the type attribute causes no issues in older browsers.

Syntax Writing Style

In HTML5, using the previous code examples, you can code the page in slightly various ways.

You can code in uppercase:

<SCRIPT SRC="MY-JAVASCRIPT-FILE"></SCRIPT>

You can code with no quotation marks:

<script src=my-javascript-file></script>

You can skip a closing slash:

Or you can use a combination!

<LiNK rel="stylesheet" tYPe="text/css" href=my-css-file.css />

All these are fine to use; however, it is strongly encouraged that you pick a style and stay with it. This is useful not only to yourself but for other developers who may at some point have to use your code. The syntax style will be consistent. We come from XHTML backgrounds, so we will close all tags, use lowercase, and use quotation marks around attributes.

Bringing all the previous together gives you the HTML5 starting page in Listing 1.1.

That is it! Save the page as an .htm (or .html) file, and now you can start filling the page with great content.

TIP

Validation is a very useful tool for checking why things might be broken, and it is a great step to have in your development process. However, with HTML5 still developing, there are no official validator services. The W3C validator, http://validator.w3.org, will check for HTML5 conformance but does warn that is an experimental feature. Another validator to test your pages against is http://html5.validator.nu. It is worth testing your pages in both of these validators.

To read the entire chapter click here: http://www.informit.com/articles/article.aspx?p=1807656 .