Sub topic 7
Make the bar accessible: landmarks, labels and focus
This chapter assumes you can write basic HTML and CSS selectors and that you have the navigation bar from the earlier chapters: a nav containing a ul of links, laid out with Flexbox. Here you check that the bar works for people who use a keyboard or a screen reader. A screen reader is software that reads a page aloud and lets users jump between its parts.
Landmarks give the page a map
A landmark is a region of the page that assistive technology can list and jump to. The nav element is the navigation landmark. A screen reader user can open a list of landmarks and go straight to the navigation without listening to the whole page.
You get this behavior from the element itself. Adding role="navigation" to a nav is redundant, so leave it out. Use that role only when you can't use a nav element, such as on a div you can't change.
Label each navigation landmark
A page often has more than one nav, such as a main menu and a footer menu. If none has a label, a screen reader announces "navigation" for each, and the user can't tell them apart. Give each one a short label with aria-label:
<nav aria-label="Main">
<ul class="nav-list" id="nav-list">
<li><a href="/" aria-current="page">Home</a></li>
<li><a href="/products">Products</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
</nav>The screen reader announces this as "Main navigation". Do not write "navigation" inside the label, because the role already supplies that word. Labels such as "Main" and "Footer" are enough. If a page has only one nav, a label is optional, but it does no harm.
The list structure is information
Because the links sit in a ul, a screen reader can announce "list, 4 items" and "2 of 4" as the user moves through. This tells the user how long the menu is. When you reset the list in CSS with list-style: none, some browsers, Safari with VoiceOver in particular, stop treating it as a list. If you need to keep list behavior in those browsers, add role="list" to the ul. Most sites don't need this.
Keyboard focus must stay visible
Keyboard users move between links with the Tab key. Focus is the state of the element that currently receives keyboard input. The user has to see where it is, so every focusable element needs a visible indicator.
The most common mistake is writing outline: none to remove the browser's default ring and giving nothing back. Keep an indicator, and style it with :focus-visible. That selector matches focus caused by the keyboard but usually not focus caused by a mouse click, so mouse users don't see a ring they didn't ask for.
.nav-list a:hover,
.nav-list a:focus-visible {
background: #374151;
}
.nav-list a:focus-visible {
outline: 2px solid #38bdf8;
outline-offset: -2px;
}The negative outline-offset draws the ring inside the link box. This matters when the bar sits at the edge of the page or inside a container with overflow: hidden, where an outer ring would be clipped.
Use the same attribute for sound and sight
The aria-current="page" attribute tells a screen reader that a link points to the page the user is on. It announces something like "Home, current page". You can also select on that attribute, so one source of truth drives both the announcement and the look:
.nav-list a[aria-current="page"] {
background: #111827;
border-bottom: 3px solid #38bdf8;
}A color difference alone doesn't reach everyone, including people with color blindness. The border adds a second, non-color cue.
Check the basics
- Use real
aelements with anhref. Adivwith a click handler is not reachable with Tab and has no link role. - Keep the visual order the same as the source order, so Tab moves in the order the eye reads. Flexbox properties such as
orderchange only the visual order and break this. - Check text contrast. White text (
#ffffff) on#1f2937is far above the 4.5:1 minimum for body text in WCAG, the Web Content Accessibility Guidelines. Check the hover background and any colors you add later. - Press Tab through the bar yourself. If you lose track of the focus, so will your users.
The dropdown and the mobile toggle in later chapters build on these rules. The submenu opens on :focus-within so keyboard users can reach it. The toggle button reports its state with aria-expanded.
Exercise
A page has a header menu and a footer menu, both in plain nav elements with no attributes. A screen reader user opens the landmark list. What do they hear, and what is the smallest change that fixes it?
Show answer
They hear "navigation" twice and can't tell which is which. Add a different aria-label to each, for example aria-label="Main" on the header menu and aria-label="Footer" on the footer menu. The screen reader then announces "Main navigation" and "Footer navigation".
Exercise
A teammate adds a:focus { outline: none; } because the ring looks ugly. Name the problem and give a safer rule.
Show answer
Keyboard users no longer see which link has focus. Replace it with a styled indicator on :focus-visible, for example a:focus-visible { outline: 2px solid #38bdf8; outline-offset: -2px; }.