Sub topic 7
Keyboard access and visible focus
This chapter assumes you can write basic HTML and CSS and know what a link, a list, and a nav element are. It also assumes you can attach a JavaScript event listener. You should already have the semantic navigation from the previous chapters: a labeled nav, a list of links, a skip link, and aria-current on the current page.
Keyboard access means a person can reach and operate every part of the navigation without a mouse. Visible focus means they can always see where they are while doing it. Both are required for people who use a keyboard, a switch device, or voice control that sends key events. They also help anyone whose mouse stops working.
How keyboard users move through a page
Browsers keep track of one focused element at a time. Pressing Tab moves focus to the next focusable element in the page. Shift+Tab moves it back. The order is the order of the elements in the HTML source, unless you change it.
A few elements are focusable by default:
- Links with an
href. - Buttons.
- Form controls such as inputs and selects.
- Elements with a
tabindexattribute.
Other elements, such as div and span, are not focusable. A click handler on a div is invisible to keyboard users.
| Key | Link | Button |
|---|---|---|
| Enter | Follows the link | Activates the button |
| Space | Scrolls the page | Activates the button |
| Tab | Moves focus to the next focusable element | Moves focus to the next focusable element |
Space does not follow a link. This is why submenu triggers should be real button elements and page destinations should be real a elements. Each one then responds to the keys people expect, with no extra code.
Keep the DOM order the same as the visual order
Focus follows source order. If you reorder items visually with CSS (order, flex-direction: row-reverse, grid placement, or absolute positioning), focus still follows the HTML. The focus indicator then jumps around the screen in a confusing way.
Write the HTML in the order you want people to tab through it. For a typical header that is: skip link, logo link, primary navigation, then search or account controls. If the layout needs something to look different, change the layout carefully and test the tab order afterward.
Avoid positive tabindex
The tabindex attribute accepts three kinds of values.
| Value | Effect |
|---|---|
| `tabindex="0"` | Puts the element in the natural tab order. |
| `tabindex="-1"` | Makes the element focusable by script (`element.focus()`) but removes it from the tab order. |
| `tabindex="3"` (any positive number) | Forces the element to the front of the tab order. Avoid this. |
Positive values create a second tab order that runs before the natural one. Every change to the page then requires renumbering, and it is easy to produce an order nobody intended. If the order is wrong, fix the HTML order instead.
You will use tabindex="-1" for one common job in navigation. A skip link jumps to #main, and some browsers do not move keyboard focus to a non-focusable target. Adding tabindex="-1" to the main element makes sure the next Tab press starts from inside the content:
<a class="skip-link" href="#main">Skip to main content</a>
<main id="main" tabindex="-1">
<h1>Home</h1>
<p><a href="/products">Browse products</a></p>
</main>If you add this, remove the focus outline from main only, because it is a landmark and not an interactive control:
main:focus {
outline: none;
}Make focus visible
A focus indicator is the outline or other mark that shows which element has focus. Browsers draw one by default. A common mistake is removing it with outline: none or outline: 0 because designers dislike the look. Doing so leaves keyboard users with no idea where they are. If you remove the default, you must replace it with something at least as clear.
Use :focus-visible
The :focus pseudo-class matches whenever an element has focus, including after a mouse click. That is why many sites hid the outline in the first place: clicking a button left a ring on it. The :focus-visible pseudo-class matches only when the browser decides the focus indicator should be shown. For keyboard navigation it matches. For mouse clicks on buttons it usually does not.
This stylesheet gives all navigation links and buttons a clear outline for keyboard users and leaves mouse clicks alone:
nav a:focus-visible,
nav button:focus-visible {
outline: 3px solid #1a5fd0;
outline-offset: 2px;
}Do not write nav a:focus { outline: none; } and then add the outline back only on :focus-visible, unless you are sure about browser support for it. Writing the visible style on :focus-visible and leaving the browser default alone otherwise is the safest approach.
Contrast and thickness
The indicator needs enough contrast against everything next to it. WCAG 2.2 success criterion 1.4.11 (Non-text Contrast) asks for a contrast ratio of at least 3:1 between the indicator and the adjacent colors. Criterion 2.4.7 (Focus Visible) requires that a visible indicator exist. WCAG 2.2 also adds 2.4.11 (Focus Not Obscured, Minimum), which says a focused element must not be entirely hidden by other content such as a sticky header.
Practical points:
- A 2 to 3 pixel outline is easy to see. A 1 pixel dotted line is not.
outline-offsetseparates the outline from the element, which helps when the link has a background color.- Check the outline on both light and dark areas. A blue outline that works on white may vanish on a dark blue header. One option is a two-color indicator, for example an outline plus a contrasting
box-shadow.
nav a:focus-visible {
outline: 3px solid #1a5fd0;
outline-offset: 2px;
box-shadow: 0 0 0 5px #ffffff;
}Here the white ring sits outside the blue outline, so at least one of the two colors contrasts with whatever is behind it.
Do not rely on hover alone
Any style you give :hover, such as an underline or a background change, should also be given to :focus-visible. Otherwise keyboard users miss the feedback that mouse users get. Group the selectors:
nav a:hover,
nav a:focus-visible {
background: #eef3fc;
text-decoration: underline;
}The skip link needs to be visible on focus
A skip link is only useful if a keyboard user can see it when it receives focus. The approach from the earlier chapter moves it off-screen until focus arrives. Do not hide it with display: none, visibility: hidden, or hidden. Those remove it from the tab order and from the accessibility tree, so it cannot be focused at all.
.skip-link {
position: absolute;
left: 0.5rem;
top: -4rem;
padding: 0.5rem 1rem;
background: #fff;
color: #000;
}
.skip-link:focus {
top: 0.5rem;
}Sticky headers can hide focus
A common failure is a sticky or fixed header that covers the element receiving focus as the page scrolls. When someone tabs through links in the main content, the browser scrolls the focused link into view, but it may land under the header. The scroll-padding-top property tells the browser to leave room:
html {
scroll-padding-top: 5rem;
}Set the value to at least the height of your sticky header, plus a little space. This also fixes in-page anchor links, such as the skip link target, which would otherwise scroll under the header.
Test by tabbing
Put the mouse aside and work through the page with the keyboard. For each item in the navigation, check:
- Can I reach it with Tab and Shift+Tab?
- Can I see where focus is at every step?
- Does Enter (links) or Enter and Space (buttons) do what I expect?
- Does the focus order match the visual order?
- Does the focused element stay visible, not hidden under a sticky header?
Doing this takes a couple of minutes and finds most keyboard problems. The later chapter on testing returns to this with screen readers and touch devices added.
Common errors
- Removing the outline with
outline: noneand giving nothing in its place. - Using
divorspanwith a click handler instead of abutton. - Using
tabindexwith a positive number. - Hiding the skip link with
display: none. - Styling
:hoverbut not:focus-visible. - Reordering items with CSS so the focus order no longer matches what the user sees.
- Letting a sticky header cover the focused element.
Exercise
A designer asks you to remove the blue outline that appears around navigation links when they are clicked, because it looks untidy. The navigation must stay usable by keyboard. What do you change, and why does a plain nav a { outline: none; } fail?
Show answer
Remove the outline from :focus only for mouse interaction by writing the visible style on :focus-visible instead, for example nav a:focus-visible { outline: 3px solid #1a5fd0; outline-offset: 2px; }, and do not set outline: none on plain nav a. Browsers apply :focus-visible when a keyboard user tabs to a link, so the indicator still appears for them, while mouse clicks on links generally do not trigger it. A plain nav a { outline: none; } removes the indicator in every case, so keyboard users cannot tell which link has focus, which fails WCAG 2.4.7 Focus Visible.
Exercise
Your header is sticky and 4rem tall. When keyboard users tab to a link near the top of a long page's content, the link scrolls under the header and cannot be seen. Which CSS property fixes this, and what value would you use?
Show answer
Use scroll-padding-top on the scrolling element, normally html, with a value larger than the header height, for example html { scroll-padding-top: 5rem; }. The extra 1rem leaves a gap so the focused element sits just below the header. It also corrects the scroll position of in-page anchor targets such as the skip link destination.