Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Collapse the Navigation on Small Screens

In this lesson, you folded the links in Kwame’s header into a Menu on small screens, and grouped two of them under a Borrowing sub-menu that opens only when a member activates it:

  • The problem. On a phone 320 pixels wide, the four links wrapped onto two lines under the site name. The header is sticky, so all three lines, 129 of the screen’s 640 pixels, stayed at the top.
  • Fold the links into a disclosure. Wrap the list in a <details> element with a <summary> that reads Menu, and keep the <nav> outside it, so the landmark keeps its name, Main. The header shrinks to one line, and opening Menu pushes the catalog down instead of covering it.
  • Keep the links on wide screens. A closed <details> hides everything after its summary with content-visibility: hidden on its ::details-content box. From 40rem up, hide the summary with display: none and set content-visibility back to visible. The links are back in a row and in the Tab order.
  • Check browser support. ::details-content and content-visibility are both Newly available, so check their support on MDN. A browser missing either one would still hide Menu but leave the links shut, so wrap the media query in @supports for both: that browser keeps Menu at every width, one click from the links.
  • Not on hover alone. A sub-menu shown only with :hover works with a mouse, but Tab goes from Catalog straight to Donate a Tool, because Tab never triggers hover. So give the sub-menu a control that opens it with a click, Enter or Space. 1.4.13 Content on Hover or Focus also asks that content shown on hover can be dismissed, and stays while you move onto it.
  • A button and a popover. Borrowing becomes a button whose popovertarget names the list’s id, and the list gets popover. It has the Options menus’ border and background, a focus outline in the dark green --color-accent that shows on its light background, and position-area: bottom, inside @supports, opens it under its button, its anchor.
  • Details stays open, a popover doesn’t. A click on the Catalog heading closes the Borrowing sub-menu by light dismiss, but Menu stays open, even after Escape. Watch out: a <details> element closes only when its summary is toggled, so keep it for menus that push the page down.
  • Check it by keyboard. On the phone, Tab reaches the skip link, the site name and Menu, and Enter opens it. Two more Tabs reach the Borrowing button. Space opens its sub-menu, and the next Tab moves into it. In Chromium, Shift+Tab inside it loops between its two links instead of going back to Borrowing. Your markup is fine, and Escape always gets you out. It closes the sub-menu, with focus back on Borrowing.
  • Check the states. In the Accessibility pane, Menu has the role DisclosureTriangle and Expanded true, and the Borrowing button has the role button, with Expanded true while its sub-menu is open. The browser works out both states. With your own screen reader, listen for Menu and Borrowing, each collapsed, then expanded.

On a small screen, fold the navigation into disclosures that open when a member activates them: <details> for a menu that pushes the page down, a button and a popover for one that covers it, and never hover alone. Here’s the navigation in catalog.html:

Language: HTML
<nav aria-label="Main">
  <details class="nav-toggle">
    <summary>Menu</summary>
    <ul class="main-nav">
      <li><a href="catalog.html" aria-current="page">Catalog</a></li>
      <li>
        <button type="button" class="sub-nav-button"
          popovertarget="borrowing-menu">Borrowing</button>
        <ul id="borrowing-menu" class="sub-nav" popover>
          <li><a href="how-borrowing-works.html">How Borrowing Works</a></li>
          <li><a href="my-reservations.html">My Reservations</a></li>
        </ul>
      </li>
      <li><a href="donate.html">Donate a Tool</a></li>
    </ul>
  </details>
</nav>

And the rules added to styles.css, starting with the sub-menu’s box styles, which the video doesn’t show:

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

00:00 Kwame’s header now has a navigation named Main, with a list of four links. In this video, you’ll fold them away on small screens, so they open only when a member asks.

00:11 Amara Okafor browses the catalog on her phone, 320 pixels wide. The four links wrap onto two lines under the site name. The header is sticky, so as she scrolls, all three lines stay at the top.

00:26 That’s 129 of her screen’s 640 pixels.

00:32 Kwame wraps the list in a details element, the same disclosure as the borrowing FAQ’s questions. Its summary reads Menu. The nav stays outside it, so the landmark keeps its name, Main.

00:45 Save, and on Amara’s phone the header shrinks to one line, with the site name and Menu.

00:53 She clicks Menu, and the links open under it. They push the catalog down instead of covering it.

01:00 In a wide window, though, the closed Menu hides the links too.

01:05 A closed details element hides everything after its summary with content-visibility: hidden. So, in a media query from 40rem up, Kwame hides the summary with display: none, and sets content-visibility back to visible.

01:22 Save, and in the wide window the four links are back in a row. Tab reaches each of them, as before. The ::details-content box and content-visibility are both newly available, so check their support on MDN.

01:38 A browser missing either one would still hide Menu, but leave the links shut. Wrap the media query in an @supports check for both. With it, that browser keeps Menu at every width, one click from the links.

01:54 Kwame now groups How Borrowing Works and My Reservations in a list under Borrowing. His first try shows it only while the pointer is over Borrowing, with the hover pseudo-class.

02:06 With a mouse, it works. With the keyboard, Tab goes from Catalog straight to Donate a Tool. Tab never triggers hover, so you never reach the two links. So give the sub-menu a control that opens it with a click, Enter or Space.

02:23 1.4.13 Content on Hover or Focus also asks that content shown on hover can be dismissed, and stays while you move onto it.

02:34 Borrowing becomes a button, and the list becomes its popover. The button’s popovertarget names the list’s id, as on the tool cards’ Options buttons. Inside an @supports check, position-area opens the list under its button, which is its anchor.

02:52 On the phone, with Menu open, Amara clicks Borrowing. Its two links open under it. Then she clicks the Catalog heading. The sub-menu closes by light dismiss, but Menu stays open.

03:07 She presses Escape, and Menu is still open. Watch out: a details element closes only when its summary is toggled, so keep it for menus that push the page down.

03:19 Reload on the phone, and press Tab three times, past the skip link and the site name, to Menu. Enter opens it. Tab twice more reaches Borrowing. Space opens its sub-menu, and the next Tab moves into it, to How Borrowing Works.

03:37 In Chromium, Shift+Tab loops between the two links instead of going back. Your markup is fine, and Escape always gets you out, back to Borrowing. In the Accessibility pane, Menu has the role DisclosureTriangle and Expanded true.

03:53 The Borrowing button’s Expanded turns true while its sub-menu is open. You didn’t write either state. The browser works them out. With your own screen reader, listen for Menu and Borrowing, each collapsed, then expanded.

04:10 On a small screen, fold the navigation into disclosures that open when a member activates them. Details suits a menu that pushes the page down. A popover suits one that covers it.

04:21 Never use hover alone.

04:25 Next, in Exercise: Build the Library Navigation, you’ll build Kwame’s navigation, with its skip link, its current page and a Menu disclosure.

Become a Member to join the conversation.