Sub topic 9
Adapting navigation from mobile to desktop
This chapter assumes you know basic HTML and CSS, including the box model, flexbox, and min-width media queries from the earlier chapters. It also assumes you have used a frame-based design tool such as Figma. Navigation is the part of a layout that changes most between the mobile, tablet, and desktop frames, so it needs its own plan.
Why navigation needs its own plan
A site with six top-level links fits in one row on a 1280px desktop. On a 360px phone, the same links wrap, shrink to unreadable sizes, or push the page wider than the screen. A layout can stack columns and still work, but navigation has to change what it looks like and how it behaves.
The goal is not to make the desktop bar smaller. The goal is to give each frame the pattern that suits its width and its input method. The usual progression looks like this:
| Frame | Typical pattern | Why it fits |
|---|---|---|
| Mobile | Toggle button that opens a menu, or a bottom tab bar | Little horizontal room, and thumbs reach the lower part of the screen |
| Tablet | Condensed horizontal bar, or a toggle with a wider panel | Room for a few links, but touch targets still need to be large |
| Desktop | Full horizontal bar, sometimes with dropdowns or a sidebar | Room for all links, and a precise pointer is usually available |
These are starting points. As the previous chapters argued, the width at which the pattern should change comes from the content. If your longest link label is long, your bar will break earlier than another site's.
The diagram shows the key idea: there is one list of links in the HTML. CSS decides how it is presented. Avoid writing two separate navigation blocks, one for phones and one for desktops. Duplicated markup means duplicated links to keep in sync, and screen readers may announce both.
Start with semantic markup
Build the navigation as a nav element containing a list. Do this before any styling, because the structure has to work at every width.
<header class="site-header">
<a class="logo" href="/">Acme</a>
<button
class="nav-toggle"
type="button"
aria-expanded="false"
aria-controls="site-nav">
Menu
</button>
<nav id="site-nav" class="site-nav" aria-label="Main">
<ul>
<li><a href="/products">Products</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/docs">Docs</a></li>
<li><a href="/blog">Blog</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
</header>Three details matter here:
- The
buttonis a real button, so it is focusable and works with the keyboard without extra code. aria-expandedtells assistive technology whether the menu is open. Your script must update it.aria-controlspoints at theidof the element the button opens.
Mobile first: the collapsed menu
In a mobile-first stylesheet, the base rules describe the narrow layout. The menu is hidden until the button opens it.
.site-header {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
padding: 0.5rem 1rem;
}
.nav-toggle {
min-height: 2.75rem;
min-width: 2.75rem;
padding: 0 1rem;
}
.site-nav {
display: none;
flex-basis: 100%;
}
.site-nav.is-open {
display: block;
}
.site-nav ul {
list-style: none;
margin: 0;
padding: 0;
}
.site-nav a {
display: block;
padding: 0.75rem 0;
}The min-height: 2.75rem keeps the touch target near 44px at the default font size. Guidelines for touch targets commonly recommend about 44px, and WCAG 2.2 sets a minimum of 24px with spacing rules. Making the whole link display: block with vertical padding makes the full row tappable, not just the text.
A small script for the toggle
The toggle needs a few lines of JavaScript. This example is complete and runs as shown when placed after the markup above.
const toggle = document.querySelector(".nav-toggle");
const nav = document.querySelector("#site-nav");
toggle.addEventListener("click", () => {
const isOpen = nav.classList.toggle("is-open");
toggle.setAttribute("aria-expanded", String(isOpen));
});
document.addEventListener("keydown", (event) => {
if (event.key === "Escape" && nav.classList.contains("is-open")) {
nav.classList.remove("is-open");
toggle.setAttribute("aria-expanded", "false");
toggle.focus();
}
});The Escape handler closes the menu and returns focus to the button. Without that, a keyboard user who presses Escape loses their place.
Desktop: reveal the full bar
At the breakpoint where the links fit in one row, add a min-width query. It hides the toggle and shows the list horizontally. Because .site-nav is display: none in the base styles, the query must override that too, whether or not the menu was opened on mobile.
@media (min-width: 48em) {
.nav-toggle {
display: none;
}
.site-nav {
display: block;
flex-basis: auto;
}
.site-nav ul {
display: flex;
gap: 1.5rem;
}
.site-nav a {
padding: 0.5rem 0;
}
}Here 48em is a placeholder. Choose the value by widening the browser until your real links stop fitting comfortably, then set the breakpoint just above that width. If you add a seventh link later, the breakpoint may need to move, which is why the chapter on choosing breakpoints recommends recording values as design tokens.
The tablet in-between
Tablets are where simple patterns fail. At 768px you may have room for four of your six links but not all of them. You have three realistic options:
- Keep the toggle menu until the full bar fits. This is the simplest and often the right answer. A tablet does not have to get its own pattern.
- Show the most important links in the bar and put the rest behind a "More" button. This needs a second disclosure control and a decision about what counts as important.
- Use a different layout, such as a side rail, at tablet widths. This is more design work and more states to test.
Whichever you choose, the tablet frame in your design tool should show it explicitly. If the mobile and desktop frames show different patterns and the tablet frame is missing, developers will guess, and the guess usually breaks between 600px and 1000px.
Dropdowns and the hover problem
Desktop navigation often includes dropdown submenus that open on hover. Hover does not exist on most touch screens, and on hybrid devices a laptop with a touch screen has both. The input-capability queries from an earlier chapter apply here.
.has-submenu .submenu {
display: none;
}
.has-submenu.is-open .submenu {
display: block;
}
@media (hover: hover) and (pointer: fine) {
.has-submenu:hover .submenu {
display: block;
}
}The click-driven .is-open state works everywhere. Hover is added as an enhancement only where the device can hover. Also open the submenu on keyboard focus, using :focus-within, so it is reachable without a mouse:
.has-submenu:focus-within .submenu {
display: block;
}A parent item that is both a link and a submenu trigger is a trap on touch screens: the first tap should open the submenu, but the browser follows the link. A common fix is to make the parent a button that opens the submenu, and put the parent's own page as the first item inside it.
Alternatives to the hamburger menu
A collapsed menu hides navigation behind one tap, so people use it less than visible links. For sites with few top-level destinations, other patterns can work better on mobile:
- Bottom tab bar. Three to five icons with labels fixed to the bottom edge. It is within thumb reach and always visible. It suits app-like products and does not suit sites with many sections.
- Horizontally scrolling bar. All links stay visible, and the user swipes to see more. Clip the last item at the edge so it is obvious that more exist.
- Priority-plus. The bar shows as many links as fit and moves the rest into a "More" menu. It adapts continuously but needs JavaScript to measure widths.
Pick the pattern from the number of destinations and how often people switch between them. Five frequently used sections favor a tab bar. Twenty rarely visited ones favor a menu.
Common errors
- Duplicate navigation in the HTML. Two navs doubles the maintenance and the screen reader output. Use one list and restyle it.
- Toggle visible on desktop with no function. The base styles show the button, and the desktop query forgets to hide it.
- Menu hidden by desktop CSS only. If the
.is-openclass was added on mobile and the user then widens the window, the desktop styles must still produce the right layout. Test by opening the menu and resizing. - Stale
aria-expanded. Toggling a class without updating the attribute leaves screen reader users with the wrong state. - Tiny touch targets. Links with no padding are hard to tap even when they look fine.
- Breakpoint chosen from a device. Using 768px because it is a tablet width ignores your own link labels.
Testing navigation between the breakpoints
Navigation breaks in the gaps. Test these cases:
- Drag the viewport from 320px to 1400px slowly and watch the bar. Look for wrapping, overflow, and the first width where the desktop bar appears.
- Open the mobile menu, then widen the window past the breakpoint, then narrow it again.
- Tab through every link with the keyboard at mobile, tablet, and desktop widths. The focus ring should always be visible and never land on a hidden item.
- Zoom the page to 200% at desktop width. Zoom shrinks the effective viewport, so users who zoom often see your mobile pattern on a large monitor.
- Test with a real touch device, or use device emulation, to confirm dropdowns open on tap.
The zoom case is easy to miss. It is a good reason to design the mobile navigation as a complete, polished pattern and not only as a fallback.
Exercise
A site's navigation has six links. At 700px wide the bar wraps onto two lines, and at 900px it fits on one line. A teammate proposes a breakpoint at 768px because that is the iPad portrait width. What would you do instead, and why? Also name one thing the script must update when the mobile menu opens.
Show answer
Test the real links and set the breakpoint just above the width where they fit on one line, which here is somewhere between 700px and 900px. Find the exact value by widening the viewport until the wrapping stops, and add some margin for longer translated labels. The 768px figure comes from a device, not from the content, so it may leave the bar wrapping or switch to the desktop bar too late. When the menu opens, the script must set aria-expanded to "true" on the toggle button (and back to "false" when it closes), so assistive technology reports the correct state.
Exercise
A dropdown submenu opens only through .parent:hover. List two groups of users who cannot open it and give one change that fixes both.
Show answer
Touch-screen users, who have no hover, and keyboard users, who cannot hover with a pointer. Add a click or tap toggle that sets an open class, and also show the submenu with :focus-within, keeping the hover rule only inside @media (hover: hover) and (pointer: fine) as an enhancement.