Sub topic 8
Responsive navigation and touch targets
This chapter assumes you can write HTML and CSS, understand the box model and media queries, and handle basic JavaScript events. It also assumes you already have a semantic primary navigation, as built in the earlier chapters: a nav landmark with an accessible name, a list of links, a skip link, and aria-current on the current page.
A navigation that works with a mouse on a wide screen often fails on a phone. Links are too small to tap, the menu overflows, or a collapsed menu cannot be opened with a keyboard. This chapter covers how to adapt the navigation to small screens and touch input without losing what you built for keyboard and screen reader users.
What changes on small screens
Three things change when the viewport shrinks.
- Space. Four to eight top-level links fit in a row on a desktop. On a 360 px wide phone they do not.
- Input. A finger is much less precise than a mouse pointer. It also covers what it is pointing at.
- Hover. Touch screens have no reliable hover state. Anything that only appears on hover is unreachable for many users.
You have three common ways to deal with limited space. Pick one based on how many links you have and how important they are.
| Pattern | How it works | Good when | Watch out for |
|---|---|---|---|
| Wrap or stack | Links flow onto several lines, or stack vertically | Five or fewer short labels | Takes vertical space above the content |
| Horizontal scroll | One row that scrolls sideways | Many sibling items such as categories | Hidden items are easy to miss |
| Menu button (disclosure) | A button shows or hides the link list | Six or more links, or long labels | Adds one extra tap; needs correct ARIA state |
The menu button is the most common choice, and it is the one that needs the most care. The rest of this chapter focuses on it.
Start with the mobile layout
Write the small-screen styles first and add wider layouts with min-width media queries. This is called mobile-first. The base CSS stays simple, and the wide layout is an enhancement.
Make sure the page can scale to the device width. Without this tag, mobile browsers render a desktop-width page and shrink it:
<meta name="viewport" content="width=device-width, initial-scale=1">Do not add user-scalable=no or maximum-scale=1. They stop people from zooming, which breaks accessibility.
Sizing touch targets
A touch target is the area a finger can tap to activate a control. Two standards give you numbers:
- WCAG 2.2 success criterion 2.5.8 (Target Size, Minimum, level AA) requires targets of at least 24 by 24 CSS pixels, with some exceptions for spacing.
- WCAG 2.5.5 (Target Size, Enhanced, level AAA) asks for 44 by 44 CSS pixels.
Aim for 44 px in navigation. Links in a menu are the main way people move through your site, so this is worth the space.
The target is the whole clickable area, not only the visible text. Padding on the a element enlarges the target. Margin does not, because margin is outside the element.
nav a,
nav button {
display: block;
min-block-size: 44px;
padding: 0.75rem 1rem;
}min-block-size is the logical version of min-height. It works the same in horizontal writing modes. Using display: block makes the whole row clickable, not only the text.
Spacing matters too. If two small targets sit close together, people tap the wrong one. Stacked links with 44 px height and no gap are fine. Inline links in a row need enough horizontal padding.
A responsive navigation, step by step
The goal is a menu that is always a list of links on wide screens, and a button-controlled list on narrow screens. The same markup serves both.
The markup
<header>
<a href="/" class="logo">Example Co.</a>
<nav aria-label="Primary">
<button type="button" class="menu-toggle" aria-expanded="false" aria-controls="primary-list">
Menu
</button>
<ul id="primary-list" class="nav-list">
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/support">Support</a></li>
</ul>
</nav>
</header>The toggle is a real button, so it is focusable and responds to Enter and Space without any extra code. The aria-expanded attribute tells screen readers whether the list is open. The aria-controls attribute points to the list it controls.
The button text is "Menu". It does not change between "Open" and "Close", because aria-expanded already communicates the state. Changing the label as well would make a screen reader say the state twice, and sometimes contradict itself.
The CSS
.menu-toggle {
min-block-size: 44px;
min-inline-size: 44px;
padding: 0.5rem 1rem;
}
.nav-list {
list-style: none;
margin: 0;
padding: 0;
}
.nav-list a {
display: block;
min-block-size: 44px;
padding: 0.75rem 1rem;
}
/* Narrow screens: hide the list until the button opens it */
.menu-toggle[aria-expanded="false"] + .nav-list {
display: none;
}
/* Wide screens: show the list as a row, hide the button */
@media (min-width: 48rem) {
.menu-toggle {
display: none;
}
.menu-toggle[aria-expanded="false"] + .nav-list {
display: flex;
}
.nav-list {
display: flex;
gap: 0.5rem;
}
}The rule .menu-toggle[aria-expanded="false"] + .nav-list uses the adjacent sibling combinator. It ties the visual state to the ARIA state, so the two cannot drift apart. The wide-screen block must override that hiding rule, which is why it repeats the selector with display: flex.
The breakpoint is in rem, not px. A rem breakpoint responds to the user's font size setting. A person who enlarges text gets the collapsed layout sooner, which is what they need.
The JavaScript
const toggle = document.querySelector('.menu-toggle');
toggle.addEventListener('click', () => {
const open = toggle.getAttribute('aria-expanded') === 'true';
toggle.setAttribute('aria-expanded', String(!open));
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && toggle.getAttribute('aria-expanded') === 'true') {
toggle.setAttribute('aria-expanded', 'false');
toggle.focus();
}
});The script only flips one attribute. CSS does the showing and hiding. That keeps the logic small.
Escape closes the menu and returns focus to the button. Without that, focus would be left on a link that just disappeared.
Why `display: none` is the right way to hide
When a closed menu uses display: none, its links are removed from the tab order and from the accessibility tree. Keyboard users do not tab through invisible links, and screen readers do not announce them.
Avoid hiding the list by moving it off-screen with left: -9999px or by setting only opacity: 0. The links stay focusable, so keyboard focus disappears into content the user cannot see. If you want an animation, animate the open state, and still end in display: none or visibility: hidden for the closed state.
Common errors
- A menu that only works on hover. Touch devices cannot hover, so the menu never opens. Always provide a click or tap control.
- Targets under 24 px. Small icon-only buttons are the usual cause. Add padding, not just a larger icon.
- The toggle has no accessible name. A button containing only a hamburger icon needs text or an
aria-label. Visible text such as "Menu" is better, because it helps everyone. aria-expandedis not updated. The visual state changes, but the attribute does not. Screen reader users then hear the wrong state.- Hiding the toggle on wide screens but leaving the list collapsed. This happens when the base rule that hides the list is not overridden in the media query. Test at both widths.
- Fixed headers covering focus. A sticky header can cover a focused element when the page scrolls. WCAG 2.2 criterion 2.4.11 (Focus Not Obscured) addresses this. Use
scroll-padding-topon thehtmlelement equal to the header height. - Disabled zoom. Described earlier: never block pinch zoom.
Testing the responsive behavior
Test at the breakpoint and on both sides of it.
- Resize the browser from 320 px wide up to 1200 px wide. Nothing should overflow horizontally.
- At the narrow width, press Tab. Focus should land on the button, and the closed links should be skipped.
- Press Enter, then Tab. Focus should move into the first link.
- Press Escape. The menu should close and focus should return to the button.
- Zoom the page to 200 percent and increase the browser's default font size. The menu must still be usable.
- On a real phone, tap every link with a thumb. If you hit the wrong one, the spacing is too tight.
Browser developer tools can emulate touch, but they do not replace a real device.
Exercise
A menu toggle button is 32 px by 32 px, has an icon but no text, and its list is hidden by left: -9999px. Name three problems and give a fix for each.
Show answer
First, the target is 32 px, which passes the 24 px minimum but not the 44 px recommendation, so add padding or min-inline-size and min-block-size of 44 px. Second, an icon-only button has no accessible name, so add visible text such as "Menu" or an aria-label. Third, an off-screen list stays in the tab order, so keyboard focus moves to invisible links. Hide the closed list with display: none (or visibility: hidden) tied to aria-expanded="false".
Exercise
Why does the chapter use a min-width media query written in rem instead of a max-width query in px?
Show answer
A min-width query keeps the base CSS as the simple small-screen layout and adds the wide layout on top, so less code is needed for the common case. A rem value scales with the user's font size, so people who enlarge text get the collapsed menu sooner, before the links would overflow.