New Structural Elements in HTML5
- Beginner Recipe: Building an HTML5 Starter Document
- Where Do All the New Elements Come From?
- Beginner Recipe: Using the header Element to Create a Site Header
- Beginner Recipe: Using the hgroup Element to Group Headings
- Beginner Recipe: Creating Navigation with the nav Element
- Intermediate Recipe: Using the New article Element
- Intermediate Recipe: Grouping Content with the section Element
- Beginner Recipe: Creating a Sidebar with the aside Element
- Beginner Recipe: Using the footer Element
- Intermediate Recipe: Using the HTML5 Outliner to Ensure the Correct Structure
- Advanced Recipe: Using All the New Elements to Build a News Page
- Advanced Recipe: Using All the New Elements to Build a Search Results Page
- Summary
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?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
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 a doctype 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.
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:
<link rel="stylesheet" type=text/css href=my-css-file.css >
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.
Listing 1.1. A Simple HTML5 Starting Page
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>page title</title> <script src="my-javascript-file.js"></script> <link rel="stylesheet" href="my-css-file.css" /> </head> <body> <!-- new HTML5 elements are going to go here :) --> </body>
That is it! Save the page as an .htm (or .html) file, and now you can start filling the page with great content.