Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 122 ยท Links and Navigation

Building Navigation Menus

Build accessible navigation with the nav element and lists. Mark the current page with aria-current, and add breadcrumbs, skip links, pagination and footer links.

Navigation is how people find their way around a site: the menu at the top, the trail of breadcrumbs, the page numbers at the bottom of a search result, the links in the footer. They all look different, but they are built from the same few pieces of HTML you already know: links and lists, wrapped in a <nav> element.

In this lesson you will build each kind of navigation with clean, accessible markup. The CSS is kept short; the focus is on getting the structure right, because that is what keyboard users, screen-reader users and search engines depend on.

The <nav> element

<nav> marks a section of major navigation links. Screen readers expose it as a navigation landmark, so users can jump straight to it, or skip past it, with a single keystroke.

HTML
<nav>
  <a href="/">Home</a>
  <a href="/menu">Menu</a>
  <a href="/about">About</a>
</nav>

Not every group of links needs a <nav>. Use it for the main blocks of navigation: the site menu, a table of contents, breadcrumbs, pagination. A couple of links inside a paragraph are just links.

When a page has more than one <nav>, give each a name with aria-label, so people can tell them apart in a list of landmarks:

HTML
<nav aria-label="Main">...</nav>
<nav aria-label="Breadcrumb">...</nav>
<nav aria-label="Footer">...</nav>

There's no need to include the word "navigation" in the label; screen readers already announce the role. You will learn more about landmarks in Semantic Elements.

A list-based menu

The most common pattern puts the links inside an unordered list. A menu is a list of options, and the list tells screen-reader users up front how many items it holds ("list, 4 items").

A horizontal site menu

Three CSS lines do most of the work: list-style: none removes the bullets, padding: 0 removes the list's indentation, and display: flex puts the items side by side. Making each link display: block with padding gives it a bigger, easier click target.

Removing bullets can remove the list. In Safari, VoiceOver may stop announcing a list as a list once you set list-style: none. Inside a <nav> this is usually fine, but if the item count matters elsewhere, adding role="list" to the <ul> restores it.

Marking the current page

Visitors should always know where they are. Add aria-current="page" to the link for the page that is currently displayed:

HTML
<li><a href="/recipes" aria-current="page">Recipes</a></li>

This does two jobs at once. Screen readers announce "current page" along with the link text, and the CSS selector a[aria-current="page"] lets you highlight it, as in the example above. Using one attribute for both means the visual highlight and the spoken information can never disagree. A plain class="active" only does the visual half.

Breadcrumbs show where the current page sits in the site's hierarchy, and let people climb back up. Since the order of the steps matters, an ordered list is the right fit:

Breadcrumb trail

The separators (/) are added with CSS ::before, not typed into the HTML. That keeps them out of the content, so screen readers don't read "slash" between every step. The last item is the current page; you can link it with aria-current="page" as shown, or leave it as plain text.

Keyboard users move through a page with Tab. If your header contains twenty links, they have to press Tab twenty times on every page before they reach the content. A skip link fixes that: it is the very first link on the page and jumps straight to the main content.

A skip link

The link sits off-screen until it receives keyboard focus, then slides into view. Don't hide it with display: none: that would remove it for keyboard users too. The tabindex="-1" on <main> makes sure focus actually moves there in every browser, so the next Tab continues from the main content. Keyboard Access and Focus explains focus management in more depth.

Pagination

Search results, product listings and blog archives are often split into pages. Pagination is another list of links in a labeled <nav>:

HTML
<nav aria-label="Pagination">
  <ul class="pagination">
    <li><a href="/blog/page/2" rel="prev">Previous</a></li>
    <li><a href="/blog/page/1">1</a></li>
    <li><a href="/blog/page/2">2</a></li>
    <li><a href="/blog/page/3" aria-current="page">3</a></li>
    <li><a href="/blog/page/4">4</a></li>
    <li><a href="/blog/page/4" rel="next">Next</a></li>
  </ul>
</nav>

Bare numbers are short, which is fine visually, but you can make them clearer for screen readers by adding hidden text such as "Page" before each number using a visually-hidden CSS class. rel="prev" and rel="next" describe the relationship between the pages. Keep the Previous and Next links in the same place on every page, so people can click repeatedly without hunting for them.

Footers often hold secondary links: legal pages, contact details, social profiles, a sitemap. Group them under headings so the structure is clear:

HTML
<footer>
  <nav aria-label="Footer">
    <h2>Company</h2>
    <ul>
      <li><a href="/about">About us</a></li>
      <li><a href="/jobs">Careers</a></li>
    </ul>

    <h2>Help</h2>
    <ul>
      <li><a href="/shipping">Shipping</a></li>
      <li><a href="/returns">Returns</a></li>
    </ul>
  </nav>
</footer>

If the footer only repeats a few small links, it may not need a <nav> at all. Ask yourself whether someone would want to jump to it as a landmark.

Keep menus consistent. Put navigation in the same place, in the same order, on every page. People learn your layout once and then navigate without thinking, and consistent navigation is also part of the accessibility guidelines most sites aim to meet.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A page has a main menu and a breadcrumb trail, both in nav elements. What should you add?

    Labels like aria-label="Main" and aria-label="Breadcrumb" let screen-reader users tell the landmarks apart.
  2. 2. Which attribute marks the link to the page currently displayed?

    aria-current="page" is announced by screen readers and can be targeted by CSS, so one attribute serves both purposes.
  3. 3. Why should the separators between breadcrumb steps come from CSS?

    Separators added with ::before are decoration. Typed into the HTML, they become content that is read between every step.
  4. 4. What is wrong with hiding a skip link using display none?

    display: none removes the element from the tab order. Move it off-screen instead and bring it back on :focus.

Summary

  • Wrap major blocks of navigation in <nav>, and label each one with aria-label when there are several.
  • Build menus as lists of links: <ul> for menus, <ol> for breadcrumbs where order matters.
  • Mark the current page with aria-current="page" and style it with the same attribute.
  • Add a skip link as the first focusable element so keyboard users can bypass the header.
  • Pagination and footer links are lists in labeled <nav> elements too.

Next lesson: URL Encoding, the rules for putting spaces and special characters into addresses.