Sub topic 5
The hamburger menu
This chapter assumes you know semantic HTML, flexbox and media queries, and how to select elements and handle events in JavaScript. You should also know why a page needs the viewport meta tag. The chapter builds one hamburger menu in three layers: markup, behavior, and layout.
What the pattern does
A hamburger menu hides the main navigation links behind a button on small screens. Tapping the button shows the links. On wide screens the button disappears and the links show as a normal row.
The pattern frees space on a narrow screen. Its cost is that hidden links are used less than visible ones, so it suits sites with many links of similar importance. Later chapters compare it with other patterns.
The same markup serves both layouts. You do not build one menu for phones and another for desktops.
Step 1: Markup
Start with a semantic nav and a real button. A button gives you keyboard focus, Enter and Space activation, and the correct role for free. A div with a click handler gives you none of these.
<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 attributes carry the meaning:
aria-controls="site-menu"names the list the button operates.aria-expanded="false"reports the current state. Screen readers announce it as "collapsed" or "expanded".hiddenon the list removes it from view and from the accessibility tree, so keyboard users do not tab through links they cannot see.
Step 2: Behavior
The script has two jobs. It flips the state when the button is clicked, and it lets Escape close the menu.
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 handler reads the current state from aria-expanded rather than keeping a separate variable. The attribute is the single source of truth, so the visible state and the announced state cannot drift apart. It changes aria-expanded and hidden in the same handler. If you update only one, sighted users and screen reader users see different menus.
aria-expanded is a string, not a boolean. The values are the text "true" and "false", which is why the code compares with === 'true' and wraps the new value in String().
The Escape handler calls toggle.focus() after closing. Without it, focus would sit on a link that has just been hidden, and keyboard users would lose their place. The next chapter covers focus in more detail.
Step 3: Layout
On a phone, the browser's default rendering for the list is enough: hidden when closed, a vertical stack when open. The CSS adds comfortable spacing, then switches to a row at a wider breakpoint.
#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;
}
}The display: block and padding make each link a large tap target, not just the width of its text. The breakpoint uses em, so it scales with the user's font size setting.
The selector #site-menu[hidden] is the key detail. The hidden attribute is styled by the browser as display: none, and an author rule with the same or higher specificity overrides it. At 48em and wider, this rule forces the list to display even if a script left hidden set, for example when the user opens the menu on a narrow window and then widens it. The toggle is hidden at that width, so the user could never close it, and the menu stays visible as it should.
Common errors
| Mistake | Result |
|---|---|
| Using a `div` or `span` as the toggle | No keyboard focus and no button role |
| Changing `hidden` but not `aria-expanded` | Screen readers announce the wrong state |
| Forgetting `type="button"` | Inside a form, the button submits it |
| Wide-screen rule missing `[hidden]` | Menu stays hidden on desktop after a narrow-window visit |
| Omitting the viewport meta tag | Phones render a zoomed-out desktop page and the media query never matches as intended |
Exercise
A user opens the menu on a narrow window, then resizes the browser past 48em. The script never ran again, so the list still has the hidden attribute set. Which CSS selector makes the links appear anyway, and why is it needed?
Show answer
The selector is #site-menu[hidden], inside the min-width: 48em media query, set to display: flex. The browser's built-in rule for [hidden] is display: none, so without a matching author rule the list would stay invisible on desktop. At that width the toggle button is also hidden, so the user could not reopen it.
Exercise
Why does the click handler read aria-expanded to find the current state instead of checking menu.hidden or storing a boolean variable?
Show answer
Reading aria-expanded keeps one source of truth that is also what assistive technology announces. If the handler trusted a separate variable, the variable and the attribute could disagree after some other code changed one of them, and screen reader users would hear a state that does not match the screen.