Sub topic 8
Responsive navigation and touch targets
This chapter assumes you can write a semantic nav with a labeled list of links, you know the box model and media queries, and you can attach a click handler. It builds on the earlier chapters on navigation patterns and keyboard focus.
What changes on a small screen
A row of links that fits on a laptop will wrap or overflow on a phone. There are two common fixes:
- Keep a short list visible. Four or five top-level links can wrap onto a second row. This is the simplest option and needs no JavaScript.
- Collapse the list behind a menu button. Use this when there are too many links to show at once.
Whichever you choose, the same links must reach every user. Don't build a separate mobile menu with different content.
Touch targets
A fingertip is much less precise than a mouse pointer. WCAG 2.2 sets a minimum target size of 24 by 24 CSS pixels (success criterion 2.5.8, level AA). A target of 44 by 44 pixels is the stronger recommendation (2.5.5, level AAA). Aim for 44 pixels on mobile.
The clickable area is the link's box, not the text. Give the link display: inline-flex or block, then add min-height and padding. Adding margin between the links does not enlarge the targets.
Don't rely on hover. Touch screens have no reliable hover state, so anything that appears only on hover is unreachable there.
A collapsible menu
The menu button is a real button. Its aria-expanded attribute tells screen readers whether the menu is open, and aria-controls points to the nav it opens.
<header class="site-header">
<a href="/">Example Co.</a>
<button type="button" class="menu-toggle" aria-expanded="false" aria-controls="site-nav">Menu</button>
<nav id="site-nav" class="site-nav" aria-label="Primary">
<ul>
<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 CSS shows the full list by default. Only narrow screens collapse it, and only when JavaScript is running. The js class on the root element is how the stylesheet knows that.
.menu-toggle {
display: none;
min-width: 44px;
min-height: 44px;
}
.site-nav ul {
display: flex;
gap: 0.5rem;
margin: 0;
padding: 0;
list-style: none;
}
.site-nav a {
display: inline-flex;
align-items: center;
min-height: 44px;
padding: 0 0.75rem;
}
@media (max-width: 40em) {
.js .menu-toggle {
display: inline-block;
}
.js .site-nav:not(.is-open) {
display: none;
}
.site-nav ul {
flex-direction: column;
gap: 0;
}
}document.documentElement.classList.add('js');
const toggle = document.querySelector('.menu-toggle');
const nav = document.getElementById('site-nav');
toggle.addEventListener('click', () => {
const open = toggle.getAttribute('aria-expanded') !== 'true';
toggle.setAttribute('aria-expanded', String(open));
nav.classList.toggle('is-open', open);
});Common errors
- Hiding the menu with
display: noneby default, then relying on JavaScript to reveal it. If the script fails, users lose all navigation. - Using a
divor an icon-only element as the toggle. Abuttongets focus and keyboard activation for free, and it needs a text label such as "Menu". - Forgetting to update
aria-expanded, so a screen reader announces the wrong state. - Setting
height: 44pxinstead ofmin-height. Text that wraps or is enlarged by the user then overflows the target.
Exercise
A link in your mobile menu is 20 pixels tall, and the list has 1rem of margin between items. Does that meet the target size guidance, and how do you fix it?
Show answer
No. Margin between items adds space but does not enlarge the clickable area, so the target is still 20 pixels tall. Make the link a block or inline-flex box and give it min-height: 44px, with padding or alignment so the text sits comfortably inside.