Build the Site Navigation

Every tool card on Kwame’s catalog page now opens its Options menu where it belongs. Above the cards, though, the header has grown: Kwame has added How Borrowing Works and Donate a Tool beside Catalog and My Reservations, bringing the navigation to four links.

Members pass through the header on every page. In this lesson, you’ll build navigation that keyboard users can skip and screen reader users can jump to. It’ll announce how many links there are and show everyone which page they’re on. Start with how the links are grouped.

Rosa Delgado uses a screen reader. When she reaches the header, the <nav> element tells her she’s in a navigation landmark, named Main by its aria-label, and she can jump to it from anywhere on the page. Inside it, though, nothing says how many links there are or where the group ends.

A list fixes that. Her screen reader can report a list of four items and move past it in one step.

Open catalog.html from this lesson’s starting site in your live preview. Kwame also wants keyboard users to get past the header in one keypress, so he adds a skip link to <main> as the page’s first link. Replace the page’s <header> with this, and give the page’s <main> the attribute id="main":

Language: HTML
<a class="skip-link" href="#main">Skip to main content</a>
<header class="site-header">
  <a href="index.html">Ashgrove Tool Library</a>
  <nav aria-label="Main">
    <ul class="main-nav">
      <li><a href="catalog.html" aria-current="page">Catalog</a></li>
      <li><a href="how-borrowing-works.html">How Borrowing Works</a></li>
      <li><a href="donate.html">Donate a Tool</a></li>
      <li><a href="my-reservations.html">My Reservations</a></li>
    </ul>
  </nav>
</header>

The list brings browser margins, padding, and bullets. In styles.css, replace the .site-header nav rule with one for the list, and add the skip link’s rules, which keep it above the window until it has focus:

Language: CSS
.main-nav {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-s) var(--space-m);
  margin: 0;
  padding: 0;
  list-style: none;
}

.skip-link {
  position: absolute;
  z-index: 200;
  inset-block-start: var(--space-s);
  inset-inline-start: var(--space-s);
  padding: var(--space-s) var(--space-m);
  color: var(--color-text);
  background: var(--color-surface);
  translate: 0 -200%;
}

.skip-link:focus {
  translate: none;
}

Save the file. The header looks unchanged, because the change is in the structure that Rosa’s screen reader reads.

Take a Detour Through Menu Roles

Most people call this row of links the site’s menu, and ARIA has roles named menu and menuitem. Adding them looks like the accessible thing to do, so follow that idea and change the list in catalog.html to this:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.