Many web developers new to HTML5 initially ask the question, What are the new tags or elements in HTML5? Some of the new elements (e.g., <video>, <audio>, <canvas>) provide an immediate, beneficial effect on the user experience (UX) when they are rendered in a browser. Yet many of the new elements, called semantic elements and listed in Figure 1, have no visual impact whatsoever on UX. What are these elements, and why should you care about them?

Figure 1: HTML5 semantic elements






Independent content, such as a blog post or article



Content slightly related to primary content on page



For grouping standalone content, such as video or image



For use with <figure>, (optionally) used to provide caption



Providing data such as author, copyright



Introductory headings; could include navigation



For grouping <h1> to <h6>



Navigation—typically site-level



Text to be referenced or highlighted



Grouping of content usually with a heading, similar to chapters



For date and/or time representation

In a previous article in this series, "Three of the Most Important Basic Elements in HTML5," we introduced the concept of semantic tags by considering the <header>, <nav>, and <footer> elements. This article extends the discussion by going into more depth about the overall structure of an HTML5 document and the key reasons for using any of the new semantic elements.

Examining a Typical HTML Template

To demonstrate the "how and why" of semantics, let's consider how you could use HTML5 semantic elements to upgrade the default template of an ASP.NET MVC 3 Razor website in Visual Studio 2010. (Our example assumes that Visual Studio 2010 SP1 has been installed, as well as the Web Standards Update.) To follow along, simply create a new website in Visual Studio 2010 and choose ASP.NET Web Site (Razor), as shown in Figure 2. Note that although the example website in this article is based on ASP.NET, it will demonstrate aspects of semantic elements that you can apply to your own website development.

Creating a new ASP.NET Razor website

The resulting files produced when creating an ASP.NET website using Razor syntax include a file named _SiteLayout.cshtml (or .vbhtml, for a site created using Visual Basic). This file is used as a template, so that other pages can focus more on the main content of the page. Take a look at the overall markup (minus the contents of the <body> element) of this HTML5 document, shown in Figure 3.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.     <head>
  4.         <meta charset="utf-8" />
  6.         <title>@Page.Title</title>
  8.         <link href="@Href("~/Styles/Site.css")" rel="stylesheet" />
  9.         <link href="@Href("~/favicon.ico")" rel="shortcut icon"
  10.                 type="image/x-icon" />
  11.     </head>
  12.     <body>
  13.         <!-- see figure 4 -->
  14.     </body>
  15. </html>

The content in the <title> element in callout A contains the following syntax:

  1. @Page.Title

This is a placeholder for the title content, which will be provided by the requested page. We'll see an example of this shortly.

For now, let's examine the markup between the <body> tags of _SiteLayout.cshtml, shown in Figure 4. Callouts A and B show code changed from the default content. This is a <div>-centric document typical of websites today. However, the only way to understand the purpose of a <div> in this example is by inference of the id attribute. Found in this file are id values such as page, header, login, main, content, and footer.

  1. <div id="page">
  2.   <div id="header">
  4.     <p class="site-title">HTML5 Demo Site</p>
  6.     <div id="login">@if (WebSecurity.IsAuthenticated)
  7.     {
  8.       <p>Welcome
  9.         <a href="@Href("~/Account/ChangePassword")"
  10.            title="Change password">@WebSecurity.CurrentUserName</a>!
  11.         <a href="@Href("~/Account/Logout")">Logout</a>
  12.       </p>
  13.       } else {
  14.       <ul>
  15.         <li><a href="@Href("~/Account/Register")">Register</a></li>
  16.         <li><a href="@Href("~/Account/Login")">Login</a></li>
  17.       </ul>
  18.     }
  19.     </div>
  20.     <ul id="menu">
  21.       <li><a href="@Href("~/")">Home</a></li>
  22.       <li><a href="@Href("~/About")">About</a></li>
  23.     </ul>
  24.   </div>
  25.   <div id="main">
  26.     <div id="content">
  27.       <h1>@Page.Title</h1>
  28.       <h2>@Page.Title</h2>
  29.       @RenderBody()
  30.     </div>
  32.     <div id="footer">&copy; @DateTime.Now.Year, Don't steal me...</div>
  34.   </div>
  35. </div>

Note the <div> with an id of "content" in Figure 4, which contains the following syntax:

  1. <h1>@Page.Title</h1>
  2. <h2>@Page.Subtitle</h2>
  3. @RenderBody()

These lines will render content from the requested page. For example, if the requested page is default.cshtml, the contents of that file will be rendered where the @RenderBody() syntax is located. Values for @Page.Title and @Page.Subtitle will be assigned in a code block in the requested page. So for completeness, we will use default.cshtml as the requested file. Figure 5 shows the adjusted markup of that file.

  1. @{  
  2.     Layout = "~/_SiteLayout.cshtml";
  3.     Page.Title = "Semantics Demonstrated!";
  4.     Page.Subtitle = "The Secret is in the Source...";
  5. }
  6. <div id="logo">
  7.   <img src=""
  8.        width="133" height="64"  
  9.        alt="HTML5 Powered with Semantics"
  10.        title="HTML5 Powered with Semantics" />
  11.   <span>HTML5 Semantics</span>
  12. </div>
  13. <p>
  14.   A list of semantic elements
  15. </p>
  16. <ul>
  17.     <li>article</li>
  18.     <li>aside</li>
  19.     <li>figure</li>
  20.     <li>figcaption</li>
  21.     <li>footer</li>
  22.     <li>header</li>
  23.     <li>hgroup</li>
  24.     <li>nav</li>
  25.     <li>mark</li>
  26.     <li>section</li>
  27.     <li>time</li>
  28. </ul>
  29. <div id="sidenote">
  30.     <h1>
  31.         <p>
  32.         What does semantic mean? A relevant definition is:
  33.         <span class="definition">to show, indicate by sign</mark>
  34.         In HTML5, the "sign" is the element.
  35.     </p>
  36. </div>

To support the modifications to the initial files of the website, the site.css file has been updated to contain additional style rules, as Figure 6 shows.

  1. /* all ul#menu replaced with #menu */
  2. h2
  3. {
  4.     padding-top: 0;
  5.     margin-top: 0;
  6.     font-size: 1.3em;
  7.     color: #5c87b2;
  8. }
  9. img
  10. {
  11.     display: block;
  12.     float: next;
  13. }
  14. #sidenote
  15. {
  16.     width: 330px;
  17.     border: 1px dashed #990000;
  18.     padding: 5px;
  19.     float: next;
  20. }
  21. #sidenote h1
  22. {
  23.     font-size: 1.2em;
  24.     color: #990000;
  25. }
  26. section h1
  27. {
  28.     font-size: 1.1em;
  29. }
  30. nav h1
  31. {
  32.     display: none;
  33. }
  34. #logo
  35. {
  36.     float: right;
  37. }
  38. .definition
  39. {
  40.     font-style: italic;
  41.     background-color: #e8eef4;
  42. }

When the default.cshtml file is requested and rendered, it will appear as shown in Figure 7.

Default.cshtml rendered in a browser

At this point, none of the new semantics elements have been introduced, but the scene has been set for showcasing how and why we will use them.

To <div> or not to <div>...

As noted earlier, _SiteLayout.cshtml (see Figure 3) is structured primarily using <div> elements. And although doing so is now commonplace (and is perfectly legal in HTML5), we have opportunities to provide more meaning to our content by making minor modifications to the HTML source. But before we do that, we have to ask whether or not a <div> is truly the right choice for grouping content.

What about the first <div> in our example:

  1. <div id="page"> ... </div>

What purpose does the above <div> serve? As the only direct child element of body, it appears to be a container for the entire page. Why is this here? The following definition in the site.css file gives the answer:

  1. #page
  2. {
  3.     width: 90%;
  4.     margin-left: auto;
  5.     margin-right: auto;
  6. }

This style definition is applied to <div> to provide value to the presented page. Because the sole purpose for this <div> is to support presentation, it does not need to be changed—nor does it have any semantic value.

Now let's consider the next <div> (displaying what is relevant):

  1. <div id="header">
  2.   <p class="site-title">HTML5 Demo Site</p>
  3.   ...
  4. </div>

With HTML5, semantic elements can replace the <div> and <p> tags to provide more meaning. But wait—who really cares whether or not our data has more meaning? The answer might lie in the rephrasing of the question: What really cares whether or not our data has more meaning? How will your page be parsed by search engines? What will the logical outline be according to the browser?

Fortunately, a number of online resources are available to help provide insight about how HTML pages will appear in "outline" form. For our example, I will use When we feed the outliner site with the rendered source of the site with no modifications, the site should output an outline similar to the one that Figure 8 shows.

Example HTML5 source outline

There are several logical problems in the resulting outline:

  1. The site's header is ignored.
  2. The subtitle supports the title; it should not be its own section.
  3. The "side note" of the page is given too much importance.

We will solve all these problems by using semantic elements.

Introducing Semantics

Returning to our <div id="header">, we will now change it to the following:

  1. <header id="header">
  2.   <h1 class="site-title">HTML5 Demo Site</h1>
  3.   ...
  4. </header>

The <header> element contains content that is page- or site-wide, and usually includes navigation (something we will visit shortly). In the example above, an <h1> tag is used inside the <header> tag to provide the heading title. In the past, it was considered proper form for only one <h1> element to exist in the page. That is not true for HTML5 documents. The context of its placement is what gives it weight.

Was it necessary to keep the id and class attributes in the above changes? No. However, removing the attributes could affect style definitions in the site.css file. To demonstrate a smooth transition, we elected to keep all id and class values in their new semantic element homes. If you decide to remove them in your sites, you should modify dependent CSS files accordingly.

Let's revisit the following markup in the _SiteLayout.cshtml file:

  1. <div id="content">
  2.     <h1>@Page.Title</h1>
  3.     <h2>@Page.Subtitle</h2>
  4.     @RenderBody()
  5. </div>

The primary "content" of this page can really be viewed as an article with a title, subtitle, and body of information. Thus, here is how it looks with semantic markup:

  1. <article id="content">
  2.     <hgroup>
  3.         <h1>@Page.Title</h1>
  4.         <h2>@Page.Subtitle</h2>
  5.     </hgroup>
  6.     @RenderBody()
  7. </article>

The <div> was replaced with an <article> element. The <h1> and <h2> elements are now grouped in an <hgroup> element, so that the <h2> tag does not introduce a new section into the outline. In fact, if we run the page through the outliner site now, we will see the improvements shown in Figure 9. The site-level heading now appears, and the subtitle has disappeared! Notice that despite multiple <h1> elements, there is correct indentation between the first two.

Example HTML5 source outline after first set of code modifications

The source of the final line in the outline is the last <div> in default.cshtml (see Figure 5).  Since this data is considered "side note" data, and is not regarded as the main content of the article, we can re-engineer it to use semantics, as follows:

  1. <aside id="sidenote">
  2.   <h1>What does semantic mean?</h1>
  3.   <p>
  4.     A relevant definition is:
  5.     <mark class="definition">to show, indicate by sign</mark>
  6.     In HTML5, the "sign" is the element.
  7.   </p>
  8. </aside>

Once again, the <div> has been replaced—this time with the <aside> element. Although the <aside> contains an <h1> element, it will be regarded with less importance by the search-engine parser because of its ambient container. The <span> was replaced with a more appropriate choice, the <mark> element. As the name implies, it represents text to be marked or highlighted. After one more run-through with the outliner after all the code changes, we see a proper indentation of the outline, shown in Figure 10.

Example HTML5 source outline after second set of code modifications

More Semantics

There are other areas in the document that are easy to identify for semantic markup. For example, in the _SiteLayout.cshtml file (see Figure 3), the final <div> on that page is for the footer. We can replace it as follows:

  1. <footer id="footer">
  2.   &copy;
  3.   <time datetime="@DateTime.Now.ToString("yyyy-MM-dd")">
  4.     @DateTime.Now.Year
  5.   </time>, Don't steal me...
  6. </footer>

Note the insertion of the <time> element. It simply provides a stronger context for any date or time provided in the HTML markup.

How about the links to other pages in the site? The following code:

  1. <ul id="menu">
  2.   <li><a href="@Href("~/")">Home</a></li>
  3.   <li><a href="@Href("~/About")">About</a></li>
  4. </ul>

can be changed to this:

  1. <nav id="menu">
  2.   <h1>Site Navigation</h1>
  3.   <li><a href="@Href("~/")">Home</a></li>
  4.   <li><a href="@Href("~/About")">About</a></li>
  5. </nav>

Because the links in the above code are regarded as the primary navigation within our site, it is appropriate to use the <nav> element in this case. But why did an <h1> tag sneak in? The <nav> element is factored in the outlining of the document. An <h1> element provides the semantic meaning, though the site.css file prevents it from being viewed.

What about the HTML5 logo in default.cshtml (Figure 5)? It can be transformed to be semantic, as follows:

  1. <figure id="logo">
  2.     <img src=""
  3.         width="133" height="64"  
  4.         alt="HTML5 Powered with Semantics"
  5.         title="HTML5 Powered with Semantics" />
  6.     <figcaption>HTML5 Semantics</figcaption>
  7. </figure>

Not only was the <div> replaced with <figure>, the span was replaced with a more suitable option, the <figcaption> element.

The Final Section

The co-author of this series, Daniel Egan, and I are often asked by those new to HTML5 when to use the <section> element. It is our opinion that this element has the potential to be the most misused or unused of the semantic elements. In our example, our "article" is rather small. The larger an article, the more likely it will be divided into manageable "sections" (as is the case with the article you are reading). You can use the HTML5 Semantic Notepad tool as an aid in visualizing the <section> as well as other semantic elements in an HTML5 document, as shown in Figure 11.

Viewing an HTML5 document's semantic elements using the HTML5 Semantic Notepad

Despite the small amount of content in the demo article, there is an appropriate use for the <section> element in it. In the default.cshtml file, we will change the list of semantic elements to be in its own "section," as shown in Figure 12.

  1. <section>
  2.     <h1>A list of semantic elements</h1>
  3.     <ul>
  4.         <li>article</li>
  5.         <li>aside</li>
  6.         <li>figure</li>
  7.         <li>figcaption</li>
  8.         <li>footer</li>
  9.         <li>header</li>
  10.         <li>hgroup</li>
  11.         <li>nav</li>
  12.         <li>mark</li>
  13.         <li>section</li>
  14.         <li>time</li>
  15.     </ul>
  16. </section>

Because this content can be logically grouped together, it makes sense to put it in a <section>. This is especially the case when you have reason to provide a heading for the content. Other uses for a <section> could be to indicate sidebar content with its own heading or a grouping of links with its own heading.

When we run our example HTML5 one last time through the outliner site, we see a rich HTML5 outline generated as a result of the correct usage of semantic elements in the document, as shown in Figure 13.

Example HTML5 source outline after final set of code modifications

Make HTML Code More Meaningful

Semantic tags may not impact your users visually, but they provide meaningful data to modern parsers and search engines—so they can potentially have significant impact on your site's search-engine ranking and web traffic. I've shown you how, with a few adjustments, you can start using semantic elements now in your own websites. Get accustomed to using these new elements now—you'll be glad you did! In the next two articles of this HTML5 series, Dev Pro contributing author Dan Wahlin will show you how to effectively structure JavaScript code in HTML5 applications.