Keyboard and focus
Everything that can be done with a mouse must also be possible with a keyboard. How focus moves around a page, why not to use a positive tabindex, how to make focus visible so that it does not disappear under a sticky header, and how to open and close a dialog properly.
How a website is used with a keyboard
- Tab and Shift+Tab – move to the next and previous control (a link, a button, a field).
- Enter – opens a link, presses a button, submits a form.
- Space – presses a button, checks a checkbox; elsewhere it scrolls the page.
- Arrow keys – radio buttons, choosing from a list, tabs, scrolling the page.
- Esc – closes a dialog, an expanded menu, a popup.
Try it: put the mouse aside and go through the website with a keyboard only. Everything must be operable (criterion 2.1.1), focus must always be visible (2.4.7) and the order must make sense (2.4.3). In Safari on a Mac the Tab key skips links and some other elements by default – it goes through them only after highlighting each item with the Tab key is turned on in the advanced Safari settings, or when Option+Tab is pressed instead of Tab.
Use HTML controls
A button and an a link with an address receive focus and react to Enter by themselves. A div element with a click event does not – it works with a mouse, but the keyboard cannot reach it at all:
Error – a “button” made from a div:
<div class="button" onclick="save()">Save</div> <button type="button" onclick="save()">Save</button>
Verified in three browsers: the Tab key skips the div and only the button receives focus. Automated tools do not find this error – they do not recognize a div with an event – which is why going through the page with a keyboard by hand is irreplaceable. If the div really had to stay, it would need role="button", tabindex="0" and handling of the Enter and Space keys. It is simpler to use a button and style it with CSS.
Focus order and tabindex
Focus follows the order of the elements in the HTML. When CSS (flexbox order, grid, absolute positioning) rearranges the elements visually, focus jumps around the page confusingly. The solution is to change the order in the HTML, not in CSS. The tabindex attribute:
tabindex="0"– an element that does not normally receive focus joins the order according to its place in the HTML. Only for custom controls with a role (see ARIA in practice).tabindex="-1"– the element can be focused by a script (element.focus()), but the Tab key skips it. For a dialog heading, an error summary, a target of a jump.- Do not use a positive
tabindex. An element with it receives focus before all the others on the page, so the order breaks.
Error – a positive tabindex:
<a href="/">Home</a> <a href="/articles/">Articles</a> <input type="search" name="q" aria-label="Search" tabindex="1">
The first press of Tab jumps to the search field and only then to the links before it – verified in three browsers.
Visible focus
The browser draws an outline around the element with focus. The worst thing you can do to it is outline: none without a replacement – a keyboard user then does not know where they are. A custom focus style belongs in :focus-visible: the browser shows it when the keyboard is used, but not after a mouse click on a button (for text fields always):
<button type="button" class="button">Order</button>
.button {
padding: 8px 18px;
border: 0;
border-radius: 4px;
background: #2e7d32;
color: #fff;
}
.button:focus-visible {
outline: 3px solid #1a1a1a;
outline-offset: 3px;
}
An outline with an offset (outline-offset) is visible on a light as well as a colored background. The outline must have a contrast of at least 3:1 with its surroundings (criterion 1.4.11). Unlike a border, an outline does not change the size of the element, so nothing moves when it receives focus.
Focus under a sticky header
Criterion 2.4.11 (new in WCAG 2.2): an element with focus must not be completely hidden by other content. This typically happens with a sticky header (position: sticky or fixed) – when the Tab key scrolls the page, the link slides under it. The scroll-padding property helps: when scrolling to the focus, the browser leaves free space at the top:
<header class="sticky">Garth.cz</header> <main> <p>…long content…</p> <a href="/next/">A link in the content</a> </main>
html {
scroll-padding-top: 80px;
}
.sticky {
position: sticky;
top: 0;
height: 64px;
background: #fff;
}
Where the browser scrolls the page when Tab is pressed differs between browsers, so an element can end up under the header – most often when moving back with Shift+Tab. Verified in three browsers: when the element with focus is in the window but hidden under the header, without scroll-padding-top the browser does not move the page and the focus stays under the header; with it, the browser scrolls the page so that the element is visible. A jump to an anchor (a #target link) behaves the same way: without scroll-padding-top the target ends up at the top edge of the window under the header, with it 80 px below the edge. For a cookie consent bar stuck to the bottom edge there is scroll-padding-bottom.
A dialog and a focus trap
A modal window (a confirmation, a sign-in, a gallery) must take over focus, not let it into the page below, close with the Esc key and return focus to where it was opened from after closing. A dialog element opened with showModal() does all of this by itself – the page below it is inactive (as with the inert attribute):
<button type="button" id="delete-account">Delete account</button>
<dialog id="confirm" aria-labelledby="confirm-title">
<h2 id="confirm-title">Really delete your account?</h2>
<p>Deletion cannot be undone.</p>
<form method="dialog">
<button type="submit" value="no">Cancel</button>
<button type="submit" value="yes">Delete</button>
</form>
</dialog>
const dialog = document.getElementById("confirm");
document.getElementById("delete-account").addEventListener("click", () => {
dialog.showModal();
});
dialog.addEventListener("close", () => {
if (dialog.returnValue === "yes") {
// … delete the account …
}
});
Verified in three browsers: after opening, the first button in the dialog has focus (Cancel – with an irreversible action the safe choice should come first), Esc closes the dialog and focus returns to the Delete account button. A different element for the first focus is set with the autofocus attribute in the dialog.
Watch out for the opposite error – a keyboard trap (criterion 2.1.2): an element that the Tab key cannot leave. It is usually an embedded player, a map or an editor that captures Tab for itself. Every such element must be possible to leave with the keyboard, and its instructions should say how.
Quick checks
- Go through the page with the Tab key from start to end. Is focus always visible? Does the order follow the rows, the way the page is read?
- Open and close the menu, a dialog, a gallery with the keyboard. Does focus return to its place?
- Can you submit a form and complete an order without a mouse?
- Does nothing react only to hovering the mouse? Content that appears on hover (a tooltip, a submenu) must also appear on focus and be possible to close with Esc (criterion 1.4.13).