Keyboard
Everything the interface can do must be doable from a keyboard. Not "most things", and not "there is an alternative path" — every control, every menu, every drag.
- Principle
- Operable
- Guideline
- 2.1
- Level
- A
- In WCAG since
- 2.0
- Number
- 2.1.1
Broken, then fixed
The same interface twice: first with the mistake, then with the smallest change that fixes it.
A div pretending to be a button.
<div class="btn" onclick="…">. It looks right and it works with a mouse. It takes no focus, Enter and Space do nothing, and a screen reader announces it as text.
A real button element.
Focusable, activated by Enter and Space, announced as a button, and it inherits the focus ring. Every one of those behaviours would otherwise be hand-written — and three of them are usually forgotten.
What changed in the code
The fix as a diff: removed lines are marked with a minus, added lines with a plus.
removed: <div class="btn" onclick="addToBasket()">Add to basket</div>
added: <button type="button" class="btn">Add to basket</button>
If a div genuinely cannot be replaced, it needs role="button", tabindex="0", and key handlers for both Enter and Space. Four things to get right instead of none.
How to test
Check your own interface against 2.1.1, one step at a time.
- Put the mouse aside and click once in the browser address bar, so the page starts with no element focused.
- Press Tab to move through the page. Check that every link, button, field and custom control receives focus at some point.
- Activate each control from the keyboard: Enter for links and buttons, Space for buttons and checkboxes, arrow keys inside menus, tabs, radio groups and sliders. Check that it does what a click does.
- Find every function that a pointer reaches through hover, drag or right-click, such as a hover menu, a tooltip or a sortable list. Check that the same function can be reached from the keyboard.
- Check that no function needs a key held down or pressed in quick succession. A keystroke may take as long as the person needs.
Common questions
Does 2.1.1 require a visible focus indicator?
No. 2.1.1 asks only that everything can be operated from the keyboard. Seeing where focus is belongs to 2.4.7 Focus Visible, so a page can pass one and fail the other.
Does a drawing or signature tool have to work from the keyboard?
Only when the function does not depend on the path the pointer takes. Freehand drawing, where the speed and shape of each stroke are the result, is exempt. Placing a shape or drawing a straight line between two points is not, because the start and end points can be given from the keyboard.
Does a menu that opens only on mouse hover fail 2.1.1?
Yes, if its links cannot be reached any other way from the keyboard. Open it on focus or on Enter as well as on hover, or make every item in it reachable by Tab.
Try it yourself
A lab is a small working piece of interface that you change yourself. Read its steps, use the controls under it, and watch the verdict line at the bottom: it says whether the criterion passes or fails as you go. Reset puts it back.
Try it: reach the button by keyboard
How to use this lab
- Goal. Add an item to the basket using only the keyboard.
- The controls. The choices under “Build the control as” make Add to basket A click-only div, A div with tabindex="0" (a div made into a Tab stop) or A real button. Press Tab, Press Enter and Press Space try it with simulated keys. Beneath, the markup shows the code and the log shows what a screen reader calls it.
- Try this. The lab starts with A click-only div, and fails. Press Tab, then Press Enter, and see that nothing is reached or added. Then choose A div with tabindex="0" and A real button in turn, and press the keys again.
- What it means. With A real button, 2.1.1 passes: Tab reaches it, and Enter and Space press it. With either div, 2.1.1 fails, because Tab skips it or no key presses it, so people who use a keyboard or a switch can’t add the item. The verdict line under the lab says which.
The markup
<div class="btn" onclick="add()">Add to basket</div>A screen reader calls it
2.1.1 fails
Also in guideline 2.1
Criteria in the same guideline often fail together.