One of the new elements for HTML 5 is the
<nav> element which allows you to group together links, resulting in more semantic markup and extra structure which may help screenreaders. In this article I’ll discuss how and where to use it as well as some reservations I have with the specifications definition.
At first, I thought the
<nav> element to be pretty simple. And whilst it still is very easy to use, I found the specification to not be overly helpful; a lot of decisions are left to the developer. Then on Monday Hixie made a change to the definition of
<nav> in the draft following a prompt from our very own Dr Bruce.
How to use it
You are probably used to using something like
<div id="nav"> <ul> <li><a.... etc
Well, for the sake of your markup, nothing much will change as you will now have something like this
<nav> <ul> <li><a href="index.html">Home</a></li> <li><a href="/about/">About</a></li> <li><a href="/blog/">Blog</a></li> </ul> </nav>
The HTML 5 specification defines
The nav element represents a section of a page that links to other pages or to parts within the page: a section with navigation links. Not all groups of links on a page need to be in a nav element only sections that consist of major navigation blocks are appropriate for the nav element. In particular, it is common for footers to have a list of links to various key parts of a site, but the footer element is more appropriate in such cases, and no nav element is necessary for those links.
The key phrase there is “major navigation” (previously primary navigation).
Looking at the example they give –
“In the following example, the page has several places where links are present, but only one of those places is considered a navigation section.”
I have to say that this is no use to me. There are six items of navigation in the header element there. But only three are in the nav tag. There is no explanation as to what differentiates the first three links with the second three links – both go to different pages and all are in internal to that current site.
They also give a second example:
<body> <h1>The Wiki Center Of Exampland</h1> <nav> <ul> <li><a href="/">Home</a></li> <li><a href="/events">Current Events</a></li> ...more... </ul> </nav> <article> <header> <h1>Demos in Exampland</h1> <nav> <ul> <li><a href="#public">Public demonstrations</a></li> <li><a href="#destroy">Demolitions</a></li> ...more... </ul> </nav> </header> <section id="public"> <h1>Public demonstrations</h1> <p>...more...</p> </section> <section id="destroy"> <h1>Demolitions</h1> <p>...more...</p> </section> ...more... <footer> <p><a href="?edit">Edit</a> | <a href="?delete">Delete</a> | <a href="?Rename">Rename</a></p> </footer> </article> <footer> <p><small>© copyright 1998 Exampland Emperor</small></p> </footer> </body>
This is a little more helpful as I find the biggest isse of the
<nav> element is deciding which sets of links should be classed as major navigation.
Where to use it?
If you take a peek at the source code for this site, there are three uses (Ed. or four if you’re on a page that includes pagination) of the nav elements – we used it on the main navigation and above the footer (both are exactly the same links by the way). We also decided to use it on the skip links that are included for accessibility.
Looking at the content of the site, it could be argued that the
<nav> element could be used on the Recent Articles and Categories listings in the sidebar. Should you do it? Honestly, I think it is hard to say from looking at the spec – but I would say these are not “major navigation” but it would tempting to do, particularly if you’re analytics suggest these areas of navigation are ‘major’ ways in which your users navigate your site.
Other Possible Uses
Below are a few more examples of other areas of the site in which you might consider using the
<nav> element. It is also important to note that while XHTML 2
<nl> element, this hasn’t been replicated in HTML 5 because navigation does not have to take list form, as we’ll see.
- Table of Contents
I would say definitely yes to that – it is primary navigation for that particularly content
- Previous/next buttons (or pagination)
I would say yes to this because it is important to the overall structure and hierarchy of the blog/site
- Search form
For me, a definite yes, but it is not mentioned in the spec. A search form is hugely important to the navigation of a site, particularly large sites which rely almost solely on their search engine.
Again, I would say yes to this as well. Although breadcrumbs are not always necessary and can be used when not applicable, on large sites a breadcrumb trail can be an important navigation aid.
The difference of nav from menu
If you aren’t aware there is another element that can confuse the issue in the HTML 5 specification – menu. I’ve noticed that some developers are using the
<menu> element for navigation rather than the
<nav> element. We thought it best to clarify that
<menu> is to be used for a list of commands and is an interactive element and more likely to be used exclusively in Web Applications. We will be covering more about the
<menu> element in a later post.
Only with the help of the community, and hopefully a much clearer spec, can we be sure when and perhaps more importantly, when not to use the