Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 1. New Structural Elements in HT... > Beginner Recipe: Creating Navigation...

Beginner Recipe: Creating Navigation with the nav Element

The nav element, as you might expect from its name, is for navigational content. It is used to link to other pages within the site or to other parts of the page (a table of contents, for example).

The most common use of a nav is for the main navigation on a website. It is common practice to use an unordered list to code navigation, as shown in Listing 1.4.

Listing 1.4. Traditional Way of Marking Up Navigation

<ul id="nav">
  <li><a href="#">Home</a></li>
  <li><a href="#">About</a></li>
  <li><a href="#">Meet the team</a></li>
  <li><a href="#">News</a></li>
  <li><a href="#">Contact</a></li>
</ul>

This code does not change too much when creating nav elements in HTML5. The code for Figure 1.5 would be something like that shown in Listing 1.5.

Figure 1.5. Sitewide navigation that would be inside a nav element


Listing 1.5. Navigation Markup in HTML5

<nav>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Meet the team</a></li>
    <li><a href="#">News</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</nav>

You can put nav in the header as well, as shown in Listing 1.6, because the header allows for introductory and navigational content. However, it does not have to be in the header, and sometimes its placement might depend on styling issues. It is also quite common to see a navigation menu in the footer of a page, sometimes duplicating the main site navigation.

Listing 1.6. The nav Element Inside a header Element

<header>
  <h1>My super HTML5 site</h1>
  <nav>
    <ul>
      <li><a href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">News</a></li>
      <li><a href="#">Contact us</a></li>
    </ul>
  </nav>
</header>

It is not necessary to put all links on a page in a nav element. The HTML5 specification warns that only blocks of “major navigation” are considered appropriate for the nav element.

On news or blog sites, it is common to see a sidebar with links to articles and other pages. The markup in Listing 1.7 is used to produce the element shown in Figure 1.6.

Figure 1.6. Example of grouped navigation in a sidebar. “Shared,” “Read,” and “Watched/Listened” would each be in a nav.


Listing 1.7. Multiple Navigation Groups in a Single nav Element

<nav>
  <h2>Shared</h2>
  <ul>
    <li><a href="#">Pellentesque habitant</a></li>
    <li><a href="#">Morbi tristique senectus</a></li>
    <li><a href="#">Aenean ultricies mi vitae est</a></li>
  </ul>
  <h2>Read</h2>
  <ul>
    <li><a href="#">Pellentesque habitant</a></li>
    <li><a href="#">Morbi tristique senectus</a></li>
    <li><a href="#">Aenean ultricies mi vitae est</a></li>
  </ul>
  <h2>Watched/Listened</h2>
  <ul>
    <li><a href="#">Pellentesque habitant</a></li>
    <li><a href="#">Morbi tristique senectus</a></li>
    <li><a href="#">Aenean ultricies mi vitae est</a></li>
  </ul>
</nav>

Notice that there is an h2 to separate groups of links in the nav. In Figure 1.6, the h2 tags can be used as tab headings, so when a heading is selected by the user, the content switches (this effect can be achieved with JavaScript). A heading element is not always necessary but should be used to break up and structure navigation groups when possible. For styling reasons, you may need to separate the previous example into two nav structures, which is also fine.

There is a big accessibility win when using the nav element. Assistive technology, such as screen readers, will be able to search and immediately use groups of navigation rather than waiting for them to appear on-screen. Traditionally, developers have used “skip” or “jump” links as the very first things in an HTML document, and they are usually links to the main navigation or main content. However, using the nav element means you will soon be able to drop such “skip” menus. The only problem is that currently assistive technologies have limited support for HTML5 elements. However, they will soon catch up.

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint