Sub topic 8
Navigation that works on a small screen
This chapter assumes you know semantic HTML (nav, ul, a), CSS selectors and the box model, and how to open browser developer tools. It builds on content prioritisation: the navigation should expose the destinations you ranked highest and hide the rest.
Why navigation is a content decision
A phone screen about 360 CSS pixels wide cannot hold a desktop menu with eight or nine links in a row. You have to decide which destinations most users need, and how to present the others. Ranking the links is the same exercise you did for page content.
Three patterns cover most sites:
| Pattern | Best for | Cost |
|---|---|---|
| Visible list of links | 2 to 5 top-level destinations | Uses vertical space |
| Disclosure menu (the "hamburger") | 6 or more destinations | Links are hidden until the user opens the menu |
| Bottom bar | 3 to 5 core actions in an app-like site | Competes with browser controls and fixed-position quirks |
If your site has only a few links, show them. A hidden menu adds a tap and hides options, so use one only when the links will not fit.
Start with a plain list
The mobile-first base is a normal list of links, stacked in a column. It needs no JavaScript and works everywhere.
<nav class="site-nav" aria-label="Main">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/support">Support</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>.site-nav ul {
list-style: none;
margin: 0;
padding: 0;
}
.site-nav a {
display: inline-flex;
align-items: center;
min-block-size: 44px;
min-inline-size: 44px;
padding-inline: 1rem;
}The min-block-size rule gives each link a tappable area of at least 44 CSS pixels. The next chapter covers touch targets in more detail.
Adding a menu that collapses
When the list grows too long, you can hide it behind a button. A native details element does this without JavaScript, and the browser handles keyboard and screen reader behaviour.
<nav class="site-nav" aria-label="Main">
<details>
<summary>Menu</summary>
<ul>
<li><a href="/products">Products</a></li>
<li><a href="/support">Support</a></li>
<li><a href="/pricing">Pricing</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
<li><a href="/blog">Blog</a></li>
</ul>
</details>
</nav>Here the list is closed by default, and the summary acts as the toggle. Give it the same minimum size as the links so it is easy to tap.
On wider screens the menu should simply be visible. Because details hides its content when closed, you need a small amount of script or markup changes to show it permanently at large widths. A common compromise is to keep the details pattern at every width for large menus. For a short menu, use the plain list from the previous section and switch to a horizontal row with a min-width query:
@media (min-width: 40em) {
.site-nav ul {
display: flex;
gap: 0.5rem;
}
}This follows the mobile-first rule: the base is the stacked column, and the query adds the row only when there is room.
Common errors
- Hiding everything. Placing every link inside a menu, including the two most-used ones, makes common tasks slower. Keep the top one or two destinations visible.
- Using only hover. Touch screens have no hover, so a dropdown that opens on
:hoveris unreachable. Open menus on tap or click, or usedetails. - Tiny close controls. A toggle smaller than about 44 CSS pixels causes mis-taps.
- No accessible name. If a page has more than one
nav, give each anaria-labelso screen reader users can tell them apart. - Fixed bars covering content. A fixed bottom bar can hide the last lines of a page unless you add matching bottom padding.
Exercise
A site has three top-level links: Home, Shop and Contact. A teammate proposes putting them all behind a hamburger menu on phones. What would you do instead, and why?
Show answer
Keep the three links visible as a stacked or horizontal list. Three links fit on a small screen, and a hidden menu adds an extra tap and hides the destinations. Use a collapsible menu only when the links no longer fit.
Exercise
Why is a dropdown that opens on :hover a poor choice for a mobile-first navigation?
Show answer
Touch screens have no reliable hover state, so users may be unable to open the dropdown. Opening on tap or click, or using a details element, works for touch, keyboard and mouse.