Sub topic 5
Navigation inside the header
This chapter covers the primary navigation inside the site header. It assumes you know basic HTML tags and attributes, basic CSS selectors and properties, and how to inspect a page with your browser's developer tools. Earlier chapters introduced semantic elements and the header. Later chapters cover the hero, layout details, and a full treatment of accessibility. Here the focus is on the nav element and the list of links inside it.
What navigation is for
Navigation answers the visitor's question "where can I go from here?" A good primary navigation does three jobs:
- It lists the few destinations most visitors need.
- It shows where the visitor currently is.
- It works with a mouse, a touch screen, a keyboard, and a screen reader.
The navigation belongs inside the header because it is part of the site's orientation. The logo or site name says who you are dealing with. The navigation says what the site offers.
The nav element
The nav element marks a block of major navigation links. Browsers expose it to assistive technology as a navigation landmark. A landmark is a named region that a screen reader user can jump to directly from a list of regions.
Do not wrap every group of links in nav. Use it for major blocks such as the primary menu, a footer site map, or a table of contents. A row of "related articles" links or a single "Read more" link does not need it. Too many landmarks make the landmark list noisy, which defeats its purpose.
Marking up the links as a list
Navigation links are a list of items, so mark them up as one. The skeleton from the page-structure chapter does this:
<header>
<a href="/">Acme Bakery</a>
<nav aria-label="Primary">
<ul>
<li><a href="/menu">Menu</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>
</header>Using ul and li has practical benefits. Screen readers announce "list, 3 items", so the visitor knows how long the menu is before reading it. The structure also gives CSS a clean hook for turning the list into a horizontal row.
Each destination is a real a element with an href. Do not use a div or button with a click handler for page-to-page navigation. A real link supports middle-click to open in a new tab, copying the address, prefetching, and the Enter key, all without extra script.
Accessible names for navigation landmarks
A page often has more than one nav. The header has the primary menu, and the footer may have a second one. If both are unnamed, a screen reader lists two landmarks that are both called "navigation". The aria-label attribute gives each one a distinct accessible name:
<nav aria-label="Primary">...</nav>
<nav aria-label="Footer">...</nav>Screen readers then announce "Primary navigation" and "Footer navigation". Two guidelines apply:
- Do not include the word "navigation" in the label. The role is already announced, so
aria-label="Primary navigation"is read as "Primary navigation navigation". - Keep labels short and distinct. "Primary", "Footer", and "Breadcrumb" are enough.
If a visible heading already names the region, you can point to it with aria-labelledby instead. A single nav on the page may omit the label, but adding one is harmless.
Styling the menu as a row
The header uses flexbox. Flexbox arranges items along one axis, which suits a logo on one side and a menu on the other. The inner list is also a flex container so its items sit in a row:
header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
}
header nav ul {
display: flex;
gap: 1.5rem;
list-style: none;
margin: 0;
padding: 0;
}Three details are worth noting:
list-style: noneremoves bullets. Some browsers, notably Safari with VoiceOver, stop announcing an unstyled-looking list as a list when its bullets are removed. If you want the list semantics preserved everywhere, addrole="list"to theul. This is one of the rare cases where adding a redundant role is deliberate.margin: 0andpadding: 0remove the default indentation that browsers apply to lists.gapspaces the items without needing margins on eachli, so no stray space appears after the last item.
Making links easy to hit
Links in a menu are small targets by default. Put the padding on the a element, not on the li, so the whole padded area is clickable:
header nav a {
display: block;
padding: 0.75rem 0.5rem;
color: inherit;
text-decoration: none;
}
header nav a:hover,
header nav a:focus-visible {
text-decoration: underline;
}Aim for a target at least 24 by 24 CSS pixels, which is the minimum in WCAG 2.2, and ideally about 44 by 44 for touch. The padding above on a 16px font gives a height of about 48px.
Never remove the focus outline without replacing it. The :focus-visible selector shows a style only when the browser decides focus should be visible, which in practice means keyboard use. Removing outline and styling nothing in its place leaves keyboard users unable to tell where they are.
Showing the current page
A visitor should be able to see which page they are on. Mark the matching link with aria-current="page":
<ul>
<li><a href="/menu" aria-current="page">Menu</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>Screen readers announce "current page" after the link text. You can also use the attribute as a styling hook, which keeps the visual state and the announced state in sync:
header nav a[aria-current="page"] {
font-weight: 700;
border-bottom: 2px solid currentColor;
}Do not rely on color alone for this state. Bold weight or an underline works for visitors who cannot distinguish the colors.
Skip links work with navigation
Keyboard users move through links with the Tab key. If the header has ten links, they must tab through all ten on every page before reaching the content. A skip link, placed as the first element in the body, solves this:
<body>
<a class="skip-link" href="#main">Skip to main content</a>
<header>
<nav aria-label="Primary">...</nav>
</header>
<main id="main">
...
</main>
</body>The link is hidden off-screen until it receives focus, then slides into view:
.skip-link {
position: absolute;
left: 0.5rem;
top: -3rem;
padding: 0.5rem 1rem;
background: #fff;
color: #000;
}
.skip-link:focus {
top: 0.5rem;
}Do not hide it with display: none or visibility: hidden. Those remove it from the tab order, so nobody could reach it. The chapter on accessibility covers this in more detail.
How the pieces fit together
The diagram shows how the navigation sits in the page and what each part gives a visitor.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Menu links built from `div` or `span` with click handlers | No keyboard access, no open-in-new-tab | Use `a` with `href` |
| Two `nav` elements with no labels | Screen reader shows two identical landmarks | Add distinct `aria-label` values |
| `aria-label="Primary navigation"` | Announced as "navigation navigation" | Use `aria-label="Primary"` |
| `outline: none` with no replacement | Keyboard focus is invisible | Style `:focus-visible` |
| Padding on `li` instead of `a` | Only the text is clickable | Put padding on the `a` |
| Current page shown by color only | Invisible to some visitors and to screen readers | Use `aria-current="page"` plus a non-color cue |
| Skip link hidden with `display: none` | It can never receive focus | Move it off-screen with positioning |
Edge cases
- Many links. A primary menu with more than about seven items is hard to scan and hard to fit on a narrow screen. Group items under fewer headings, or move secondary links to the footer.
- Narrow screens. A horizontal row of links may overflow. The responsive chapter covers collapsing it. A simple fallback is
flex-wrap: wrapon theul, which lets items flow to a second line instead of overflowing. - Dropdown menus. Submenus add complexity: they need keyboard operation, correct state attributes, and a way to close them. If you can avoid them with a flatter structure, do. When you cannot, use a button with
aria-expandedto toggle the submenu, not a hover-only trigger. - The logo link. The site name in the header is a link to the home page. It sits outside the
navin the skeleton. That is fine, because the home link is already identified by its text and position. - Link text. Each label should make sense out of context, since screen reader users can list all links on a page. "Menu" and "Contact" work. "Click here" does not.
A final callout on the most common source of landmark noise:
Practice
Exercise
A page has a primary menu in the header and a site map in the footer, both marked up with nav. A screen reader user hears two landmarks, both announced as "navigation". What change fixes this, and what label text would you avoid?
Show answer
Give each nav a distinct accessible name, for example aria-label="Primary" on the header nav and aria-label="Footer" on the footer nav. Avoid including the word "navigation" in the label, such as "Primary navigation", because the role is already announced and the user would hear "navigation" twice.
Exercise
You hide a skip link with display: none and reveal it on :focus by setting display: block. Why does this fail, and how should you hide it instead?
Show answer
Elements with display: none are removed from the tab order, so the link can never receive keyboard focus and the :focus rule never applies. Hide it visually by positioning it off-screen, for example position: absolute; top: -3rem;, and bring it into view with top: 0.5rem; on :focus.
Exercise
Write the HTML attribute and one CSS rule that mark and style the "About" link as the current page.
Show answer
Add aria-current="page" to the link: <a href="/about" aria-current="page">About</a>. Then style it with header nav a[aria-current="page"] { font-weight: 700; border-bottom: 2px solid currentColor; }. The attribute tells screen readers, and the bold weight and border show it visually without relying on color.