Sub topic 4
Marking up navigation with nav, lists, and labels
This chapter assumes you know basic HTML and CSS, and that you are comfortable with links and lists. You will build the markup for a primary navigation. Styling, mega menus, and responsive behavior come in later chapters.
The working model
A navigation is a list of links. Three HTML pieces express that:
- The
<nav>element marks the block as a navigation landmark. A landmark is a region of the page that assistive technology can list and jump to. - A
<ul>with<li>items holds the links. Screen readers announce "list, 4 items", so users know how long the menu is before they read it. - An
aria-labelon the<nav>gives the landmark an accessible name.
The name matters once a page has more than one <nav>. A site with a header menu and a footer menu would otherwise expose two landmarks that are both called "navigation".
A primary navigation
<header>
<nav aria-label='Primary'>
<ul>
<li><a href='/' aria-current='page'>Home</a></li>
<li><a href='/products'>Products</a></li>
<li><a href='/pricing'>Pricing</a></li>
<li><a href='/support'>Support</a></li>
</ul>
</nav>
</header>
<main id='main'>
<h1>Home</h1>
</main>Here is what each part does:
<header>at the top level of the page is the banner landmark. The navigation sits inside it.aria-label='Primary'names the landmark. Screen readers read it as "Primary, navigation". Do not write "Primary navigation", because the role is announced for you and the word would be repeated.- Each
<li>holds exactly one link. The link text is the label users hear when they tab through. aria-current='page'marks the link for the page the user is on. A later chapter covers it in detail.<main>follows the navigation and holds the page content, with one<h1>.
Common errors
- Using
<div>or<span>for the wrapper. These have no landmark role, so screen reader users cannot jump to the menu. - Skipping the list. A row of bare links works visually. It loses the item count and the grouping that the list announces.
- Putting every group of links in a
<nav>. Reserve<nav>for major blocks such as the primary menu, the footer menu, or a table of contents. Too many landmarks make the landmark list noisy. - Giving two navs the same label. Two landmarks both named "Primary" cannot be told apart. Use distinct names such as "Primary" and "Footer".
- Using
<a>withouthref. It is not focusable and is not announced as a link. - Using
<button>for page links. Links go to a URL. Buttons run an action. Submenu triggers are the exception, and the disclosure chapter covers them.
Exercise
A page has a header menu and a footer menu, both wrapped in <nav> with no other attributes. What problem does this cause for screen reader users, and how do you fix it?
Show answer
Both landmarks are announced simply as "navigation", so users cannot tell them apart in the landmark list. Add distinct accessible names, for example aria-label='Primary' on the header nav and aria-label='Footer' on the footer nav.