Sub topic 12
Add a mobile menu toggle button
This chapter adds a menu button for narrow screens. It assumes you can write basic HTML and CSS selectors and have the navigation bar from the earlier chapters: a nav landmark, a flex-based .nav-list, hover and focus styles, and a media query that stacks the links. You also need to be comfortable reading a few lines of JavaScript.
Why a toggle is needed
On a phone, a row of links either wraps awkwardly or takes up much of the screen. A common fix is to hide the links behind a button labeled "Menu". The reader taps the button to open and close the list.
The button needs to do two jobs:
- Show or hide the list visually.
- Tell assistive technology, such as a screen reader, whether the menu is open.
Both jobs are handled by one click handler, so the two states cannot drift apart.
Step 1: Add a real button
Add a button element before the list, inside the nav. Use a real button, not a div or a link. A button is focusable and can be activated with Enter and Space, so keyboard support comes for free.
<button class="nav-toggle" aria-expanded="false" aria-controls="nav-list">Menu</button>Two attributes describe the button to assistive technology:
aria-expandedis"false"while the menu is closed and"true"while it is open. A screen reader announces this as "collapsed" or "expanded".aria-controlsholds theidof the element the button controls. Here that isnav-list, which matches theidon theulfrom the first chapter.
In the full page, the structure looks like this:
<nav aria-label="Main">
<button class="nav-toggle" aria-expanded="false" aria-controls="nav-list">Menu</button>
<ul class="nav-list" id="nav-list">
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>Step 2: Show the button only on small screens
On wide screens the links are already visible, so the button should be hidden. The media query shows it only below a breakpoint, stacks the links, and hides the list until it is opened.
.nav-toggle {
display: none;
}
@media (max-width: 40rem) {
.nav-toggle {
display: block;
}
.nav-list {
display: none;
flex-direction: column;
}
.nav-list.is-open {
display: flex;
}
}The base rule hides the button. Inside the media query, display: block overrides it. The list is hidden with display: none, and the is-open class turns it back into a flex container. Because .nav-list.is-open is more specific than .nav-list, it wins when both apply.
Step 3: Wire up the click handler
The script flips the aria-expanded attribute and the is-open class in the same handler.
const toggle = document.querySelector('.nav-toggle');
const list = document.getElementById('nav-list');
toggle.addEventListener('click', () => {
const isOpen = toggle.getAttribute('aria-expanded') === 'true';
toggle.setAttribute('aria-expanded', String(!isOpen));
list.classList.toggle('is-open', !isOpen);
});Step by step:
- Read the current state from
aria-expanded. Attribute values are strings, so compare with'true'. - Write the opposite value back.
String(!isOpen)converts the boolean to"true"or"false". - Call
classList.togglewith a second argument. Passing!isOpenforces the class on or off instead of flipping it blindly, so the class always matches the attribute.
Common errors
- The menu never opens. The
aria-controlsvalue or the script'sgetElementByIdargument does not match the list'sid. Check that both saynav-list. - The menu is stuck open or closed after the first click. The script compares against
trueinstead of'true', or the initialaria-expandedis missing. Start with"false"in the HTML. - The menu disappears on a wide screen after being closed on a narrow one. This happens if you hide the list with
display: noneoutside the media query. Keep the hiding rule inside the query so wide screens always show the links. - The button looks unstyled. Browsers give buttons their own font and border. Add a background, color, border and padding so it matches the bar.
Check your understanding
Exercise
In the click handler, why is list.classList.toggle('is-open', !isOpen) safer than list.classList.toggle('is-open') with no second argument?
Show answer
With one argument, the class flips based on whatever it currently is. If the class and the aria-expanded attribute ever disagree, for example because another script changed one of them, they stay out of sync. Passing !isOpen sets the class from the same value that was written to aria-expanded, so the visual state and the announced state always match.
Exercise
A user reports that on a desktop-width window the links are missing after they opened and closed the menu on a narrow window and then widened it. Which CSS rule is the likely cause, and how do you fix it?
Show answer
The rule that hides the list, .nav-list { display: none; }, is probably outside the media query, or the script writes an inline display style. Keep display: none for .nav-list inside the @media (max-width: 40rem) block, and toggle only the is-open class so wide screens always show the flex row.