HTML5 Cookbook - Kimbersoft

Transcription

HTML5 Cookbook

HTML5 CookbookChristopher Schmitt and Kyle SimpsonBeijing Cambridge Farnham Köln Sebastopol Tokyo

HTML5 Cookbookby Christopher Schmitt and Kyle SimpsonPublished by O'Reilly Media, Inc., , , .Printing History:ISBN: 978-1-449-39679-41315931630

Table of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix1. Fundamental Syntax & Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1IntroductionSpecifying the DOCTYPEProblemSolutionDiscussionSee AlsoSpecifying the Character SetProblemSolutionDiscussionSee AlsoSpecifying the LanguageProblemSolutionDiscussionSee AlsoOptimizing scripts and linksProblemSolutionDiscussionSee AlsoAdding Document Structure With HTML5’s New ElementsProblemSolutionDiscussionSee AlsoChoosing Between article and 9v

DiscussionSee AlsoChecking Your Document OutlineProblemSolutionDiscussionSee AlsoModifying the Document OutlineProblemSolutionDiscussionSee AlsoEmphasizing TextProblemSolutionDiscussionSee AlsoAdding Importance to TextProblemSolutionDiscussionSee AlsoHighlighting Text for ReferenceProblemSolutionDiscussionSee AlsoMarking Up Small PrintProblemSolutionDiscussionSee AlsoDefining Acronyms & AbbreviationsProblemSolutionDiscussionSee AlsoAdding Links to Block-Level ContentProblemSolutionDiscussionSee AlsoMarking Up Figures and Captionsvi Table of 16171717171717171818181818181919191919202020

ProblemSolutionDiscussionSee AlsoMarking Up Dates and TimesProblemSolutionDiscussionWhat About Microformats?Publication DatesSee AlsoSetting the Stage for Native Expanding and CollapsingProblemSolutionDiscussionSee AlsoControlling the Numbering of Your ListsProblemSolutionDiscussionSee AlsoHide Content to Show LaterProblemSolutionDiscussionSee AlsoMaking Portions of a Page EditableProblemSolutionDiscussionSee AlsoSet the Stage for Native Drag and DropProblemSolutionDiscussionSee 262627272727272828282828292. Progressive Markup & Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31IntroductionAdding More Semantic MeaningProblemSolutionDiscussion3131313232Table of Contents vii

See AlsoPicking a Markup StyleProblemSolutionDiscussionSee AlsoUnderstanding Browser Support for HTML5ProblemSolutionDiscussionSee AlsoMaking Internet Explorer Recognize HTML5 ElementsProblemSolutionDiscussionSee AlsoDetecting HTML5 Features with JavaScriptProblemSolutionDiscussionSee AlsoUsing HTML5 BoilerplateProblemSolutionDiscussionSee AlsoValidating HTML5ProblemSolutionDiscussionSee AlsoMapping HTML5 Elements to id and class NamesProblemSolutionDiscussionSee 424445454545474748484850513. Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53IntroductionDisplaying a Search Input FieldProblemSolutionDiscussionviii Table of Contents5353535354

See AlsoContact Information Input FieldsProblemSolutionDiscussionSee AlsoUtilizing Date and Time Input FieldsProblemSolutionDiscussionSee AlsoNumber InputsProblemSolutionDiscussionSee AlsoSelecting From a Range of NumbersProblemSolutionDiscussionSee AlsoSelecting ColorsProblemSolutionDiscussionSee AlsoCreating an Editable Drop-downProblemSolutionDiscussionSee AlsoRequiring a Form FieldProblemSolutionDiscussionSee AlsoAuto-focusing a Form FieldProblemSolutionDiscussionSee AlsoDisplaying Placeholder le of Contents ix

SolutionDiscussionSee AlsoDisabling AutocompleteProblemSolutionDiscussionSee AlsoRestricting ValuesProblemSolutionDiscussionSee AlsoMaking HTML5 Work in Older BrowsersProblemSolutionDiscussionSee AlsoValidating Form Data in Older Browsers with JavaScriptProblemSolutionDiscussionExample: Sample 57577777777808081818182828282834. Native Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85IntroductionAdding HTML5 AudioProblemSolutionDiscussionSee AlsoManipulating the Audio StreamProblemSolutionDiscussionSee AlsoGenerating audio Using JavaScriptProblemSolutionDiscussionx Table of Contents858585858890909090919292929393

See AlsoVisualizing audio Using canvasProblemSolutionDiscussionSee AlsoSample Design: Custom Audio PlayerDefine audio and player structureJavaScript APICSS for StyleSee Also939393939696969697991005. Native Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103IntroductionAdding HTML5 VideoProblemSolutionDiscussionSee AlsoEnsuring Multi-Browser Video SupportProblemSolutionDiscussionSee AlsoSetting Video DimensionsProblemSolutionDiscussionSee AlsoDisplaying a Placeholder Image Before Video PlaysProblemSolutionDiscussionSee AlsoMaking Video LoopProblemSolutionDiscussionSee AlsoSample Design: Manipulating Video With canvasAdd video and canvasGenerate PreviewSet Preview Table of Contents xi

Add an Event ListenerAnd close out the function:Draw PreviewsThe Final ProductSee Also1131141141141156. Microdata and Custom Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117IntroductionWhen to use microdata versus custom dataAdding Microdata to MarkupProblemSolutionDiscussionSee AlsoUsing Microdata and Schema.orgProblemSolutionDiscussionSee AlsoAdding Custom Data to MarkupProblemSolutionDiscussionSee AlsoAccessing Custom Data with JavaScriptProblemSolutionDiscussionSee AlsoManipulating Custom DataProblemSolutionDiscussionSee AlsoExample: Creating a Map Application Using Custom DataProblemSolutionDiscussionSee 7. Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129IntroductionThe Purpose of Accessibilityxii Table of Contents129129

Accessibility GuidelinesWriting Appropriate ALT Text DescriptionsProblemSolutionDiscussionSee AlsoIdentifying Abbreviations and AcronymsProblemSolutionDiscussionSee AlsoIdentifying Sections of a Page Using ARIA Landmark RolesProblemSolutionDiscussionSee AlsoCreating More Accessible Navigation LinksProblemSolutionDiscussionSee AlsoAssociating Form Fields with their LabelsProblemSolutionDiscussionSee AlsoGrouping Form Fields LogicallyProblemSolutionDiscussionSee AlsoEnabling a fieldset DynamicallyProblemSolutionDiscussionSee AlsoIdentifying Required Form FieldsProblemSolutionDiscussionSee AlsoUsing ARIA Live Regions to Announce When Dynamic Content Is 2143143143144144144144144145145146Table of Contents xiii

ProblemSolutionDiscussionSee Also1461461481488. GeoLocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149IntroductionGetting Basic GeoLocation DataProblemSolutionDiscussionSee AlsoBasic GeoLocation Data with FallbackProblemSolutionDiscussionSee AlsoReverse Geocoding an Address with Latitude and LongitudeProblemSolutionDiscussionSee AlsoConvert an Address into Latitude and LongitudeProblemSolutionDiscussionSee AlsoDirections From Current LocationProblemSolutionDiscussionSee AlsoStarbucks to StarbucksProblemSolutionDiscussionSee 571571571571581591591591591621651651651651681699. Canvas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171IntroductionDrawing on a canvasProblemSolutionxiv Table of Contents171172172172

DiscussionSee AlsoUsing TransparencyProblemSolutionDiscussionSee AlsoSetting canvas DimensionsProblemSolutionDiscussionCreating a GradientProblemSolutionDiscussionSee AlsoPulling External Images Into a canvas DrawingProblemSolutionDiscussionSetting Color TransformationsProblemSolutionDiscussionSee AlsoWorking with Geometric TransformationsProblemSolutionDiscussionPlacing Text on canvasProblemSolutionDiscussionSee AlsoClipping canvas DrawingsProblemSolutionDiscussionAnimating canvas DrawingsProblemSolutionDiscussionDrawing Graphs with 91192192192192192193193193194195197Table of Contents xv

ProblemSolutionDiscussionSee AlsoSaving a canvas Drawing to a FileProblemSolutionDiscussionSee Also19719719819819919919919920010. Advanced HTML5 JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201IntroductionLocal StorageProblemSolutionDiscussionSee AlsoApplication CachingProblemSolutionDiscussionSee AlsoDrag and DropProblemSolutionDiscussionSee AlsoWeb WorkersProblemSolutionDiscussionWeb SocketsProblemSolutionDiscussionSee AlsoHistoryProblemSolutionDiscussionSee AlsoLocal FilesProblemxvi Table of 6226

SolutionDiscussionSee Also226228229Table of Contents xvii

PrefaceYour first few paragraphs should introduce the book to the reader by describing, whatthis book is about, interesting points about this technology, things to bear in mind, etc.AudienceWhile it would probably suffice to say that this book is for any person interested inlearning about.Assumptions This Book MakesSo on and so forth about what expertise and/or technical savvy is necessary to utilizeor appreciate, etc.Contents of This BookThe most common presentation of this section lists each chapter with brief descriptions. Some others describe each part or provide a narrative explanation of the book.For example:Part I describes.Chapter 1 covers.Conventions Used in This BookThe following typographical conventions are used in this book:Plain textIndicates menu titles, menu options, menu buttons, and keyboard accelerators(such as Alt and Ctrl).xix

ItalicIndicates new terms, URLs, email addresses, filenames, file extensions, pathnames,directories, and Unix utilities.Constant widthIndicates commands, options, switches, variables, attributes, keys, functions,types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, macros, the contents of files,or the output from commands.Constant width boldShows commands or other text that should be typed literally by the user.Constant width italicShows text that should be replaced with user-supplied values.This icon signifies a tip, suggestion, or general note.This icon indicates a warning or caution.Using Code ExamplesThis book is here to help you get your job done. In general, you may use the code inthis book in your programs and documentation. You do not need to contact us forpermission unless you’re reproducing a significant portion of the code. For example,writing a program that uses several chunks of code from this book does not requirepermission. Selling or distributing a CD-ROM of examples from O’Reilly books doesrequire permission. Answering a question by citing this book and quoting examplecode does not require permission. Incorporating a significant amount of example codefrom this book into your product’s documentation does require permission.We appreciate, but do not require, attribution. An attribution usually includes the title,author, publisher, and ISBN. For example: “HTML5 Cookbook by Christopher Schmittand Kyle Simpson (O’Reilly). Copyright 2011 Christopher Schmitt and Kyle Simpson,978-1-449-39679-4.”If you feel your use of code examples falls outside fair use or the permission given above,feel free to contact us at permissions@oreilly.com.xx Preface

How to Contact UsPlease address comments and questions concerning this book to the publisher:O’Reilly Media, Inc.1005 Gravenstein Highway NorthSebastopol, CA 95472800-998-9938 (in the United States or Canada)707-829-0515 (international or local)707-829-0104 (fax)We have a web page for this book, where we list errata, examples, and any additionalinformation. You can access this page at:http://www.oreilly.com/catalog/9781449396794To comment or ask technical questions about this book, send email to:bookquestions@oreilly.comFor more information about our books, courses, conferences, and news, see our websiteat http://www.oreilly.com.Find us on Facebook: http://facebook.com/oreillyFollow us on Twitter: http://twitter.com/oreillymediaWatch us on YouTube: http://www.youtube.com/oreillymediaSafari Books OnlineSafari Books Online is an on-demand digital library that lets you easilysearch over 7,500 technology and creative reference books and videos tofind the answers you need quickly.With a subscription, you can read any page and watch any video from our library online.Read books on your cell phone and mobile devices. Access new titles before they areavailable for print, and get exclusive access to manuscripts in development and postfeedback for the authors. Copy and paste code samples, organize your favorites, download chapters, bookmark key sections, create notes, print out pages, and benefit fromtons of other time-saving features.O’Reilly Media has uploaded this book to the Safari Books Online service. To have fulldigital access to this book and others on similar topics from O’Reilly and other publishers, sign up for free at http://my.safaribooksonline.com.Preface xxi

AcknowledgmentsUse this section to thank reviewer and other people and organizations that assistedwith the book’s creation.xxii Preface

CHAPTER 1Fundamental Syntax & SemanticsEmily LewisIntroductionThis chapter is designed to get you up and running with HTML5 basics.Covering the fundamental recipes, from declaring the DOCTYPE and character set todealing with ambiguities of the new HTML5 semantics, this chapter helps lay thegroundwork for the rest of the book.Specifying the DOCTYPEProblemYou want to create an HTML5 page.SolutionSpecify the HTML5 DOCTYPE at the very beginning of your page: !DOCTYPE html html head title HTML5, for Fun & Profit /title /head body /body /html Note that the DOCTYPE is not case-sensitive. Feel free to go CaMeLcAsE with the characters.1

DiscussionThe Document Type Definition, or DOCTYPE, tells browsers and validators what version of HTML the page is written in. Previous versions of HTML specified the versionnumber, such as the DOCTYPE for XHTML 1.0 Strict: !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" h HTML5, the version is dropped from the DOCTYPE. This allows HTML5 to bebackwards compatible in terms of syntax and, hopefully, makes the transition toHTML5 easier.Let’s say you have a site that is valid HTML 4.0, but you want to transition to HTML5.Start with just the DOCTYPE change, and you not only have a valid site, but also avalid HTML5 site.Additionally, all browsers recognize the shortened DOCTYPE and render in strictstandards mode.There are some elements that have changed between HTML4 andHTML5, so some now removed or deprecated parts of your HTML4document.For example, center ) might not technically validate as HTML5.See AlsoThe W3C Working Draft discussion on differences between HTML 4 and HTML5includes DOCTYPE at g the Character SetProblemYou need to define the character encoding of your web page.SolutionIn your document head, add a meta declaration for the character set: meta charset "UTF-8" / DiscussionCharacter encoding tells browsers and validators what set of characters to use whenrendering web pages.2 Chapter 1: Fundamental Syntax & Semantics

If you do not declare the character set in your HTML, browsers first try to determinethe character set from your server’s HTTP response headers (specifically, the “ContentType” header).The character set declared in the response headers is generally taken in preference overthe character set specified in your document, and is thus the preferred method.However, if you cannot control what headers your server sends, declaring the characterset in your HTML document is the next best option.If a character set is neither declared in the document nor in the response headers, thebrowser might choose one for you and it may be the wrong one for your site’s needs.This could not only cause issues with rendering, but also poses a security risk.Several years ago, a cross-site scripting vulnerability was discovered atGoogle that demonstrates the importance of character encoding: erabilityIn previous versions of HTML, the character set needed to be declared with additionalattributes and values: meta http-equiv "Content-Type" content "text/html; charset UTF-8" / But, like the DOCTYPE, HTML5 only needs the minimum information required bybrowsers. Again, this helps with backwards compatibility and makes it easier for authors to implement.Special CharactersIn this recipe, the page was specified to Unicode (UTF-8) because it is a versatile encoding that covers most web builders’ needs. Sometimes, though, you need to includea character that is outside the encoding.A great resource for character entities is at /. It includes the numeric, named and Unicodereferences for many of the more common characters allowed in HTML.For these characters, you specify them with a Numeric Character Reference (NCR) oras a named entity in order to help browsers render them correctly. If you wanted acopyright symbol, for example, you could include it in your HTML as an NCR:©Or you could include it as a named entity:©Specifying the Character Set 3

See AlsoMark Pilgrim's "Dive Into HTML5" discussion about character encoding at pecifying the LanguageProblemYou want to specify the primary language of your web page.SolutionAdd the lang attribute and appropriate value to your opening html element: html lang "en" DiscussionBrowsers, screen readers and other user agents use the lang attribute to know whatlanguage the content should be interpreted in. The example above specifies English viathe en value.Declaring a document’s primary language isn’t a requirement for HTML5 or any of theprevious versions, for that matter. It is, however, a good practice for both usability andaccessibility.See AlsoMark Pilgrim's "Dive Into Accessibility" discussion about identifying your documentlanguage at http://diveintoaccessibility.org/day 7 identifying your language.htmlOptimizing scripts and linksProblemYou want to reference JavaScript and include links to external CSS files in your webpage as simply as possible.SolutionInclude script and link declarations, but without the type attribute:4 Chapter 1: Fundamental Syntax & Semantics

link rel "stylesheet" href "styles.css" / script src "scripts.js" /script DiscussionHTML5 requires only the minimum amount of information needed for user agents. Inprevious versions of HTML, both CSS links and script required the type attribute toindicate the language. If you forgot to include type, though, most browsers assumedthe correct value.Makes sense, since there is really only one standard scripting language and only onestyling language for the web right now. HTML5 makes type officially optional, but stillvalidates older documents that do include the attribute.See AlsoThe W3C Working Draft discussion on differences between HTML 4 and HTML5includes changed attributes at s.Adding Document Structure With HTML5’s New ElementsProblemYou want to define your document structure with the new header, footer, nav, aside,section and article elements.SolutionExamine your content and document structure to determine which of the new elementswork with your page: header Is used to contain the headline(s) for a page and/or section. It can also containsupplemental information such as logos and navigational aids. footer Contains information about a page and/or section, such as who wrote it, links torelated information and copyright statements. nav Contains the major navigation links for a page and, while not a requirement, isoften contained by header. aside Contains information that is related to the surrounding content but also existsindependently, such as a sidebar or pull-quotes.Adding Document Structure With HTML5’s New Elements 5

section Is the most generic of the new structural elements, containing content that can begrouped thematically or is related. article Is used for self-contained content that could be consumed independent of the pageas a whole, such as a blog entry.A simple blog structure, with a headline, navigation, sidebar, blog posts and footer (seeFigure 1-2) could be marked up in HTML5 as: header h1 abbr title "Hypertext Markup Language" HTML /abbr 5, for Fun & Profit /h1 nav ul li a href "/Archive/" Archive /a /li li a href "/About/" About /a /li /ul /nav /header article h2 code nav /code Isn't for em All /em Links /h2 p Though the code nav /code element often contains links, that doesn't mean that em all /em lin /article article h2 You've Got the code DOCTYPE /code . Now What? /h2 p HTML5 isn't an all or nothing proposition. You can pick and choose what works best for you. So on /article aside h2 HTML5 Elsewhere /h2 p Feed your HTML5 fix with resources from our partners: /p ul li a href "http://lovinghtml5.com" Loving HTML5 /a /li li a href "http://semanticsally.com" Semantic Sally /a /li /ul /aside footer p Copyright © 2011 a href "http://html5funprofit.com" HTM5, for Fun & Profit /a . All rig /footer And, with the right CSS and supporting HTML, this markup could render on thebrowser as shown in Figure 1-1.DiscussionThese new structural elements were developed based on actual practices. A review ofover a billion web pages (see http://code.google.com/webstats/) revealed the namingconventions markup authors were already using to structure and describe their contentvia class and id, including: Header Footer6 Chapter 1: Fundamental Syntax & Semantics

Figure 1-1. Sample rendering of a simple blog structure using HTML5’s new elements. NavThe new elements in HTML5 simply attempt to reflect what authors are already doing.Structural ElementsUsing these structural elements helps you make your markup more semantic, but theyalso help define the main landmarks in the document.Consider how important it is for screen reader users and folks who navigate with thekeyboard to be able to skip to different areas of the page, like the navigation.Previously, we’ve tried to provide such functionality via “skip links” and shortcuts, butHTML5 establishes formal landmark elements that can be used.That said, this is still a hypothetical.As of this writing, no browsers or assistive technologies reference the structural elements for any navigational purposes.In fact, the Web Accessibility Initiative’s Accessible Rich Internet Applications (WAIARIA) specification already addresses how to use HTML5 with ARIA landmark roles(see Chapter 2) for this very purpose.When to Use Structural ElementsHow do you know when to use these new elements?Again, focus on your content and consider the semantics of each element.For example, most sites have an area considered a “header,” comprised of a logo, maybethe company name and a tagline. And that certainly sounds like a good case for header.You may also have a section or aside in your document with its own set of headlinesand navigation, for which header is also completely appropriate.The same holds true for footer. While most pages have content appropriate for thenew footer element — author, copyrights, related information — sections, articlesand asides can feature the same information. And, in turn, can also include footer.Adding Document Structure With HTML5’s New Elements 7

Or consider nav. You may have many groups of links on your site, some of which arefor navigation, while others are external.But nav is only appropriate for major site navigation, not search results links or a blogroll.When to Use div ElementsAs you can see from the blog markup example, the new structural elements can replacemany of the non-semantic container divs that you may have grown accustomed tousing. But div still has a place at the HTML5 party.If you ever need a containing element strictly for style purposes, div is the element touse. You don’t want to use one of the new structural elements just to serve as a hookfor your CSS. That is not what semantic markup is about.Just remember to focus on your content and avoid unnecessary use of div, such as whenanother element is more semantic.For example, if you have a paragraph of text, use the p element not div. Both give youblock-level formatting, but p is more semantic for that particular purpose.Styling Structural ElementsAll of today’s browsers render the content contained by these new elements, howeversome browsers don’t recognize them and, as such, treat them like inline elements. Thisdefault rendering can cause some serious problems with styling.Fortunately, it doesn’t take much to fix.The current cast of browsers simply needs to be told to treat the elements as block-levelelements:header, footer, nav, article, aside, section {display: block;}And with that single CSS declaration, you can happily style the structural elements.Well, almost.In versions of Internet Explorer (IE) prior to IE9 you have to add a bit of JavaScript toyour document so IE recognizes the elements and allow you to style them: script nt('aside');document.createElement('section'); /script 8 Chapter 1: Fundamental Syntax & Semantics

Any time you add a new HTML5 element to your document, you need to add thisdocument.createElement if you want cooperation from Internet Explorer before version9. See Chapter 2 for a more detailed discussion on using JavaScript with IE.See AlsoScript Junkie's article "Using HTML5's New Semantic Tags Today" at 786.Choosing Between article and sectionProblemYou don’t know whether article or section is the most appropriate element to use onyour web page.SolutionFocus on your content and the semantic definitions of article and section (“ChoosingBetween article and section” on page 9). article article can be considered a specialized form of section. It is intended for content thatcould stand on its own, outside of all surrounding content, such as “syndicatable”content like blog posts.article is suitable for other types of content, including: Video and accompanying transcript News articles Blog commentsOften the name of an article or a blog post is in the title of the URI. Ifthat’s the case with a page you are working on, that content should bewrapped in an article element.As you saw in the code example in “Choosing Between article and section” on page 9, we used article to contain the individual blog posts: article h2 code nav /code Isn't for em All /em Links /h2 p Though the code nav /code element often contains links, that doesn't mean that em all /em lin /article Choosing Between article and section 9

article h2 You've Got the code DOCTYPE /code . Now What? /h2 p HTML5 isn't an all or nothing proposition. You can pick and choose what works best for you. So on /article section section, meanwhile, is the most generic of the new structural elements, intended tosimply group related content. However, it is not a generic container element like div.The content it groups must be related.This definition, applied to “Choosing Between article and section” on page 9, couldmean the addition of section as the parent element for both instances of article: section article h2 code nav /code Isn't for em All /em Links /h2 p Though the code nav /code element often contains links, that doesn't mean that em all /em /article article h2 You've Got the code DOCTYPE /code . Now What? /h2 p HTML5 isn't an all or nothing proposition. You can pick and choose what works best for you. S /article /section This example meets the core criteria for section: the grouped content is thematicallyrelated.But, wait! The spec (see on-element) further clarifies:“A general rule is that the section element is appropriate only if the element's contentswould be listed explicitly in the document's outline.”The document outline refers to HTML5’s new sectioning content model where each ofthe new structural elements creates its own self-contained outline. This outline is generated by the headings (h1-h6) contained in each element (see “EmphasizingText” on page 15).So, based on this context, if you want to use section, the content should have a naturalheading.Given this additional clarification, let’s modify the markup from “Choosing Betweenarticle and section” on page 9 to include a heading for section: section h1 Most Recent Blog Posts /h1 article h2 code nav /code Isn't for em All /em Links /h2 p Though the code nav /code element often contains links, that doesn't mean that em all /em /article article h2 You've Got the code DOCTYPE /code . Now What? /h2 p HTML5 isn't an all

HTML5 Cookbook by Christopher Schmitt and Kyle Simpson Published by O'Reilly Media, Inc., ,