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 withcontent-visibility: hiddenon its::details-contentbox. From40remup, hide the summary withdisplay: noneand setcontent-visibilityback tovisible. The links are back in a row and in the Tab order. - Check browser support.
::details-contentandcontent-visibilityare 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@supportsfor both: that browser keeps Menu at every width, one click from the links. - Not on hover alone. A sub-menu shown only with
:hoverworks 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
popovertargetnames the list’sid, and the list getspopover. It has the Options menus’ border and background, a focus outline in the dark green--color-accentthat shows on its light background, andposition-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:
<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: