Sub topic 6
Making the menu toggle accessible
This chapter assumes you can write basic HTML, use flexbox and media queries, and attach JavaScript event listeners. It builds on the hamburger menu from the previous chapter. That menu works with a mouse and a finger. Here you make it work with a keyboard and a screen reader too.
What an accessible toggle must do
A menu toggle is a control that shows and hides content. Assistive technology (software such as a screen reader that reads the page aloud) needs three facts about it:
- What it is. A real
<button>is announced as a button and can be activated with Enter or Space. - What it does.
aria-controlspoints to theidof the element it shows or hides. - Its current state.
aria-expandedis"true"when the menu is open and"false"when it is closed.
ARIA is a set of HTML attributes that add meaning for assistive technology. It changes what is announced. It does not change how the page behaves, so your JavaScript must keep the attributes accurate.
Start with a real button
The markup below is the one from the hamburger chapter.
<meta name="viewport" content="width=device-width, initial-scale=1">
<header>
<a href="/">Site name</a>
<button id="menu-toggle" type="button" aria-expanded="false" aria-controls="site-menu">Menu</button>
<nav aria-label="Main">
<ul id="site-menu" hidden>
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/docs">Docs</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
</header>Three details matter here:
type="button"stops the button from submitting a form if it is ever placed inside one.- The visible text "Menu" gives the button its accessible name. If you use an icon-only button, add
aria-label="Menu". - The
hiddenattribute removes the list from the page, from the tab order, and from the accessibility tree (the structure that screen readers read). Hidden links cannot be focused by accident.
A <div> with a click handler fails all three tests. It cannot be focused, it ignores Enter and Space, and it has no role.
Keep the state and the visibility in sync
The script flips aria-expanded and hidden in the same handler. It also adds Escape handling.
const toggle = document.getElementById('menu-toggle');
const menu = document.getElementById('site-menu');
toggle.addEventListener('click', () => {
const isOpen = toggle.getAttribute('aria-expanded') === 'true';
toggle.setAttribute('aria-expanded', String(!isOpen));
menu.hidden = isOpen;
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && !menu.hidden) {
toggle.setAttribute('aria-expanded', 'false');
menu.hidden = true;
toggle.focus();
}
});The click event fires for mouse, touch, Enter, and Space on a native button, so one listener covers every input method.
isOpen is read from the attribute, not from a separate variable. This keeps a single source of truth. If the attribute and the menu ever disagree, a screen reader user hears the wrong state.
Keyboard behavior and focus
Keyboard users move with Tab and leave a menu with Escape. The script handles Escape in three steps:
- It checks that the menu is open.
- It closes the menu and updates
aria-expanded. - It calls
toggle.focus(), so the user returns to the button that opened the menu instead of losing their place.
Without the last step, focus can land on an element that is now hidden. The browser then resets it to the top of the page, and the user has to tab through everything again.
The tab order follows the DOM order. In the markup, the button comes before the list, so Tab moves from the button into the first link when the menu is open. Do not use positive tabindex values to change this. Fix the source order instead.
Make sure the wide layout still works
On wide screens the toggle is not needed, so the CSS hides the button and shows the list even if hidden is set.
#site-menu {
list-style: none;
margin: 0;
padding: 0;
}
#site-menu a {
display: block;
padding: 0.75rem 1rem;
}
@media (min-width: 48em) {
#menu-toggle {
display: none;
}
#site-menu,
#site-menu[hidden] {
display: flex;
gap: 1rem;
}
}A button with display: none is removed from the accessibility tree, so wide-screen users do not meet a control that does nothing. The #site-menu[hidden] selector overrides the browser's default hiding rule for the hidden attribute.
Common errors
| Error | Effect | Fix |
|---|---|---|
| `aria-expanded` never updated | Screen reader always says "collapsed" | Set it in the same handler that changes `hidden` |
| Toggle is a `<div>` or `<span>` | No keyboard access, no role | Use a `<button>` |
| Menu hidden only with `opacity: 0` | Invisible links still receive focus | Use `hidden` or `display: none` |
| No Escape handling | Keyboard users must tab back to close | Add the keydown listener and refocus the button |
| Icon button with no text | Announced only as "button" | Add visible text or `aria-label` |
Try it
Open your menu page and put your mouse aside. Press Tab until the button has focus, press Enter to open the menu, tab through the links, then press Escape. Focus should return to the button. Then turn on a screen reader and listen for "Menu, button, collapsed" changing to "expanded".
Exercise
A user opens the menu with the keyboard, tabs to the "Docs" link, and presses Escape. Which two attribute changes does the script make, and where should focus end up?
Show answer
It sets aria-expanded to "false" on the toggle and sets the hidden attribute on the menu list. Focus moves to the toggle button, so the user does not lose their place after the focused link disappears.
Exercise
A teammate hides the closed menu with opacity: 0 and pointer-events: none instead of hidden. Name one accessibility problem this causes.
Show answer
The links stay in the tab order and the accessibility tree. Keyboard users can focus invisible links, and screen readers still read them even though the menu is closed.