Navigation layouts
Overview
Navigation is the part of a layout people use most, and it has to work everywhere: a row of links on a desktop, something compact on a phone, with a keyboard, with a screen reader, and at 400% zoom. This lesson covers the layout patterns — and the accessibility details that make them work.
The header row
A site header is a one-dimensional row — a job for flexbox:
.site-header {
display: flex;
align-items: center;
gap: 1.5rem;
padding: 0.5rem 1rem;
}
.menu {
display: flex;
gap: 1.5rem;
margin-inline-start: auto; /* push the links to the right */
}margin-inline-start: auto absorbs all the free space before the menu, which puts the logo on the left and the links on the right. Using the logical property means right-to-left languages get the mirror image automatically.
Option 1: just let it wrap
If you have a handful of short links, the simplest responsive nav is no special mobile nav at all:
.menu { display: flex; flex-wrap: wrap; gap: 0.5rem 1.5rem; }On narrow screens, links wrap onto a second line. Every link stays visible and one tap away — often better for users than hiding them behind a button.
Option 2: a scrolling row
For more links (category tabs, section navigation), a horizontally scrolling row keeps them on one line:
.tabs {
display: flex;
gap: 1rem;
overflow-x: auto;
scroll-snap-type: x proximity;
scrollbar-width: thin;
}
.tabs a { flex: none; scroll-snap-align: start; }flex: none stops the links shrinking or wrapping their text. Give users a visual hint that more links exist (a fade on the edge, or a partly visible last item), since a scroll bar isn’t always shown on touch screens.
Option 3: the disclosure menu
When the full menu genuinely doesn’t fit, hide it behind a button on small screens. The markup matters more than the CSS:
<header class="site-header">
<a class="logo" href="/">Academy</a>
<button class="menu-button" aria-expanded="false" aria-controls="menu">Menu</button>
<ul id="menu" class="menu" hidden>…</ul>
</header>- A real
<button>is focusable and works with Enter and Space automatically. Don’t use a<div>or a checkbox hack. aria-expandedtells screen-reader users whether the menu is open;aria-controlslinks the button to the menu.- The
hiddenattribute hides the menu from everyone — sighted, keyboard and screen-reader users — until it’s opened. - A word like “Menu” next to (or instead of) a ☰ icon is clearer to more people than the icon alone.
A few lines of JavaScript toggle aria-expanded and hidden when the button is clicked.
The CSS
.site-header { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem 1.5rem; }
.menu-button { margin-inline-start: auto; }
.menu { flex-basis: 100%; display: grid; } /* open: a full-width list on its own line */
.menu[hidden] { display: none; }
@media (width >= 48rem) {
.menu-button { display: none; }
.menu, .menu[hidden] { display: flex; flex-basis: auto; margin-inline-start: auto; }
}Two subtle points:
displayoverrideshidden. Thehiddenattribute works through the browser’s default stylesheet ([hidden] { display: none }), and any author rule like.menu { display: grid }beats it. That’s why we restate.menu[hidden] { display: none; }— and why the wide-screen rule can deliberately show a menu that still hashiddenon it.flex-basis: 100%on the open menu forces it onto its own line below the logo and button, because the header wraps.
Touch targets
Make links in the mobile menu at least 44 × 44px — WCAG’s enhanced target size and the size Apple recommends:
.menu a { display: flex; align-items: center; min-block-size: 44px; }Keyboard and focus
- After opening, the first Tab should move into the menu. Placing the menu right after the button in the HTML does this naturally.
- Pressing Escape should close it and return focus to the button (a few lines of JavaScript).
- Never remove focus outlines; style them instead with
:focus-visible.
Breadcrumbs
Breadcrumbs are a wrapping row with separators:
.breadcrumbs ol { display: flex; flex-wrap: wrap; gap: 0.25rem 0.5rem; list-style: none; padding: 0; }
.breadcrumbs li:not(:last-child)::after { content: "/"; margin-inline-start: 0.5rem; color: #888; }Mark the current page with aria-current="page" on its link, and wrap the list in <nav aria-label="Breadcrumb">.
Sticky headers
.site-header { position: sticky; top: 0; z-index: 10; }
@media (height < 30rem) {
.site-header { position: static; } /* don't eat half the screen in landscape */
}A sticky header is useful on long pages, but on a short landscape phone screen it can cover a big part of the content. The height query turns it off when there isn’t room.
Mega menus and dropdowns
For dropdowns inside a desktop menu, anchor positioning (see Anchor positioning) or the Popover API handles placement and closing. The same disclosure-button principles apply: a <button> with aria-expanded for each dropdown, not a hover-only effect — hover menus don’t work on touch screens and are fiddly for people with motor difficulties.
Rule of thumb: show links when you can (wrap or scroll), hide them behind a real button only when you must, and test the menu with a keyboard, a screen reader and at 400% zoom.
A responsive site header
The starter includes a menu button and a small script that toggles the hidden attribute and aria-expanded. Write the CSS: logo on the left; on small screens the Menu button on the right and the menu, when open, below the header as a full-width vertical list. From 48rem, hide the button, always show the menu as a row of links pushed to the right, with 1.5rem gaps. Links must be at least 44px tall on small screens.
<header class="site-header">
<a class="logo" href="#">Academy</a>
<button class="menu-button" aria-expanded="false" aria-controls="menu">Menu</button>
<ul id="menu" class="menu" hidden>
<li><a href="#">Courses</a></li><li><a href="#">Pricing</a></li><li><a href="#">Blog</a></li><li><a href="#">About</a></li>
</ul>
</header>
<script>
const button = document.querySelector('.menu-button');
const menu = document.getElementById('menu');
button.addEventListener('click', () => {
const open = button.getAttribute('aria-expanded') === 'true';
button.setAttribute('aria-expanded', String(!open));
menu.hidden = open;
});
</script>
<style>
* { box-sizing: border-box; }
body { margin: 0; }
.site-header { background: #16181d; padding: 0.5rem 1rem; }
.site-header a, .menu-button { color: white; }
.menu-button { background: none; border: 1px solid white; padding: 0.5rem 1rem; }
.menu { list-style: none; margin: 0; padding: 0; }
/* your code here */
</style><header class="site-header">
<a class="logo" href="#">Academy</a>
<button class="menu-button" aria-expanded="false" aria-controls="menu">Menu</button>
<ul id="menu" class="menu" hidden>
<li><a href="#">Courses</a></li><li><a href="#">Pricing</a></li><li><a href="#">Blog</a></li><li><a href="#">About</a></li>
</ul>
</header>
<script>
const button = document.querySelector('.menu-button');
const menu = document.getElementById('menu');
button.addEventListener('click', () => {
const open = button.getAttribute('aria-expanded') === 'true';
button.setAttribute('aria-expanded', String(!open));
menu.hidden = open;
});
</script>
<style>
* { box-sizing: border-box; }
body { margin: 0; }
.site-header { background: #16181d; padding: 0.5rem 1rem; }
.site-header a, .menu-button { color: white; }
.menu-button { background: none; border: 1px solid white; padding: 0.5rem 1rem; }
.menu { list-style: none; margin: 0; padding: 0; }
.site-header {
display: flex;
flex-wrap: wrap; /* lets the open menu drop to its own line */
align-items: center;
gap: 0.5rem 1.5rem;
}
.menu-button { margin-inline-start: auto; }
.menu {
flex-basis: 100%; /* full width on its own line */
display: grid;
}
.menu[hidden] { display: none; }
.menu a {
display: flex;
align-items: center;
min-block-size: 44px;
}
@media (width >= 48rem) {
.menu-button { display: none; }
.menu,
.menu[hidden] { /* always visible on wide screens */
display: flex;
gap: 1.5rem;
flex-basis: auto;
margin-inline-start: auto;
}
}
</style>Summary
- A header is a flexbox row;
margin-inline-start: autopushes the menu or button to the far side. - On small screens, use a real
<button>witharia-expandedandaria-controlsto show and hide the menu. - CSS
displayoverrides thehiddenattribute, so restate[hidden] { display: none }when you style the menu. - Keep touch targets at least 44px, and make sure the menu works with the keyboard.
- Horizontally scrolling tab bars and breadcrumbs are good alternatives to hiding links.