Add a Skip Link and Hide Closed Menus

Focus on the choir’s site now has a clear ring, and the sticky header no longer covers it. Walking the home page in the first lesson also left a question open about where focus goes when you can’t see it. The header still causes two problems. On every page, keyboard users have to Tab through all of it first, and on a phone-sized screen, focus disappears for several presses.

In this lesson, you’ll add a link that lets keyboard users skip the header, keep the closed menu’s links out of the Tab order, and then check both with the keys people use. Start with the stops that stand before every page’s content.

Someone who uses the keyboard instead of a mouse, for example because fine hand movements are hard for them, reaches a page’s content only after the choir’s name and the four nav links. That’s five presses of Tab on every page before the content starts.

2.4.1 Bypass Blocks, at Level A, asks for a way to skip blocks that repeat on every page. For keyboard users, the usual answer is a skip link, the first link on the page, which points at the main content. Open index.html and add it as the first element in <body>, before the header:

Language: HTML
<body>
  <a class="skip-link" href="#main">Skip to main content</a>
  <header class="site-header">

The link’s href points at an id, so the target needs one. Give <main> that id, and a tabindex too:

Language: HTML
  <main id="main" tabindex="-1">

You met tabindex="-1" in the first lesson. It lets an element take focus, but Tab never stops on it. Why does <main> need it? Without it, following the link scrolls to <main>, and Chrome starts the next Tab from there, but focus doesn’t follow. It drops back to the page as a whole, and the skip link loses its focus ring.

With tabindex="-1", focus really lands on <main>, so your :focus-visible ring shows where you arrived. A value of 0 would work too, but it would add a pointless stop to every Tab walk.

Now hide the link until someone needs it. In the components layer of styles.css, add two rules:

Language: CSS
  .skip-link {
    position: absolute;
    inset-block-start: var(--space-s);
    inset-inline-start: var(--space-s);
    z-index: 2;
    padding: var(--space-s) var(--space-m);
    color: var(--color-link);
    background-color: var(--color-surface);
    transform: translateY(-200%);
  }

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

The transform lifts the link twice its own height, above the page. The moment it takes focus, .skip-link:focus cancels the move, and the link drops into view. Its z-index keeps it above the sticky header. Save, reload the page, and press Tab once:

The choir's home page with a white "Skip to main content" link showing over the left end of the dark header, circled by the red focus ring.
The Skip Link Appears on the First Tab

The link sits over the header with your focus ring around it. Press Enter to follow it. A ring now surrounds the whole main content, and the scroll-padding-top from the last lesson keeps its top edge clear of the header. Press Tab again, and focus goes to “See the program and dates”, the first link in the content. Every page has its own copy of the header, so give each one the same link and the same <main>.

If -1 against 0 still feels fuzzy, the button below opens the mentor to check your reasoning:

Hide the Closed Menu

On narrow screens, the nav becomes a menu behind a button, and its closed links cause the second problem. Open DevTools (Ctrl+Shift+I, or Cmd+Option+I on macOS), then turn on its device toolbar with Ctrl+Shift+M (Cmd+Shift+M). Choose Responsive, set the width to 390 pixels, and click the page so your keys reach it. The four links fold away behind a Menu button.

The menu is a popover. An element with the popover attribute stays closed until a button whose popovertarget names its id opens it, and Esc closes it again, with no script. Here’s the markup in the header:

Language: HTML
      <button class="menu-button" popovertarget="site-menu"
        aria-label="Menu">
        <!-- The three-line icon -->
      </button>
      <ul class="site-menu" id="site-menu" popover>
        <li><a href="index.html">Home</a></li>
        <li><a href="rehearsals.html">Rehearsals</a></li>
        <li><a href="concerts.html">Concerts</a></li>
        <li class="nav-join"><a href="join.html">Join</a></li>
      </ul>

The narrow-screen styles in the components layer of styles.css slide the menu in from the left:

Language: CSS
  @media (max-width: 39.99em) {
    .site-menu {
      display: flex;
      flex-direction: column;
      inset: 0 auto 0 0;
      inline-size: 16rem;
      border: none;
      background-color: var(--color-brand);
      transform: translateX(-100%);
      transition: transform 0.3s;
    }

    .site-menu:popover-open {
      transform: none;
    }
  }

The closed menu waits past the left edge, and :popover-open, which matches a popover while it’s open, slides it in. Moving content off screen hides it from sight, but the keyboard can still reach it. Reload the page and press Tab four times. Focus passes the skip link, the choir’s name, and the Menu button, and then nothing on the page shows a ring:

The choir's home page at phone width with the menu closed, after four presses of Tab. No link or button on the page has a focus ring.
Focus on a Link That's Off Screen

Focus is on the Home link, out of sight to the left. Three more presses go to Rehearsals, Concerts, and Join before the ring comes back on “See the program and dates”. That breaks 2.4.7 Focus Visible, at Level AA, which says keyboard users must be able to see where focus is.

Why are the closed links reachable at all? The browser’s own stylesheet gives a closed popover display: none, which would take its links out of the Tab order. But your stylesheet beats the browser’s, whatever the specificity.

The display: flex in .site-menu shows the closed menu, and transform only moves its box. The fix is to make the closed state hidden again. Replace the narrow-screen block with this one:

Language: CSS
  @media (max-width: 39.99em) {
    .site-menu {
      flex-direction: column;
      inset: 0 auto 0 0;
      inline-size: 16rem;
      border: none;
      background-color: var(--color-brand);
    }

    .site-menu:popover-open {
      display: flex;
    }

    .site-menu:not(:popover-open) {
      display: none;
    }
  }

Deleting display: flex from .site-menu would bring back the browser’s display: none. The :not(:popover-open) rule says it in your own stylesheet. :not() adds no weight itself, but the :popover-open inside it counts like a class. So the selector outweighs .site-menu, and a display added there later can’t bring the problem back.

The slide goes too, since an element appearing from display: none has no earlier position to move from. If Marjorie misses it, visibility: hidden on the closed state also keeps its links out of the Tab order and can let the slide play.

Leave the wide-screen query as it is. Its display: flex and position: static show the whole nav beside the choir’s name, closed popover or not, and those links belong in the Tab order. Don’t add the inert or hidden attribute to the menu, though. With no script, nothing could ever remove them, so the menu would stay out of reach.

Why can the skip link hide off screen when the menu can’t? The skip link comes into view the moment it takes focus, and the closed menu’s links never did. The mentor can quiz you on other ways of hiding things:

Walk the Header With the Keyboard

Now try both changes with the keyboard. Walk the home page twice, once at full width and once at 390 pixels, reloading before each walk.

At full width, check the skip link and the nav:

  1. Press Tab: The skip link appears with a ring.
  2. Press Tab five times, then Shift+Tab five times: The skip link hides as focus leaves it. The choir’s name and all four nav links take a visible ring, there and back, and the skip link reappears.
  3. Press Space: The link isn’t followed. Space presses buttons and may scroll the page, while Enter follows links.
  4. Press Enter, then Tab: The ring surrounds the main content, then moves to “See the program and dates”.

At 390 pixels, check the menu:

  1. Press Tab three times: Focus reaches the Menu button.
  2. Press Enter or Space: The menu opens.
  3. Press Tab, then Shift+Tab: Focus moves to Home in the menu, which comes right after its button, and back.
  4. Press Esc: The menu closes, and focus returns to the Menu button.
  5. Press Tab: Focus skips the closed menu and lands on “See the program and dates”.

With the menu open and focus on Home, the page looks like this:

At phone width, the open menu covers the left of the page, listing Home, Rehearsals, Concerts and Join, with a yellow focus ring around Home.
Focus Inside the Open Menu

With the menu open, press Tab past Join. Focus moves into the page while the menu stays open on top, so press Esc to close it.

Check Your Understanding

Try these before you open the answers:

  1. Why does the skip link’s target, <main>, get tabindex="-1" instead of 0?
  2. Someone moves the .site-menu:not(:popover-open) rule out of the narrow-screen query, to the top of the components layer. Predict what happens on a wide screen.
  3. Which keys follow the skip link, open the menu from its button, and close the menu?
  1. With -1, focus lands on <main> and its ring shows where you arrived, but Tab never stops there. A 0 would add a pointless stop to every walk.
  2. The whole nav disappears. On a wide screen, the menu is a closed popover, and .site-menu:not(:popover-open) outweighs the wide-screen query’s .site-menu, so display: none wins.
  3. Enter follows the skip link, Enter or Space opens the menu, and Esc closes it.

The skip link now lands on the main content, and the closed menu stays out of the Tab order while the wide-screen nav still shows.

Next comes the course’s first coding exercise, which works differently from your live preview. You edit the page’s HTML file, with its styles inside it, in the browser and click Run Tests, and automated tests check your HTML and CSS, with a message for each failure. Tests can’t press keys, so the keyboard walk stays yours. The Contents tab below the editor explains how coding exercises work.

In the exercise, Make the Rehearsals Page Keyboard Ready, you’ll get the page with the weekly schedule ready for keyboard users.

Become a Member to join the conversation.