O’Reilly Ebooks—Your bookshelf on your devices!

When you buy an ebook through oreilly.com, you get lifetime access to the book, and whenever possible we provide it to you in four, DRM-free file formats—PDF, .epub, Kindle-compatible .mobi, and Android .apk ebook—that you can use on the devices of your choice. Our ebook files are fully searchable and you can cut-and-paste and print them. We also alert you when we’ve updated the files with corrections and additions.

Learn more at http://oreilly.com/ebooks/ You can also purchase O’Reilly ebooks through iTunes, the Android Marketplace, and Amazon.com. HTML5: The Missing Manual by Matthew MacDonald

Copyright © 2011 Matthew MacDonald. All rights reserved. Printed in the Unites States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly Media books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles: http://my.safaribooksonline.com. For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Printing History: August 2011: First Edition.

Nutshell Handbook, the Nutshell Handbook logo, the O’Reilly logo, and “The book that should have been in the box” are registered trademarks of O’Reilly Media, Inc. HTML5: The Missing Manual, The Missing Manual logo, Pogue Press, and the Pogue Press logo are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.

ISBN: 978-1-449-30239-9 [M] Table of Contents

The Missing Credits...... xi Introduction...... 1

Part One: Meet the New Language Chapter 1: Introducing HTML5...... 11 The Story of HTML5 ...... 11 XHTML 1.0: Getting Strict...... 12 XHTML 2: The Unexpected Failure...... 12 HTML5: Back From the Dead...... 13 HTML: The Living Language...... 14 Three Key Principles of HTML5...... 15 1. Don’t Break the Web...... 15 2. Pave the Cowpaths...... 17 3. Be Practical...... 17 Your First Look at HTML5 Markup...... 18 The HTML5 Doctype...... 20 Character Encoding...... 21 The Language...... 21 Adding a Style Sheet...... 22 Adding JavaScript...... 22 The Final Product...... 23 A Closer Look at HTML5 Syntax...... 24 The Loosened Rules...... 24 HTML5 Validation...... 25 The Return of XHTML...... 27

iii HTML5’s Element Family...... 29 Added Elements...... 29 Removed Elements...... 30 Adapted Elements...... 30 Tweaked Elements...... 32 Standardized Elements...... 33 Using HTML5 Today...... 34 Evaluating Browser Support...... 34 Browser Adoption Statistics...... 36 Feature Detection with Modernizr...... 38 Feature “Filling” with Polyfills...... 40

Chapter 2: A New Way to Structure Pages...... 43 Introducing the Semantic Elements...... 44 Retrofitting a Traditional HTML Page...... 45 Page Structure the Old Way...... 47 Page Structure with HTML5...... 49 Subtitles with

...... 52 Adding a Figure with
...... 53 Adding a Sidebar with