Chapter 3. Elements That Are New in HTML5

HTML5 provides us with new and mostly semantic elements; it redefines some existent elements and makes other elements obsolete (think of “obsolete” as the new, politically correct version of “deprecated”). As we saw in Chapter 2, we have the root <html> element, document metadata described in the <head> section, and scripting elements. HTML5 provides us with sectioning elements, heading elements, phrase elements, embedded elements, and interactive elements. Interactive form elements are covered in Chapter 4. The media-related embedded elements will be discussed in Chapter 5. We won’t discuss table elements, since, for the most part, they haven’t changed in HTML5. The other elements are discussed in the next section.

In prior specifications, elements were described as being either inline, for text-level semantics, or block, for flow content. HTML5 doesn’t use the terms block or inline to describe elements anymore. The HTML5 authors correctly assume that CSS is responsible for the presentation, and all browsers, including all mobile browsers, come with stylesheets that define the display of elements. So, while HTML5 no longer defines elements in terms of block or inline, the default user-agent stylesheets style some elements as block and others as inline, but the delineation has been removed from the specification.

With HTML5, we have most of the HTML 4 elements and the addition of a few new ones. HTML5 also adds several attributes and removes mostly presentational elements and attributes that are better handled by CSS. In Chapter 2, we covered the new global attributes. In this chapter, we cover many of the new elements of HTML5, and some of the existent elements that have had major changes. Other elements such as form and embedded content elements will be discussed in their own, separate chapters.

Sectioning Elements in HTML5

The sectioning root is the <body> element. Other HTML 4 sectioning elements included in the HTML5 spec are the rarely used <address> element and the leveled headings from <h1> to <h6>. HTML5 adds several new sectioning elements, such as:[21]

  • section

  • article

  • nav

  • aside

  • header

  • footer

It also maintains support for these older sectioning elements:

  • body

  • h1–h6

  • address

The new sectioning elements encompass content that defines the scope of headings and footers. The new sectioning elements, like <footer>, <aside>, and <nav>, do not replace the <div> element, but rather create semantic alternatives. Sectioning elements define the scope of headings and footers: does this heading belong to this part of the page or the whole document? Each sectioning content element can potentially have its own outline. A sectioning element containing a blog post, for example, can have its own header and footer. You can have more than one header and footer in a document. In fact, the document, each section, and even each blockquote can have its own footer. Because each section has its own scope for headings, you are not limited to a single <h1> on a page, or even limited to six heading levels (<h1> through <h6>). Let’s cover the new sectioning elements.

The authors of HTML5 scanned billions of documents, counting each class name, to determine what web developers were calling the various sections of their page. Opera repeated the study, including the names of IDs in addition to class names. Due to Dreamweaver and Microsoft Word, style1 and MsoNormal were very, very popular. Ignoring the software-generated classes and obviously presentational class names like left and right, they discovered that web developers were using semantic sectioning names like main, header, footer, content, sidebar, banner, search, and nav almost as if they were included in a default Dreamweaver template (they weren’t).

Reflecting what developers were doing, more than 25 new elements have been added to HTML5. Originally missing was the “main” or “content” elements. The reason? Anything that is not part of a navigation, sidebar, header, or footer is part of the main content. The <main> element is a late addition to the specification, and is described .

Using the new elements, like <header> and <footer>, which replace and make more semantic the semantically neutral <div id="header"> and <div id="footer">, we can create the standard web layout in a more semantic way. These new elements, shown in Figure 3-1 in what is a common page layout, enable including semantics to the layout of a document.

There is some debate in the spec-writing community that these two elements are too similar. You should use <article> versus <section> when the encapsulated content is a discrete item of content. It is often a judgment call. The only difference in the two code snippets given in the previous two sections is that I’ve added an optional <time> to the <article> example.

In terms of explaining the similarities, differences, and functionalities of these two elements, use the analogy of the Sunday newspaper (for those of you too young to remember what a newspaper is, it’s that thing you recycle or start chimney fires with). The Sunday newspaper has several sections: the front page, news, real estate, classified, weekly magazine, comics, and so on. Each of these sections has articles. Those articles have headers, and some, especially in-depth news reports, have nested sections. Similar to the Sunday paper, articles and sections can be nested within each other and within themselves.

An <article> can be a forum post, a magazine or newspaper article, a blog entry, a user-submitted comment, an interactive widget or gadget, or any other independent item of content.

The general rule is that the <section> element is appropriate only if the element’s contents can be listed explicitly in the document’s outline, such as saying “the ‘<section> versus <article>’ section is in this book.” The HTML5 sections grouping you are reading right now would be in a <section> tag if it were online.

Examples of sections would be chapters, the various tabbed pages in a tabbed dialog box, or the numbered sections of a thesis. A website’s home page could be split into sections for an introduction, news items, and contact information.

Authors are encouraged to use the <article> element instead of the <section> element when it makes sense to syndicate the contents of the element.

With games on mobile devices, we want to provide as much room as possible for the board. However, if our users have a desktop screen, we have all this extra room! So, depending on browser size, we include a header and footer above and below the game in larger screens:

1     <article>
2      <header>
3          <h1>CubeeDoo</h1>
4          <h2>The Mobile Memory Matching Game</h2>
5      </header>
6      <section id="game" class="colors">
7        <div id="board" class="level1">
8        <!-- game board goes here -->
9        </div>
10        <footer> <!-- footer for the section -->
11           <div class="control scores">Scores</div>
12           <div class="control menu" id="menu">Menu</div>
13           <ul>
14             <li id="level">Level: <output>1</output></li>
15             <li id="timer">
16              <div id="seconds"></div>
17             </li>
18             <li><button id="mutebutton">Mute</button></li>
19             <li id="score">Score: <output>0</output></li>
20           </ul>
21        </footer>
22      </section>
23      <footer><!-- footer for the article -->
24       <ul>
25          <li><a href="about/estelle.html">About Estelle</a></li>
26          <li><a href="about/justin.html">About Justin</a></li>
27       </ul>
28       </footer>
29      </article>

We’ve contained our page in an <article> with the game (lines 6–22) a <section> nested within that article. The document has a <header> (lines 2–5) and <footer> (lines 23–28). In the article header, we have a title and a subtitle marked up as an <h1> and <h2>. The page has two footers. In addition to the document footer that is relevant and is visible in all the pages on our site, there is also a <footer> (lines 10–21) for the game’s main screen that is encapsulated in a <section>.

There are more than 20 text-level semantic elements in HTML5. Some of these elements are new, some have been repurposed, some have attribute changes, some have remained the same, and a very few, like <acronym>, have been removed from the specification altogether. The elements are shown in Table 3-2.

The <mark> element is used to mark text due to its relevance in another context. “Huh?” you may ask. Think of <mark> as “marking text,” rather than giving strong or emphasized importance to it. A good use is highlighting search terms in search results, or text in one block that you reference in the next.

Have you ever used native search in Safari desktop? You know how it grays out the page, highlighting occurrences of the term you were seeking?

Were you to create that effect, or the effect of viewing a cached page in Google search, it would be correct to use <mark> for all the results that were found, with:

mark {background-color: yellow;}
mark:focus {background-color: blue;}

in your CSS. This would give you an effect similar to the results shown in Figure 3-2.

While presentational in explanation, it does have semantic meaning. Text that is marked gets semantically (and with CSS, visually) highlighted to bring the attention to a part of the text that might not have been considered important in its original unmarked presentation.

<mark> can be used to indicate a part of the document highlighted due to its current, though not necessarily permanent, relevance. For example, when searching for “HTML5,” to highlight the search term in the resulting page, the most semantic method would be to code it as follows, styling <mark> with CSS:

<p><mark>HTML5</mark> is currently under development as the
   next major revision of the HTML standard. Like its
   immediate predecessors, HTML 4.01, and XHTML 1.1,
   <mark>HTML5</mark> is a standard for structuring and
   presenting content on the World Wide Web.</p>

You can add scripts to your search functionality to encapsulate search term results with the <mark> tags, and then you can style the marks with CSS to show how many search results were found and where they are.

The <ruby>, or ruby annotation, element allows spans of phrasing content to be marked with ruby annotations. This has nothing to do with the Ruby programming language. Rather, ruby annotations are notes or characters used to show the pronunciation of East Asian characters (Figure 3-3).

The <rp>, or ruby parenthesis, element can be used to provide parentheses around a ruby text component of a ruby annotation, to be shown by browsers that don’t support ruby annotations, hidden when the browser does support it. The <rt>, or ruby text, element marks the ruby text component of a ruby annotation.

Use together with the <rt> and/or the <rp> tags: the <rp> element provides information, an explanation, and/or pronunciation of the <ruby> contents. The optional <rp> element defines what to show browsers that do not support <ruby>. We don’t use this in CubeeDoo and ruby is only partially supported on mobile devices. If you are interested in more information, there is a link to a good explanation of these three elements and their implementations in the online chapter resources.

A few elements from HTML have been modified in HTML5, including a, small, s, cite, i, b, and u.

As you well know, the <a> element represents a hyperlink. While not new, we include a description here since there are changes to the element in HTML5, and there are special mobile actions depending on the value of the now-optional href attribute.[25]

First, note that some attributes of the <a> element are now obsolete, such as the name attribute. To create an in-page anchor, put the id attribute on the closest element to your target in the document and create a hyperlink to that element using the id. For example, <a href="#anchorid"> is an anchor link that targets the element with an id of anchorid. Also obsolete are the shape, coords, charset, methods, and rev attributes.

The target attribute of <a>, which was deprecated in XHTML Strict, is back. There are a few new attributes, including download, media, and ping.[26] The download attribute indicates the hyperlink is intended for downloading. The value, if included, is the name the filesystem should use in saving the file. The media attribute takes as its value a media query list for which the destination of the hyperlink was designed. The ping attribute accepts a space-separated list of URLs to be pinged when a hyperlink is followed, informing the third site that an action was taken, without redirecting through that site.

Also different in HTML5 is that the <a> element can encompass both inline and block-level content, or in HTML5 parlance, sectioning and phrase elements. For example, this is now a valid HTML5 hyperlink:

<a href="index.html" rel="next" target="_blank">
   <header>
    <h1>This is my title</h1>
    <p>This is my tagline</p>
  </header>
</a>

Mobile devices have a few link types that receive special treatment when displayed in a browser or in the mobile device’s email client.[27]

You’re likely familiar with mailto: links. When clicked, it opens your computer’s or mobile device’s email application, creates a new message, and addresses it to the target of the link. You can also include the subject and content for the email.

The tel: link will open the mobile device’s calling application and calls the number used as the link’s target. In iOS, a confirmation dialog pops up before redirecting to the phone application and dialing the number for you. When a tel: link is clicked in Android, users are brought directly to the phone application with the telephone number from the link pre-entered, but doesn’t dial for you. Similarly, the sms: link will open up messaging.

If you are unfamiliar with SMS links, the syntax is:

sms:<phone_number>[,<phone-number>]*[?body=<message_body>]

Clicking on the following hyperlink will open an alert that asks if you want to call the number in the link or cancel in iOS, and brings up the key pad with the number pre-populated on Android. The SMS link opens up the messaging application. Remember that not all devices have SMS capabilities:

<a href="tel:16505551212">1 (650) 555-1212</a>
<a href="sms:16505551212">Text me</a>

Telephone number detection is on by default on most devices. For example, Safari on iPhone automatically converts any number that takes the form of a phone number of some countries to a phone link even if it isn’t a link, unless you explicitly tell the device not to (see format-detection in Chapter 2).

Other links handled differently are Google Maps, YouTube links, iTunes links, and Google Play. When a regular link to a Google Maps page is included in a web page or an email, some mobile devices will open the device’s map application, rather than opening the map in the current or new browser window. Recognizing the link, the phone will launch the Maps application instead:

<a href="http://maps.google.com/maps?q=san+francisco,+ca"> Map of SF</a>

Links to YouTube and to the iTunes store (in iOS) will launch the YouTube widget and iTunes respectively. Links on Android for Android applications will open a pop-up that asks if you want to follow the link or open the link in Google Play.

There are examples of these link types in the online chapter resources.

Text-Level Element Changes from HTML 4

We all thought the presentational elements of <i>, <b>, <s>, <u>, and <small> were destined to become deprecated, or made obsolete. Instead, they have newfound glory with semantic meaning.

The <i> element should be used instead of a <span> to offset text from its surrounding content without conveying any extra emphasis or importance, such as when including a technical term, an idiomatic phrase from another language, a thought, or a ship name.

The <b> element represents a span of text to be stylistically offset without conveying any extra importance, such as keywords in a document abstract, product names in a review, or other spans of text whose typical typographic presentation is bold.

The <s> element encompasses content that is no longer accurate or relevant and is therefore “struck” from the document. The <s> element has been around for a long time, but before HTML5 only had a presentational definition. HTML5 provides <s> with semantic value.

Similarly, the <u> element has been given semantic value. The <u> element represents text offset from its surrounding content without receiving any extra emphasis or importance, and for which the typographic convention dictates underlining, such as misspelled words or Chinese proper names.

The <small> element should be used to represent the “fine print” part of a document. While <small> text doesn’t need to be displayed in tiny print, it should be reserved for the fine print, such as legalese in a sweepstakes or side effects in a pharmaceutical ad (which, if you think about it, should really be in a huge font).

While I didn’t think <cite> was heading for the realm of distant memories, it too has acquired new meaning. The <cite> element now solely represents the cited title of a work; for example, the title of a book, song, film, TV show, play, legal case report, or other such work. In previous versions of HTML, <cite> could be used to mark up the name of a person: that usage is no longer considered conforming.

The 12 embedded elements include six new elements and six old elements. The new elements include the following:[28]

  • embed

  • video

  • audio

  • source

  • track

  • canvas

And these are the existing elements:

  • img

  • iframe

  • object

  • param

  • map

  • area

The embedded elements include the media elements, <video>, <audio>, <source>, <track>, and <canvas>, which we will discuss in Chapter 5. The other “new” element is <embed>, which has been implemented for years but was never part of the HTML 4 or XHTML specifications. We’ll discuss this new element and visit the previously existent elements, as some attributes have been made obsolete.

The <iframe> element is not new to HTML5, but it does have different attributes than before. <iframe> lost longdesc, frameborder, marginwidth, marginheight, scrolling, and align attributes, and gained srcdoc, sandbox, and seamless.

The srcdoc attribute value is HTML that is used to create a document that will display inside the <iframe>. In theory, any element that can be used inside the <body> can be used inside the srcdoc. You should escape all quotes within the srcdoc value with &quot; or your value will end prematurely. If a browser supports the srcdoc attribute, the srcdoc content will be used. Browsers that do not support the srcdoc attribute will display the file specified in the src attribute instead:

<iframe srcdoc="<p>Learn more about the 
<a href=&quot;http://developers.whatwg.org/the-iframe-element.html
#attr-iframe-srcdoc&quot;>srcdoc</a> attribute." 
src="http://developers.whatwg.org/the-iframe-element.html
#attr-iframe-srcdoc"></iframe>

The sandbox attribute enables a set of extra overrideable restrictions on any content hosted by the <iframe>. The effect of adding the attribute is to embed the externally sourced content as if it were served from the same domain, but with severe restrictions. Plug-ins, forms, scripts, and links to other browsing contexts within the <iframe> are disabled. The content of the <iframe> is treated under a unique origin and cannot traverse the DOM or read cookie information. These features are overwritable by setting a value to the sandbox attribute that conforms to a specific syntax.

The allow-same-origin keyterm allows the content to be treated as being from the same origin instead of forcing it into a unique origin. The allow-top-navigation keyword allows the content to navigate its top-level browsing context; and the allow-forms, allow-pointer-lock, allow-popups, and allow-scripts keywords re-enable forms, the pointer-lock API, pop-ups, and scripts, respectively. Include zero or more space-separated values depending on your needs, but realize that each value can create a security risk:

<iframe sandbox="allow-same-origin allow-forms allow-scripts" 
    src="http://maps.google.com" seamless></iframe>

The seamless attribute makes the <iframe> appear seamless: as if it’s a native part of the parent document. When supported, it is expected that it will allow CSS rules to cascade from parent through to the contents of the <iframe>, enable links to navigate the parent, and grow and shrink as necessary to expand to fit the parent, all on same origin only.

The interactive elements currently include form elements, the changed <menu> element, the new <detail>, <summary>, and <command> elements.

Have you ever created a node that, when clicked on, opens up more details about the content of the node? And, when clicked on again, the details disappear? When supported, the <details> and child <summary> enable doing this natively in HTML5 without any JavaScript (Figure 3-4).

The <details> element can be used to encompass a disclosure widget from which the user can obtain additional information or controls, such as content that may best fit into footnotes, endnotes, and tooltips. The <details> element has an open attribute that is also new in HTML5. With the open attribute, the content of the <details> will initially be visible. Without the open attribute, the details will be hidden until the user requests to see them.

The <summary> element should be included as a child element of the <details> element, with its text node providing a summary, caption, or legend for the rest of the contents of the <summary> element’s parent details element. The contents of the <summary> show by default whether the open attribute is set or not. By clicking on the <summary>, the user can show and hide the rest of the content of the <detail> element. This interactivity is a default behavior of the <details>/<summary> element combo and, when supported, requires no JavaScript:

<details>
  <summary>5 of 5 stars from three reviews</summary>
  <ul>
     <li>5 stars from Amazon</li>
     <li>5 stars from Costco</li>
     <li>5 stars from Barns &amp; Noble</li>
  </ul>
 </details>

When supported, the contents of <details> (except for the <summary>) are hidden, as the optional open attribute is not included (the default is to hide everything other than the summary). The rest of the contents should display when the <summary> is clicked. The <summary>, not to be confused with the summary attribute of the <table> element, a child of the <details> element, is the control that toggles the rest of the <details> content between visible and hidden (as if display: none; was set). The <summary> element represents a summary, caption, or legend for the rest of the contents of the parent <details> element.

Because the <summary> is the control to open and close the <details> element, it is always visible (in both the details open and not open states). Until all browsers support this functionality, it’s easy to replicate with JavaScript. Simply add an event listener to the summary element that adds and removes the open attribute on the parent <details> element, and add the following styles to your CSS:

details * {display: none;}
details summary {display: auto;}
details[open] * {display: auto;}

If that last line doesn’t make sense to you yet, don’t worry! We cover attribute selectors in Chapter 8.

The <menu> element, deprecated in HTML 4.01 and XHTML, has resurfaced. Originally, it was defined as a “single column menu list.” In HTML5, the <menu> element has been redefined to represent a list of commands or controls.

The <menu> element has been redefined in HTML5 to list form controls. The value of the <menu>’s id attribute can be included as the value of a <button>’s menu or <input>’s menuitem attribute to provide a menu or context menu for a form control. A menu can contain <menuitem> elements that cause a particular action to happen. The type attribute set to toolbar should be used when using <menu> to mimic a toolbar or when using context to create a content menu. The value of the label attribute determines the menu’s label. They can be nested to provide multiple levels of menus.

Almost all of the elements from XHTML are still available and valid in HTML5. The elements that are obsolete include the following:

As mentioned earlier, a few elements, instead of being made obsolete, have gained more semantic meaning. <b>, <hr>, <i>, <u> and <small>, while completely presentational in prior specs, now have semantic meaning and are defined beyond their appearance. <menu> has gained a purpose to be useful for toolbars and context menus. <strong> now means “important” rather than “strongly emphasized.” <a> can now encompass blocks instead of just inline content, and doesn’t need to have the href attribute present.

Some attributes are now obsolete. Some attributes have been added. Most of the changes are in web form elements, which we’ll cover in great detail in Chapter 4. Otherwise, a few things to note that have not been previously detailed include:

The HTML5 spec is huge. This section has introduced you to the syntax and semantics of HTML5, and to some of the new elements. This chapter was intended as a quick (or not so quick) explanation of the new elements in HTML5.

We’re not done with HTML5. We are going to cover some wonderful features of web forms in Chapter 4, and show you how the Web Form features of HTML5 can help you quickly develop fantastic user interfaces with minimal JavaScript.



[21] The hgroup element that was originally added to HTML5 was made obsolete. It has not yet been removed from the WHATWG spec, but has been removed from the W3C HTML5 and W3C HTML5.1 specifications.

[22] Because of its semantic value, screen readers announce the beginning and end of each <section>. Unless your page is comprised of semantic sections of content, use the nonsemantic <div>, which is not called out by the screen reader.

[23] Date values must be machine-readable dates. Date strings are defined at http://www.w3.org/TR/NOTE-datetime.

[25] If the href attribute is not specified, the <a> represents a placeholder hyperlink.

[26] The media, ping, and download attributes are under discussion, but are expected to be included in the specifications.

[27] Skype can also be launched from the browser. Check out http://dev.skype.com/skype-uri for more details.

[28] The <picture> element for responsive images is currently under consideration. The draft specification is at http://www.w3.org/TR/html-picture-element/.

[29] The Clown Car Technique is described in full detail at http://github.com/estelle/clowncar/.