Focus management

On this page 7

Focus management is the practice of deliberately controlling where keyboard focus is placed as a web interface changes, so that keyboard and screen reader users always know where they are and can continue without searching. In web accessibility, it covers the visible focus indicator, a logical focus order, and moving focus programmatically when dialogs open, content updates or views change.

Browsers manage focus well for static documents: the Tab key follows the source order of interactive elements. Problems start when scripts add, remove or hide content. If the focused element disappears or new content appears elsewhere, focus can fall back to the top of the page, and the user loses context. Good focus management restores that context explicitly.

How keyboard focus works in the browser

At any moment, exactly one element in a document holds focus and receives keyboard input; scripts can read it through document.activeElement. Native controls such as links, buttons and form fields are focusable by default and appear in the sequential focus order that Tab and Shift+Tab traverse.

The tabindex attribute changes this behavior, and its values are often misunderstood. The table summarizes their effect.

ValueIn Tab orderFocusable by scriptTypical use
Not set on native controlYesYesLinks, buttons, inputs
tabindex=”0″Yes, in source orderYesCustom widgets built from generic elements
tabindex=”-1″NoYesHeadings or containers that receive focus after a change
Positive valueYes, before everything elseYesAvoid, it breaks the natural order
Effect of tabindex values on focus

The inert attribute removes a whole subtree from focus and from the accessibility tree, which is the modern way to block background content while a modal is open.

Requirements in WCAG

Focus management is not a single success criterion but is required by several WCAG criteria together. Meeting them is part of conformance at levels A and AA, which most accessibility laws and procurement rules reference.

CriterionLevelWhat it requires
2.1.1 KeyboardAAll functionality is operable with a keyboard
2.1.2 No keyboard trapAFocus can always move away from a component
2.4.3 Focus orderAFocus order preserves meaning and operability
2.4.7 Focus visibleAAThe focused element has a visible indicator
2.4.11 Focus not obscured (minimum)AASticky headers or overlays do not hide the focused element entirely
3.2.1 On focusAReceiving focus does not trigger an unexpected change of context
WCAG success criteria that govern focus

WCAG 2.2 also adds 2.4.13 Focus appearance at level AAA, which sets a minimum area and a contrast ratio of at least 3:1 between focused and unfocused states. Many design systems adopt it as a target even when only AA is required.

Common focus management patterns

Most interactive components follow established patterns described in the ARIA Authoring Practices. Each pattern answers two questions: where focus goes when the component appears, and where it returns when the component closes.

SituationFocus on open or changeFocus on close
Modal dialogFirst meaningful control or the dialog titleBack to the element that opened it
Route change in an SPAMain heading of the new viewNot applicable
Deleted list itemNext item, or previous if it was lastNot applicable
Inline form error after submitError summary or first invalid fieldNot applicable
Disclosure or menuFirst item for menus, stays on toggle for disclosuresBack to the toggle button
Where focus should go in common situations

Composite widgets such as tabs, listboxes and grids use a roving tabindex or aria-activedescendant. Only one element of the widget sits in the Tab order, and the arrow keys move focus inside it, which keeps long widgets from flooding the Tab sequence.

Focus management works together with neighboring accessibility techniques, and confusing them leads to incomplete fixes. A skip link lets users bypass repeated navigation once per page, while focus management responds to every dynamic change.

Semantic structure provides the headings and landmarks that focus targets rely on, and live regions announce updates without moving focus at all. Moving focus is the stronger intervention: it should be used when the user must act on new content, while a live region suits status messages such as “Saved” that require no action.

Mistakes that break focus

Focus bugs are among the most frequent findings in accessibility audits of web applications, especially single-page applications. The typical failures are listed below.

SymptomCauseFix
Focus jumps to page topFocused element was removed from the DOMMove focus to a logical neighbor before removal
Tab reaches content behind a modalBackground not made inertApply inert or trap focus within the dialog
No visible focus ringoutline: none without replacementStyle :focus-visible with sufficient contrast
New view is silentClient-side routing without focus handlingFocus the new main heading
Focus lost on modal closeOpener reference not storedSave the trigger and restore focus to it
Focus failures, causes and fixes

Moving focus too often is also harmful. Unrequested focus jumps disorient users, so focus should move only in response to a user action or when the old position no longer exists.

Put simply

People who use a keyboard instead of a mouse move through a page one element at a time, like a cursor that marks “you are here”. Focus management makes sure that marker is always visible and always in a sensible place.

When a window pops up, the marker should jump into it. When the window closes, the marker should return to where the person was. Without that care, users get lost and have to start again from the top of the page.

In practice

A bank rebuilds its online transfer form as a single-page application. An audit with a screen reader user reveals that after pressing “Review transfer”, nothing is announced, and focus remains on a button that was removed, so the next Tab press lands at the top of the page.

The team changes the flow: after the view switches, focus moves to the heading “Review your transfer”, which has tabindex="-1". The confirmation dialog traps focus while open and returns it to the confirm button when closed, and validation errors focus an error summary with links to each field. Keyboard-only users now complete the transfer in a straight line, and the retest shows no focus-related WCAG failures.