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 bar from the earlier chapters: a nav containing a ul of links, laid out with Flexbox and styled for hover and the current page. Here you will check that people using a keyboard or a screen reader can use that bar as easily as someone with a mouse.
What accessibility means for a navigation bar
A screen reader is software that reads a page aloud or sends it to a braille display. It does not see your layout. It reads the page's accessibility tree, which the browser builds from your HTML elements and attributes. Keyboard users move through the page with Tab, Shift+Tab and Enter, and never touch a pointer.
Most of the work is already done if you used semantic elements. The remaining work is to name things clearly, keep focus visible and avoid CSS that breaks the semantics. This chapter covers four areas:
- Landmarks, which let users jump to the navigation.
- Labels, which tell users which navigation they are in.
- Focus, which shows keyboard users where they are.
- Hidden content, which controls what the accessibility tree contains.
Landmarks
A landmark is a region of the page that assistive technology can list and jump to. The nav element creates a navigation landmark. header, main and footer create other landmarks, and main is the one you can rely on for page content. Screen reader users often open a list of landmarks and pick one, much as sighted users scan a page.
A div with a class of nav creates no landmark. A nav element does, with no extra attributes.
Use nav for major blocks of navigation links, such as the main menu, a footer link group or a table of contents. Do not wrap every group of links in one. A landmark list full of navigation regions is as hard to use as no landmarks.
Labeling the landmark
Many pages have more than one nav. A screen reader listing them would announce "navigation, navigation", which tells the user nothing. Give each one a distinct accessible name 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>
<nav aria-label="Footer">
<ul>
<li><a href="/privacy">Privacy</a></li>
<li><a href="/terms">Terms</a></li>
</ul>
</nav>Screen readers announce these as "Main navigation" and "Footer navigation". The role name is added for you, so do not write aria-label="Main navigation", or users will hear "Main navigation navigation".
If a visible heading already names the region, you can point to it with aria-labelledby and the heading's id instead of repeating the text in aria-label. For a main menu with no visible heading, aria-label is the simpler choice.
What the list markup gives you
When a screen reader enters your ul, it typically announces "list, 4 items". Each link is then announced with its position, such as "1 of 4". This gives users a sense of the menu's size before they read it. It is the main reason to use a list rather than a row of loose links.
Resetting the bullets with list-style: none can have a side effect. Some versions of Safari with VoiceOver treat an unstyled list as plain content and stop announcing it as a list. The usual workaround is to restore the role explicitly:
<ul class="nav-list" id="nav-list" role="list">This is a rare case where a redundant ARIA role is justified. Test in your target browsers before deciding it is needed.
Marking the current page
The previous chapter used aria-current="page" to style the active link. The attribute also has an accessibility job: a screen reader announces the link as "current page", so a user who cannot see the highlight still knows where they are.
Two rules apply:
- Put
aria-current="page"on exactly one link per navigation list. - Do not rely on color alone for the visual style. The example uses a 3px bottom border as well as a darker background, so users who cannot tell the two background shades apart still see the marker.
If you generate pages from a template, set the attribute on the server or in the build step, not with JavaScript after load. That way it is correct before any script runs.
Keyboard focus
Focus is the element that currently receives keyboard input. Pressing Tab moves focus to the next focusable element in document order. Links with an href and real button elements are focusable by default, so your bar is keyboard-operable as long as you did not break it.
The order of focus follows the order of elements in the HTML, not their position on screen. Flexbox properties such as flex-direction: row-reverse and order change how items look without changing the tab order. If the two disagree, focus appears to jump around. Keep the HTML in the order you want users to move through it.
Show where focus is
Browsers draw a default outline around the focused element. A keyboard user depends on that outline. Here is the focus rule from the earlier chapter:
.nav-list a:hover,
.nav-list a:focus-visible {
background: #374151;
}
.nav-list a:focus-visible {
outline: 2px solid #38bdf8;
outline-offset: -2px;
}Three details matter here:
:focus-visiblematches when the browser decides the user needs a focus indicator, which is nearly always for keyboard focus and usually not for a mouse click. This gives keyboard users the outline without drawing a ring on every mouse click.outline-offset: -2pxdraws the outline inside the link's box. If an ancestor hasoverflow: hidden, an outline drawn outside the box would be clipped.- The outline color must contrast with what is behind it. WCAG, the Web Content Accessibility Guidelines, asks for at least 3:1 for focus indicators. The light blue
#38bdf8on the dark#1f2937bar is comfortably above that.
Skip links
A keyboard user who opens a page with a long menu must tab through every link before reaching the content, and must do so again on every page. A skip link is a link at the very start of the page that jumps past the navigation. It is hidden until it receives focus. This complete page shows it working with the bar:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Accessible navigation</title>
<style>
body { margin: 0; font-family: system-ui, sans-serif; }
.skip-link {
position: absolute;
top: 0;
left: 0;
padding: 0.75rem 1rem;
background: #ffffff;
color: #111827;
transform: translateY(-100%);
}
.skip-link:focus {
transform: translateY(0);
}
.nav-list {
display: flex;
gap: 0.25rem;
margin: 0;
padding: 0;
list-style: none;
background: #1f2937;
}
.nav-list a {
display: block;
padding: 0.75rem 1rem;
color: #ffffff;
text-decoration: none;
}
.nav-list a:hover,
.nav-list a:focus-visible {
background: #374151;
}
.nav-list a:focus-visible {
outline: 2px solid #38bdf8;
outline-offset: -2px;
}
.nav-list a[aria-current="page"] {
background: #111827;
border-bottom: 3px solid #38bdf8;
}
main { padding: 1rem; }
</style>
</head>
<body>
<a class="skip-link" href="#main">Skip to main content</a>
<header>
<nav aria-label="Main">
<ul class="nav-list" role="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>
</header>
<main id="main">
<h1>Welcome</h1>
<p>Page content starts here.</p>
</main>
</body>
</html>Press Tab once after loading the page. The skip link slides into view. Press Enter and focus moves to the main region, so the next Tab goes to the first focusable element in the content, not back to the menu.
The skip link is moved off screen with transform, not hidden with display: none. An element with display: none is removed from the tab order, so it could never receive focus and would never appear.
Controlling what assistive technology sees
CSS can change the accessibility tree. These properties matter for navigation:
| CSS | Visible | In accessibility tree | In tab order |
|---|---|---|---|
| `display: none` | No | No | No |
| `visibility: hidden` | No | No | No |
| `opacity: 0` | No | Yes | Yes |
| `transform: translateY(-100%)` | Off screen | Yes | Yes |
Two consequences follow for later chapters:
- A dropdown submenu hidden with
display: nonecannot be reached by Tab. That is why the submenu rule also shows the menu on:focus-within. When focus enters the parent item, the submenu appears and its links become focusable. - A mobile menu hidden with
display: noneis correctly absent from the tab order while closed. Hiding it withopacity: 0would leave invisible links that keyboard users can still tab to.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Links built from `div` or `span` with click handlers | Not focusable, no link role | Use `a` with an `href` |
| Two `nav` elements with no labels | Landmarks sound identical | Add distinct `aria-label` values |
| `outline: none` with no replacement | Keyboard focus is invisible | Style `:focus-visible` |
| `aria-current` on several links | The current page is ambiguous | Set it on one link only |
| Positive `tabindex` values | Tab order differs from reading order | Use only `tabindex="0"` or `-1`, and rely on DOM order |
| Current page shown by color only | Some users cannot see the difference | Add a border, weight or underline |
Testing your bar
You do not need special software to catch most problems. Work through this checklist:
- Put your mouse aside and press Tab from the top of the page. Every link should receive a clearly visible outline, in an order that matches the visual order.
- Press Enter on a focused link and confirm it navigates.
- Open your browser's developer tools and inspect the accessibility pane for the
navelement. Check that it has the rolenavigationand the name you gave it. - Turn on a screen reader (VoiceOver on macOS, NVDA on Windows) and open its landmarks list. Check that the labels are distinct and that the current link is announced as the current page.
- Zoom the page to 200% and confirm focus outlines are still visible and not clipped.
Exercise
A page has a header navigation and a footer navigation, each a nav element with no attributes. A screen reader user opens the landmarks list. What do they hear, and what one change fixes it?
Show answer
They hear "navigation" twice, so they cannot tell the two regions apart. Add a distinct aria-label to each, for example aria-label="Main" and aria-label="Footer". The screen reader then announces "Main navigation" and "Footer navigation", because it adds the role name itself.
Exercise
A teammate hides the mobile menu with opacity: 0 and pointer-events: none while it is closed. Mouse users cannot click the links, but a keyboard user reports confusing behavior. What happens, and how should the menu be hidden?
Show answer
The links are still in the accessibility tree and the tab order, so Tab moves focus onto invisible links and screen readers read them out. Hide the closed menu with display: none (or visibility: hidden), which removes the links from both. The toggle button's aria-expanded attribute should reflect whether the menu is open.
Exercise
Why is outline-offset: -2px used in the link focus style, and why is the skip link hidden with transform rather than display: none?
Show answer
A negative offset draws the outline inside the link's box, so a parent with overflow: hidden cannot clip it. The skip link uses transform because display: none removes an element from the tab order, so it could never receive focus and would never be revealed.