Keyboard use: the test you can run yourself in five minutes
AccessibilitySome of your visitors do not use a mouse.
7 min read
By Timo Wessels Published
Some of your visitors do not use a mouse. They operate your website with the keyboard alone. Some have a motor impairment, some use a screen reader (a program that reads the page aloud), some control the computer by voice.
For them the Tab key is the most important tool. With Tab you jump from one control to the next.
The most important points first:
- Every link, every button and every form field must be reachable with Tab.
- You must always be able to see where you are.
- You can check this yourself in five minutes, with the keyboard only.
What has to be right
Three things:
- Everything is reachable with Tab.
- The order follows the reading direction: top to bottom, left to right.
- You can see at all times where the focus is. The focus is the spot that is currently selected, usually marked by a frame.
Underneath lies the structure of the page. It has two parts:
- Landmarks are the large areas of a page: the header, the navigation (
nav), the main content (main) and the footer. With them, screen reader users jump straight to where they want to go. - A skip link is a link at the very start of the page that lets you skip the menu and go straight to the content.
Why it matters
The rules are in the WCAG, the international guidelines for accessible websites. Each rule has a number and a level: A is the base level, AA the next, AAA the strictest.
- WCAG 2.1.1 (Level A): Everything the page can do must work with the keyboard. The only exceptions are movements such as freehand drawing.
- WCAG 2.1.2 (Level A): Nobody may get stuck anywhere. The classic case is a cookie banner or a window that cannot be closed with the keyboard.
- WCAG 2.4.7 (Level AA): The focus must be visible. Its frame must also stand out clearly from the background, with at least 3:1 contrast. This 3:1 is written in a rule of its own, WCAG 1.4.11, and applies to the focus frame (Rheinwerk, Barrierefreie Webseiten, ch. 8.2).
The skip link is where the benefit is easiest to see. Without it, a keyboard user has to go through the whole menu on every page before they reach the content. With thirty menu items that is thirty key presses. On every page, again. This is WCAG 2.4.1 (Level A).
Two mistakes I see often
The focus has been made invisible. Somewhere in the stylesheet there is outline: none, because someone found the blue default frame ugly. The page can still be operated, but nobody sees where they are. It is like hiding the mouse pointer.
A tabindex="1" or higher. With tabindex you decide where an element sits in the tab order. A positive value moves the element to the start of the whole page, wherever it stands. A few of them, and nobody knows where Tab goes next. MDN advises using only 0 and -1. The order should come from the structure of the page, not from tabindex (Rheinwerk, Barrierefreie Webseiten, ch. 8.2).
Mistakes you do not have to argue about
Some mistakes are simply broken:
- A reference that points to nothing.
- A role that does not exist.
- An identifier (
id) used twice although a label or a skip link points to it. The browser then finds only the first one. - A skip link whose target is missing or cannot take the focus.
How big a click target has to be
WCAG 2.5.8 (Level AA) asks for click targets of at least 24 × 24 pixels. There are exceptions. Two matter for most sites:
- Enough space: A smaller target is allowed if there is room around it. Draw a circle 24 pixels across around the centre of the target. This circle must not touch another target, nor the circle of another small target. Well-spaced links in the footer are therefore not a mistake.
- Links in text: A link in the middle of a sentence does not have to be 24 pixels large.
And overlays?
Some add-on programs lay a small bar over your page, with font size and contrast switches. They are called accessibility overlays. They change nothing about the points above. Missing labels, a wrong focus order and keyboard traps are not repaired by such an overlay. That is my experience from practice.
How to check it yourself
This is the test you really can do alone. It takes five minutes.
Open your home page in a private window. Do not accept the cookies. That is exactly when the banner is in the way, and you see how the page really behaves (Rheinwerk, Barrierefreie Webseiten, ch. 8.2). Put the mouse aside and press Tab.
Look out for five things:
- Is the first stop a skip link, for example “Skip to content”? It may be invisible as long as it is not selected. As soon as it has the focus, you must be able to see it.
- Do you see at every step where you are?
- Does the order follow what you read? Top to bottom, left to right.
- Can you get out again everywhere? Open the menu, a foldable section and a window on purpose. Does the Escape key close them? Can you carry on afterwards?
- Can you trigger everything? Links work with Enter, buttons with Enter and with the space bar.
If you cannot go on somewhere, or no longer know where you are, you have found a mistake.
What you can do if it is missing
1. Build in a skip link. It is the first element that can take the focus. It points to the id of the <main> element.
2. Replace every outline: none with a focus style of your own that is easy to see. A frame of at least 2 pixels with a little space has worked well for me:
:focus-visible { outline: 3px solid #1a73e8; outline-offset: 2px; }
3. Remove all positive tabindex values. If the order is wrong afterwards, the order in the HTML is wrong. Then repair the HTML instead of overriding it.
4. Use real landmarks: one <header>, one <main>, one <footer> and <nav> for every navigation block. If there are several <nav>, each gets its own aria-label so they can be told apart. The aria-label names the purpose, for example “Main”. The word “navigation” does not belong in it, because the screen reader says it anyway.
5. For foldable sections and windows, use the browser’s ready-made parts. <details> and <summary> are a foldable section that opens with the keyboard. <dialog>, opened with showModal(), is a window that closes with Escape and puts the focus on the first control inside it when it opens. You do not have to program that yourself.
6. Make click targets bigger through padding, not through font size.
Sources
- W3C, Understanding SC 2.1.1: Keyboard — everything must work by keyboard, except movements that depend on the path, Level A: w3.org(external site: w3.org)
- W3C, Understanding SC 2.1.2: No Keyboard Trap — if the focus can be moved to an element with the keyboard, it must be possible to move it away again with the keyboard, Level A: w3.org(external site: w3.org)
- W3C, Understanding SC 2.4.7: Focus Visible — the focus must be visible, Level AA; the focus frame is also subject to rule 1.4.11 on the contrast of interface components: w3.org(external site: w3.org)
- W3C, Understanding SC 2.4.1: Bypass Blocks — a way to skip repeated blocks such as the navigation is needed, Level A: w3.org(external site: w3.org)
- W3C, Understanding SC 2.5.8: Target Size (Minimum) — 24 × 24 pixels, with the exceptions spacing (a circle of 24 pixels), text and others, Level AA: w3.org(external site: w3.org)
- W3C, Landmark Regions, ARIA Authoring Practices Guide —
header,nav,mainandfooteras landmarks; a name of its own for each area when a landmark is used more than once: w3.org(external site: w3.org) - MDN, tabindex — use only
0and-1, avoid positive values: developer.mozilla.org(external site: developer.mozilla.org) - MDN, The Dialog element —
showModal(), closing with Escape, focus on the first operable element when it opens: developer.mozilla.org(external site: developer.mozilla.org) - MDN, The Details disclosure element — the foldable section that opens by focus and space bar: developer.mozilla.org(external site: developer.mozilla.org)
- Rheinwerk, Barrierefreie Webseiten, ch. 8.2 — test in a private window without cookie consent, the five checks, the recommendation to get the order through the HTML structure instead of
tabindex. - Own testing practice — the two frequent mistakes, the focus style with 3 pixels, the view on accessibility overlays, the order of the measures.