Sub topic 9
Adapting navigation from mobile to desktop
This chapter assumes you know the box model, flexbox or grid basics, and how mobile-first min-width media queries work. It also assumes you can build frames in a design tool such as Figma. Navigation is usually the component that changes most between frames, so it is worth planning on its own.
Why navigation changes
On a phone there is room for a logo and one or two controls. On a desktop there is room for every top-level link. The same list of links therefore needs two presentations:
- Mobile frame: links hidden behind a menu button, shown as a panel or drawer.
- Desktop frame: links laid out in a horizontal row, always visible.
The markup stays the same. Only the CSS changes at a breakpoint.
Start with the mobile version
Write the collapsed state as the base style. The button toggles the hidden state of the list with a small script.
<nav aria-label="Main">
<button class="nav-toggle" aria-expanded="false" aria-controls="nav-list">
Menu
</button>
<ul id="nav-list" class="nav-list" hidden>
<li><a href="/">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
<script>
const toggle = document.querySelector(".nav-toggle");
const list = document.querySelector("#nav-list");
toggle.addEventListener("click", () => {
const open = toggle.getAttribute("aria-expanded") === "true";
toggle.setAttribute("aria-expanded", String(!open));
list.hidden = open;
});
</script>The aria-expanded attribute tells screen readers whether the panel is open. aria-controls links the button to the list it controls.
Switch to the desktop layout
At the breakpoint, hide the button and show the list as a row. The display: flex rule overrides the hidden attribute's default styling, so the list stays visible without changing the script.
.nav-list {
display: grid;
gap: 0;
padding: 0;
list-style: none;
}
.nav-list a {
display: block;
padding: 0.875rem 1rem; /* large touch target */
}
@media (min-width: 48em) {
.nav-toggle {
display: none;
}
.nav-list,
.nav-list[hidden] {
display: flex;
gap: 0.5rem;
}
.nav-list a {
padding: 0.5rem 0.75rem;
}
}The .nav-list[hidden] selector matters. If someone closes the menu on a narrow screen and then widens the window, the hidden attribute is still set. Without that selector the links would stay invisible on desktop.
Choosing the pattern
Not every site should use a menu button. Pick the pattern from the number and importance of the links.
| Pattern | Best for | Trade-off |
|---|---|---|
| Menu button with panel | Five or more links, or nested sections | Links are one tap further away |
| Always-visible short row | Two to four links | Runs out of room as links are added |
| Bottom tab bar | Three to five primary app-style destinations | Takes permanent screen height |
| Horizontally scrolling row | Many peer categories | Links off screen are easy to miss |
Pick the breakpoint from the content, not from a device. The right point is where the full row of links, at its real text length, stops fitting on one line. If you add a link or translate labels into a longer language, that point moves.
Plan it in your frames
In the design tool, build the navigation as a component with two variants, one for the mobile frame and one for the tablet and desktop frames. Draw the open state of the mobile variant as its own frame. Reviewers often skip it, and it is where most problems appear, such as a panel covering the page or no visible close control.
Exercise
A user opens the mobile menu, closes it, and then rotates the phone to a landscape width above the breakpoint. The links are gone. Which CSS rule is probably missing, and why does it fix the problem?
Show answer
The .nav-list[hidden] selector inside the min-width media query is probably missing. Closing the menu sets the hidden attribute on the list, and the browser's default style for hidden is display: none. Adding .nav-list[hidden] { display: flex; } at the breakpoint overrides that default, so the links show on wide screens whatever the open or closed state was.