Sub topic 8
Navigation that works on a small screen
This chapter assumes you can write semantic HTML, you know basic CSS (selectors, the box model, units), and you have already built a single-column base layout with min-width media queries. It covers how to design site navigation for a narrow viewport first, then enhance it for wider screens. Touch target sizing gets its own chapter, so here it appears only where it affects the patterns.
Why navigation is the hard part of small screens
On a desktop, a horizontal bar of eight links costs almost nothing. On a 360 px wide phone, the same bar would either wrap into several rows, shrink to unreadable text, or push the real content below the fold. Navigation competes with content for the same scarce space.
Mobile-first design treats this as a ranking problem. You already ranked your content by user need. Apply the same ranking to your links:
- Which links do most visitors need in their first visit?
- Which are rarely used, such as legal pages or archives?
- Which can live in the footer instead of the main menu?
A short, ranked list gives you more pattern options. A flat list of fifteen equal links leaves you only a hidden menu.
Choosing a pattern
Four patterns cover most sites. Pick by the number of links and how often people use them.
| Pattern | Best for | Main risk |
|---|---|---|
| Visible inline links | 2 to 4 top-level links | Wraps or overflows if a label grows |
| Scrollable or wrapping link row | 5 to 7 short links | Users may not notice hidden items off-screen |
| Disclosure menu (the "hamburger") | 5 or more links, secondary sections | Hidden links get used less |
| Bottom bar | App-like sites with 3 to 5 frequent actions | Collides with browser and OS controls at the screen edge |
A rule of thumb: if every link fits on screen with readable labels and adequate tap size, show them. Hiding navigation costs discoverability, so only hide what does not fit.
Start with the markup
Write navigation as a real list inside a nav element. This works with no CSS and no JavaScript, which is the mobile-first baseline: a working page first, enhancements after.
<header class="site-header">
<a class="site-logo" href="/">Northside Books</a>
<nav class="site-nav" aria-label="Main">
<ul>
<li><a href="/shop">Shop</a></li>
<li><a href="/events">Events</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
</header>The aria-label matters when a page has more than one nav, for example a footer navigation. Screen reader users can list landmarks and choose by name.
A small-screen base style
For a short list, the simplest working pattern is a row that wraps. The base rules need no media query.
.site-header {
display: grid;
gap: 0.5rem;
padding: 0.5rem 1rem;
}
.site-nav ul {
display: flex;
flex-wrap: wrap;
gap: 0.25rem 0.5rem;
margin: 0;
padding: 0;
list-style: none;
}
.site-nav a {
display: inline-flex;
align-items: center;
min-block-size: 44px;
padding-inline: 0.75rem;
}
@media (min-width: 48em) {
.site-header {
grid-template-columns: auto 1fr;
align-items: center;
}
.site-nav ul {
justify-content: flex-end;
}
}On narrow screens the logo sits above the links. From 48em the logo and links share one row. The min-block-size keeps each link comfortably tappable.
Wrapping is a legitimate answer for four or five short labels. It fails when translations make labels longer or when the list grows, so treat it as a starting point.
A disclosure menu without a framework
When the list is too long, collapse it behind a button. The button must be a real button element, because it needs keyboard focus and a role that assistive technology understands. A div with a click handler gives you neither.
<header class="site-header">
<a class="site-logo" href="/">Northside Books</a>
<button class="menu-toggle" type="button"
aria-expanded="false" aria-controls="primary-menu">
Menu
</button>
<nav class="site-nav" aria-label="Main">
<ul id="primary-menu" hidden>
<li><a href="/shop">Shop</a></li>
<li><a href="/events">Events</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
<li><a href="/archive">Archive</a></li>
<li><a href="/press">Press</a></li>
</ul>
</nav>
</header>const toggle = document.querySelector('.menu-toggle');
const menu = document.getElementById('primary-menu');
toggle.addEventListener('click', () => {
const open = toggle.getAttribute('aria-expanded') === 'true';
toggle.setAttribute('aria-expanded', String(!open));
menu.hidden = open;
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && !menu.hidden) {
toggle.setAttribute('aria-expanded', 'false');
menu.hidden = true;
toggle.focus();
}
});The script does three things. It flips aria-expanded so screen readers announce the state. It toggles the hidden attribute so the links leave the tab order when closed. It closes on Escape and returns focus to the button, so keyboard users do not lose their place.
Now the CSS. The mobile layout is the default. The wide layout shows the list and hides the button.
.menu-toggle {
min-block-size: 44px;
min-inline-size: 44px;
padding-inline: 1rem;
}
#primary-menu {
margin: 0;
padding: 0;
list-style: none;
}
#primary-menu a {
display: flex;
align-items: center;
min-block-size: 44px;
padding-inline: 1rem;
}
@media (min-width: 48em) {
.menu-toggle {
display: none;
}
#primary-menu[hidden] {
display: flex;
}
#primary-menu {
display: flex;
gap: 0.5rem;
}
}The #primary-menu[hidden] rule is the part people forget. The hidden attribute applies display: none, and the script leaves it in place when the page loads. On a wide screen you want the list visible regardless, so you override it inside the media query. Without that rule the desktop layout has no navigation at all.
How the states fit together
The menu has two states on small screens and one on wide screens. The diagram shows what moves between them.
Pattern notes and edge cases
Place the control where thumbs reach
Most people hold a phone in one hand and tap with the thumb. The top corners are the hardest areas to reach on a tall screen. A top-right menu button is still the most recognised location, so keep it there unless your own testing shows a problem. A bottom bar suits sites with a few frequent actions, but remember that it needs spacing from the system gesture area:
.bottom-bar {
position: fixed;
inset-inline: 0;
inset-block-end: 0;
display: flex;
justify-content: space-around;
padding-block-end: env(safe-area-inset-bottom);
background: #fff;
}The env(safe-area-inset-bottom) value keeps controls clear of the home indicator on phones that have one. It needs viewport-fit=cover in the viewport meta tag to take effect on some devices. Add matching bottom padding to the page body, or the bar will cover the last piece of content.
Label the button with text
An icon of three lines is widely understood but not universal. A visible word such as "Menu" removes the guesswork and costs little space. If you use only an icon, give the button an accessible name with aria-label.
Show the current page
Mark the active link with aria-current="page" and style it from that attribute:
.site-nav a[aria-current="page"] {
font-weight: 700;
text-decoration: underline;
}This gives screen reader users the same information as the visual style, and keeps the style and the meaning from drifting apart.
Nested menus
Dropdowns that open on hover cannot work on touch screens, because there is no hover. In a mobile-first build, show sub-items as an indented list inside the open menu, or give each parent its own disclosure button. If the menu goes more than two levels deep, the structure is the problem. Flatten it or move rarely used pages to the footer.
Long menus on short screens
When the menu is open on a phone in landscape, the list can be taller than the viewport. Let it scroll rather than clip:
#primary-menu {
max-block-size: calc(100dvh - 4rem);
overflow-y: auto;
}The dvh unit tracks the visible height as mobile browser toolbars appear and disappear, which vh does not.
Sticky headers
A fixed header keeps navigation in reach, but on a landscape phone it can consume a third of the visible height. If you use position: sticky, keep the header small, and test it in landscape.
Common errors
- Hiding the menu with
display: nonein CSS and opening it with a class, but not updatingaria-expanded. Screen readers report a stale state. - Using
visibilityoropacityto hide a closed menu while leaving it positioned off-screen. Keyboard users still tab through invisible links unless you also usehiddenorvisibility: hidden. - Writing the desktop bar first and removing it with
max-widthqueries. The result is hard to maintain and often leaves leftover rules active on mobile. - Forgetting the breakpoint override for the
hiddenattribute. The menu works on phones and disappears on desktops. - Choosing the breakpoint from a device width. Widen the window until the links start to crowd or wrap, and set the breakpoint there.
Exercise
A site uses the disclosure menu from this chapter. On a 1200 px wide desktop browser, the main navigation is missing entirely, although it opens correctly on a phone. The .menu-toggle button is correctly hidden at that width. What is the likely cause, and what is the fix?
Show answer
The ul still carries the hidden attribute from the HTML, and the browser's display: none for [hidden] wins over the wide-screen layout. Add a rule inside the min-width: 48em media query, such as #primary-menu[hidden] { display: flex; }, so the list shows on wide screens regardless of the attribute. Alternatively, let the script remove hidden when the viewport matches the wide breakpoint, using window.matchMedia.
Exercise
You have a site with six top-level links and a user study showing that "Contact" and "Shop" account for most clicks. Which navigation approach fits, and why?
Show answer
Show "Shop" and "Contact" directly in the header as visible links, and put the other four behind a "Menu" disclosure button. This keeps the most-used links one tap away while saving space, which follows the content ranking approach used across mobile-first design. A full hamburger menu for all six would hide the most important links for no space saving that matters.