Track 03 · Lesson 6 of 8Building for everyone
Track 03 · Building for everyone
Keyboard access
Lesson 6 of 8· 6 min
Many people never use a mouse: power users, people with tremors, and anyone using a switch device or voice control. Everything must work with Tab, Shift+Tab, Enter, Space and the arrow keys, and focus must always be visible.
Put the mouse down
Click into the box, then press Tab
Click into the box and press Tab repeatedly, or use the buttons below on a touch screen.
≡ Menu
Help Subscribe to news
Focus is on
nothing yet
Tab order so far
Press Tab inside the box
Controls on screen
MenuSearchHelpSubscribeSend
Tab through it. Menu and Subscribe are styled divs, so the keyboard skips them. Send and Help jump the queue because of positive tabindex values. And with outlines removed, you can’t see where focus is.
What went wrong in the mouse-only build#
- Divs pretending to be controls. The Menu button and the checkbox are styled divs, so Tab skips straight past them.
- Positive tabindex.
tabindex="1"andtabindex="3"pull Send and Help to the front of the queue, so focus jumps around the page in an order that matches nothing on screen. - No focus ring. The outline was removed "because it looked ugly", so even when focus lands somewhere, you can't see where.
The rules#
- Use real buttons, links and inputs; they're focusable and work with Enter and Space already.
- Keep the tab order the same as the reading order.
tabindex="0"adds an element to the natural order andtabindex="-1"makes it focusable from code only. Avoid anything higher. - Never remove the focus outline without a clearly visible replacement.
- Don't trap focus. In a dialog, keep it inside while it's open, and return it to the button that opened it when it closes.
Check yourself