4 / 6 · Intermediate
Using a page with the keyboard
Unplug the mouse and press Tab: every link, button and field should light up in turn, in the order you read the page.
Jump to the 10 practice questionsYou will learn
- Make every control work from the keyboard
- Keep the Tab order and the focus ring
- Add a skip link past repeated menus
Before this: Colour contrast
1Explain
Without a mouse
Many people never touch a mouse. Some cannot hold one steady; some use a switch, a mouth stick or speech recognition that sends key presses; screen reader users mostly move with the keyboard too. WCAG 2.2 success criterion 2.1.1 Keyboard, at level A, says everything on the page must work from a keyboard. Criterion 2.1.2 No Keyboard Trap adds that if focus can move into a part of the page, it must be able to move out again with the keyboard.
The usual keys. Tab moves forward, Shift+Tab back, Enter follows a link or presses a button, Space presses a button or ticks a checkbox, arrows move inside radio groups and menus.
2Problem
The next element
Hint
Shift with it goes back.
Solution
Answer: Tab
Tab moves forward; Shift+Tab moves back.
3Problem
Ticking a checkbox
Which key ticks a focused checkbox?
Hint
The same key presses a button.
Solution
Answer: Space
Space toggles checkboxes and presses buttons.
4Explore
Real controls
Links with an href, button, input, select and textarea are focusable out of the box and respond to the right keys. A div with a click handler is not: it cannot be reached with Tab, Enter does nothing, and a screen reader does not announce it as a button. MDN's advice is plain. Do not use tabindex to make a div act like a button; use the button element, which comes with its role and keyboard behaviour built in.
Click into the preview and press Tab: focus skips the div and lands on the button. The list shows that only the second one is announced as a button.
<style>
.btn { display: inline-block; padding: 8px 14px; background: #1a5fb4; color: #fff; border-radius: 6px; }
</style>
<div class="btn" onclick="this.textContent = 'Added'">Add to basket</div>
<button onclick="this.textContent = 'Added'">Add to basket</button>5Problem
A div with onclick
A div has an onclick handler. What is wrong?
Hint
Can Tab reach it?
Solution
Answer: It cannot be reached or pressed with the keyboard
Use a button, which is focusable and responds to Enter and Space.
6Explain
Tab order
The Tab order follows the order of elements in the HTML source. Criterion 2.4.3 Focus Order asks that this order keeps the meaning and use of the page intact, so the simplest fix for a strange order is to rearrange the HTML. MDN lists three kinds of tabindex value. Zero puts an element into the normal order. A negative value, usually -1, takes it out of the Tab order but still lets a script call focus() on it, which is useful for a dialog or an error message.
Positive tabindex values make focus jump out of reading order.
<a href="#" tabindex="3">Home</a>
<a href="#" tabindex="1">Menu</a>
<a href="#" tabindex="2">Contact</a>
<p>Click here, then press Tab: Menu, Contact, Home. Delete the tabindex values and try again.</p>7Problem
tabindex="0"
What does tabindex="0" do?
Hint
Zero means "in the normal order".
Solution
Answer: Adds the element to the normal Tab order
Zero means focusable in source order.
8Problem
Recommended values
Which tabindex values does MDN recommend?
Hint
Positive numbers jump ahead of the source order.
Solution
Answer: Only 0 and -1
Use 0 for the normal Tab order and -1 when only programmatic focus is needed. Positive values jump ahead of source order and make navigation harder to follow.
9Explain
Seeing the focus
Keyboard users need to see which element has focus. Criterion 2.4.7 Focus Visible, at level AA, asks for a visible focus indicator, so a stylesheet rule like outline: none with nothing in its place is a failure. WCAG 2.2 added two related criteria. 2.4.11 Focus Not Obscured (Minimum), at level AA, says the focused item must not be completely hidden behind something the author added, such as a sticky footer or a cookie banner.
Click into the preview and press Tab twice. The first button gives no sign that it has focus.
<style>
a, button { margin: 4px; }
.bad:focus { outline: none; }
.good:focus-visible { outline: 3px solid #1a5fb4; outline-offset: 2px; }
</style>
<button class="bad">No focus ring</button>
<button class="good">Visible focus</button>10Problem
outline: none
A stylesheet sets outline: none on every focused element. Which criterion fails?
Hint
Can a keyboard user see where they are?
Solution
Answer: 2.4.7 Focus Visible
With no replacement style, keyboard users cannot see where they are.
11Problem
A sticky banner
A sticky cookie banner covers the focused button completely. Which WCAG 2.2 criterion is that?
Hint
A WCAG 2.2 criterion about focus being covered.
Solution
Answer: 2.4.11 Focus Not Obscured (Minimum)
Criterion 2.4.11, added in WCAG 2.2 at level AA, prevents the focused item from being completely hidden. A focus outline cannot help if the whole button is behind a banner.
12Problem
Target size
What minimum target size, in CSS pixels per side, does 2.5.8 ask for?
px
Hint
Criterion 2.5.8.
Solution
Answer: 24 px
At least 24 by 24 CSS pixels, unless there is enough spacing or another exception applies.
13Explore
The skip link
One more pattern helps a great deal: the skip link. A site with a long menu makes keyboard users press Tab through every menu item on every page. A link placed first in the page, pointing to the main content, lets them jump past it in one key press. It can be visually hidden until it receives focus.
The skip link is the first thing a keyboard or screen reader user meets.
<a href="#main">Skip to main content</a>
<nav>
<a href="/">Home</a> <a href="/bread">Bread</a> <a href="/cakes">Cakes</a>
</nav>
<main id="main">
<h1>Corner Bakery</h1>
</main>14Problem
What a skip link does
What does a skip link do?
Hint
It points to main.
Solution
Answer: Jumps past repeated menus to the main content
It is the first link on the page and points to main.
15Problem
No way out
Focus enters a widget but no keyboard method lets the user leave it. Which criterion fails?
Hint
Focus that can enter must be able to leave.
Solution
Answer: 2.1.2 No Keyboard Trap
Focus that can enter must be able to leave with the keyboard.
16Practice
A real button
This "button" is a div, so it cannot be reached with Tab. Replace it with a real button element with the same text and the same onclick.
- a real button
- no div button left
<style>
.btn { display: inline-block; padding: 8px 14px; background: #1a5fb4; color: #fff; border-radius: 6px; cursor: pointer; }
</style>
<div class="btn" onclick="document.querySelector('#out').textContent = 'Added'">Add to basket</div>
<p id="out"></p>Hint
Change the div tags to button; keep the class, onclick and text.
A solution
<style>
.btn { display: inline-block; padding: 8px 14px; background: #1a5fb4; color: #fff; border-radius: 6px; cursor: pointer; }
</style>
<button class="btn" onclick="document.querySelector('#out').textContent = 'Added'">Add to basket</button>
<p id="out"></p>17Practice
Add a skip link
Add a skip link as the very first element of the body: <a href="#main">Skip to main content</a>, and give the main element id="main".
- skip link comes first
- main has id="main"
<nav>
<a href="/">Home</a> <a href="/bread">Bread</a> <a href="/cakes">Cakes</a> <a href="/contact">Contact</a>
</nav>
<main>
<h1>Corner Bakery</h1>
</main>Hint
It must be the very first element, before the nav.
A solution
<a href="#main">Skip to main content</a>
<nav>
<a href="/">Home</a> <a href="/bread">Bread</a> <a href="/cakes">Cakes</a> <a href="/contact">Contact</a>
</nav>
<main id="main">
<h1>Corner Bakery</h1>
</main>18Practice
Remove positive tabindex
The positive tabindex values make focus jump around. Remove every positive tabindex so the order follows the HTML.
- three links still there
- no positive tabindex
<a href="/" tabindex="3">Home</a>
<a href="/menu" tabindex="1">Menu</a>
<a href="/contact" tabindex="2">Contact</a>Hint
All three links lose their tabindex.
A solution
<a href="/">Home</a>
<a href="/menu">Menu</a>
<a href="/contact">Contact</a>19Explain
Common mistakes
<style>
:focus { outline: none; }
</style>
<a href="#">Menu</a> <a href="#">Contact</a><style>
:focus-visible { outline: 3px solid #1a5fb4; outline-offset: 2px; }
</style>
<a href="#">Menu</a> <a href="#">Contact</a>Removing the outline without a replacement hides focus from keyboard users. Style :focus-visible instead.
20
Summary
- Everything must work from the keyboard, with no traps (2.1.1, 2.1.2).
- Use button and a href instead of clickable divs.
- Tab order follows the HTML; use only tabindex 0 and -1.
- Keep focus visible and unhidden; targets at least 24 by 24 CSS px.