Full answer
Start with the markup that works with no CSS: a nav that holds a list of links. A screen reader user can find it as a navigation landmark, and the links work with the keyboard.
On wide screens, show the links in a row. On narrow screens, hide the list and show a button that opens it. Rules for the button:
- Use a real
buttonwithtype="button", not a div. It has an accessible name, such as "Menu". aria-expanded="false"or"true"tells assistive technology whether the list is open.aria-controlscan point to the list id. Its support is uneven, so do not rely on it alone.- A small script toggles
aria-expandedand shows or hides the list. The CSS reads the attribute, so the visual and announced state stay in sync. - Escape should close the menu and return focus to the button.
Do not use role="menu" for a normal site navigation. That role is for application-style menus with arrow-key behaviour, and it changes how screen readers handle the content. A list of links inside a nav is the right pattern.
Hide the closed list with display: none on small screens, so its links are not focusable. Do not hide it with only opacity: 0 or by moving it off screen, because then keyboard users can still tab to invisible links.
Use min-width queries (mobile first), and show the button only below the breakpoint.
On a phone width the page shows only the Menu button. Clicking it shows the three links under it, and clicking again hides them. Escape closes the menu and puts focus back on the button. On wide screens the button is gone and the links are in a row.
Test with the keyboard only, and at several widths between the two layouts.
<style>
body { margin: 0; font-family: sans-serif; }
nav ul { list-style: none; margin: 0; padding: 0; display: none; }
nav ul a { display: block; padding: 8px 12px; }
#btn { padding: 8px 12px; }
#btn[aria-expanded="true"] + ul { display: block; }
@media (min-width: 600px) {
#btn { display: none; }
nav ul { display: flex; }
}
</style>
<nav aria-label="Main">
<button type="button" id="btn" aria-expanded="false" aria-controls="menu">Menu</button>
<ul id="menu">
<li><a href="/courses" id="first">Courses</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/help">Help</a></li>
</ul>
</nav>
<script>
const btn = document.getElementById("btn");
btn.addEventListener("click", () => {
btn.setAttribute("aria-expanded", String(btn.getAttribute("aria-expanded") !== "true"));
});
document.addEventListener("keydown", (e) => {
if (e.key === "Escape" && btn.getAttribute("aria-expanded") === "true") {
btn.setAttribute("aria-expanded", "false");
btn.focus();
}
});
</script>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Start with a nav and a list of links.
- A real button with aria-expanded controls the list.
- CSS can show the list with [aria-expanded="true"] + ul.
- Hide closed items with display: none so focus cannot reach them.
- Escape should close and return focus to the button.
Common mistakes
- Hiding the closed menu with opacity or off-screen positioning, so keyboard focus enters invisible links.
- Using role="menu" for ordinary site navigation.
- Not updating aria-expanded, so screen readers do not know the state.
Follow-ups
- Why use display: none for the closed list?
- What should happen to focus when the menu closes with Escape?
Tips
- Say that the markup is a normal nav with a list of links first, and the button is an enhancement.
- Mention why role=menu is the wrong role for site navigation.