Lesson 3 · The web platform, in depth
Accessibility engineering
Accessible names, ARIA state and focus management for custom components.
About 30 minutes
Tab indents. Press Escape, then Tab, to leave the editor.
Screen readers, switch devices, voice control and keyboard users never see your pixels. They interact with the accessibility tree the browser derives from your DOM, and with whatever element has focus. Custom components fail them in predictable ways: no name, stale state, focus that goes nowhere. This lesson covers how names are computed, how ARIA state works (and doesn't), and how to manage focus, ending with a tabs component built to the ARIA Authoring Practices Guide (APG) pattern.
The accessibility tree
For each relevant node the browser exposes a role (button, tab, checkbox), a name ("Delete note"), an optional description, states (expanded, selected, checked, disabled) and sometimes a value. It maps these to the platform's accessibility API: UI Automation and IAccessible2 on Windows, NSAccessibility on macOS, UIAccessibility on iOS, AT-SPI on Linux, and Android's accessibility framework. Assistive technology reads that API, not the DOM.
Two consequences. First, an element's semantics come from its role, so <div onclick> is "group" or nothing, however much it looks like a button. Second, the tree is computed: DevTools shows it (Chrome's Elements → Accessibility pane, the Firefox Accessibility Inspector, Safari's Node → Accessibility), and that's the ground truth to debug against.
How the accessible name is computed
The Accessible Name and Description Computation spec (accname) defines a precedence order. Simplified, for an element, the first of these that yields a non-empty string wins:
aria-labelledby: the text of the referenced elements, in IDREF order, joined with spaces. Referenced elements contribute even if they're hidden. References aren't followed recursively past one hop.aria-label: the attribute's string.- Native labeling:
<label for>or a wrapping<label>for form controls,alton<img>,<caption>for tables,<legend>for fieldsets,<title>in SVG. - Name from content, only for roles that allow it (button, link, heading, tab, cell, option, menuitem…): the text content of descendants, including their own names (an
<img alt>inside a link contributes its alt text). title, and for inputs,placeholderas a last resort.
Pitfalls this explains: aria-label on a <div> with no role is ignored by many screen readers, because generic elements don't support naming. A placeholder-only input does get a name (browsers fall back to the placeholder), but the visible hint disappears as soon as the user types, which is why it fails as a label. An icon button with an SVG and no text has an empty name unless you add aria-label or visually hidden text. Whatever you choose, include the visible label text in the name (WCAG 2.5.3, Label in Name) so voice-control users can say what they see.
ARIA: what it changes and what it doesn't
ARIA attributes change only the accessibility tree. They add no behavior, no focusability, no keyboard handling. role="button" on a <div> makes a screen reader announce "button" and promises Enter and Space will activate it, a promise you now have to keep yourself, along with tabindex="0" and a visible focus style. That's why the first rule of ARIA use is to prefer the native element: <button>, <a href>, <input type="checkbox">, <details>/<summary>, <dialog>, <select> come with the semantics and the interaction model already wired.
When you do build custom, the states you'll use most:
| Attribute | On | Means |
|---|---|---|
aria-expanded="true|false" | the control that opens something | A disclosure, menu button or combobox popup is open. Omit it entirely on things that don't expand. |
aria-selected | tab, option, gridcell | Selected within a selection container |
aria-pressed | button | A toggle button's state. Don't change the label as well as the state. |
aria-checked | checkbox, radio, switch, menuitemcheckbox | true, false, or mixed for tri-state checkboxes |
aria-current="page|step|true" | link, item | The current item in a set (the nav link for this page) |
aria-disabled="true" | any | Announced as disabled but stays focusable, unlike disabled. Useful when users need to discover why something is unavailable. You must also block activation. |
aria-invalid, aria-describedby | form controls | The field has an error; point aria-describedby at the message text |
And three ways to hide, which are not interchangeable:
hidden(ordisplay: none): gone visually, from the accessibility tree, and from focus order.aria-hidden="true": removed from the accessibility tree only. It's still visible and still focusable, so never put it on, or around, anything focusable: keyboard users land on something screen readers say doesn't exist.inert: still visible, but not focusable, not clickable and hidden from assistive tech. The right tool for "everything behind this modal".
aria-controls deserves honesty: few screen readers do anything with it. The APG tabs pattern includes it and it costs nothing, but don't rely on it to convey a relationship. Structure and naming have to do that.
Focus management
Focus is the keyboard user's cursor. The tools:
tabindex="0"puts an element in the sequential focus order at its DOM position.tabindex="-1"makes it focusable by script (el.focus()) but not by Tab. Positivetabindexvalues jump the queue and create an order nobody can maintain; don't use them.- Composite widgets (tabs, toolbars, listboxes, grids, menus, radio groups) are one Tab stop. Arrow keys move within them. Two implementations: a roving tabindex (the active item has
tabindex="0", the rest-1, and arrow keys move both the zero and real focus), oraria-activedescendant(focus stays on the container, which points at the active item's id). Roving tabindex is simpler and lets the browser scroll the focused item into view;aria-activedescendantsuits comboboxes, where focus must stay in the text input. - When content changes, move focus deliberately. Opening a modal moves focus into it; closing returns focus to the control that opened it. Deleting the focused row should focus a neighbor, not drop focus to
<body>.<dialog>opened withshowModal()makes the rest of the page inert, focuses the first focusable element (or the one withautofocus), closes on Escape, and current browsers return focus to the previously focused element on close. Check it in your target browsers, and do all of this yourself for any custom overlay. - Show focus.
:focus-visiblelets you hide rings for mouse clicks while keeping them for keyboard use. WCAG 2.2 adds 2.4.11 Focus Not Obscured: a sticky header that covers the focused element fails it.scroll-padding-topon the scroll container fixes most cases.
Build it: an APG tabs component
The APG tabs pattern: a tablist containing tabs, each tab aria-controls its tabpanel, each panel aria-labelledby its tab. The selected tab has aria-selected="true" and is the only tab with tabindex="0". Left and Right Arrow move between tabs and wrap at the ends; Home and End jump to the first and last. With automatic activation, moving focus also selects the tab, which APG recommends when panels display without noticeable delay.
Challenge
Make the tabs accessible
The tabs work with a mouse, but the accessibility tree has no idea which tab is selected and the keyboard can't reach the others sensibly. Make them follow the APG pattern with automatic activation:
- Connect each tab and panel with
aria-controlsandaria-labelledby. - Keep exactly one tab
aria-selected="true"(the others"false"), with a roving tabindex:0on the selected tab,-1on the rest. - Hide inactive panels with the
hiddenattribute. - Handle ArrowRight, ArrowLeft (wrapping), Home and End: move focus to the new tab and select it.
Try it with Tab and the arrow keys in the preview.
- Nothing logged yet.
- Each tab points at its panel with aria-controls, and each panel is labelled by its tab
- Exactly one tab is selected, only it has tabindex 0, and only its panel is shown
- Clicking a tab selects it and shows its panel
- ArrowRight and ArrowLeft select the next and previous tab, wrapping at the ends
- Arrow keys, Home and End move keyboard focus to the newly selected tab
In a right-to-left layout, swap the meaning of the arrow keys. For vertical tablists, set aria-orientation="vertical" and use Up and Down instead.
Announcing changes: live regions
Changes away from focus (a "Saved" toast, a search result count, a sync error) are silent unless you put them in a live region. role="status" (implicitly aria-live="polite") announces when the user is idle; role="alert" (assertive) interrupts, so reserve it for things that need immediate attention. The region must exist in the DOM before its content changes: inject the container on page load and update its text later. Inserting an element that already contains the message is announced unreliably.
Testing what you built
Automated checkers (axe, Lighthouse, the browser's own issues panel) reliably catch missing names, invalid ARIA, and contrast failures. They cannot tell you whether the focus order makes sense, whether an announcement is useful, or whether a custom widget is operable. So the minimum manual pass is: unplug the mouse and complete the task with Tab, Shift+Tab, Enter, Space, Escape and arrows; then repeat with a screen reader (VoiceOver on macOS and iOS, NVDA on Windows, TalkBack on Android), because each pairs with a different browser and they differ. The WCAG 2.2 criteria you'll hit most building components: 2.1.1 Keyboard, 2.4.3 Focus Order, 2.4.7 Focus Visible, 2.4.11 Focus Not Obscured (Minimum), 2.5.8 Target Size (Minimum, 24 × 24 CSS px), and 4.1.2 Name, Role, Value.
Recap
- Assistive tech reads the accessibility tree: role, name, description, state. Debug against the tree in DevTools, not the DOM.
- Names resolve in order:
aria-labelledby→aria-label→ native label → content (for roles that allow it) →title/placeholder. - ARIA changes semantics only. Prefer native elements; if you build custom, you own focusability, keyboard behavior and state updates (
aria-expanded,aria-selected,aria-pressed…). hidden,aria-hiddenandinertdo different things; never hide focusable content witharia-hidden.- Composite widgets are one Tab stop with arrow-key navigation (roving tabindex), and focus moves deliberately when content opens, closes or disappears.
Finished this lesson?
Mark it complete to track your progress on this device.