Skip to the page

Keyboard, focus and widgets · core

Keyboard access, tab order and visible focus

Everything a mouse can do must work with a keyboard: Tab moves through interactive elements in DOM order, tabindex=0 adds an element to that order, tabindex=-1 makes it focusable only from code, positive tabindex breaks the order, and focus must always be clearly visible.

The notes

What you must know

  • Tab / Shift+Tab move between focusable elements; Enter activates links and buttons; Space activates buttons and checkboxes; arrow keys move inside composite widgets; Escape closes things.
  • Naturally focusable: a[href], button, input, select, textarea, summary, and elements with tabindex >= 0 (unless disabled, hidden or inert).
  • tabindex="0": focusable in DOM order. tabindex="-1": focusable with element.focus() only (headings you move focus to, panels). Positive values jump the queue and create a confusing order; avoid them.
  • Focus order follows the DOM, so keep the DOM order the same as the visual order (be careful with CSS order, flex-direction: row-reverse, absolute positioning).
  • Never remove the focus outline without a replacement. Use :focus-visible to show a strong ring for keyboard users; WCAG 2.2 adds that focus must not be hidden by sticky headers or banners.
  • Skip link: a 'Skip to main content' link as the first focusable element.
  • No keyboard traps: users must be able to Tab out of every component (except a modal dialog, which keeps focus inside on purpose and closes with Escape).

The red pen

Common traps: mistake → fix

  • Mistake: "outline: none looks cleaner"

    Fix: keyboard users lose track of where they are; style :focus-visible instead.

  • Mistake: "tabindex='1' fixes the order"

    Fix: positive tabindex creates a separate order before everything else; fix the DOM order.

  • Mistake: "Disabled buttons should be focusable for screen readers"

    Fix: native disabled removes focus; if users must discover it, use aria-disabled and explain why.

Easy to hard

Questions interviewers ask, easy to hard

  1. What do tabindex 0, -1 and positive values do?
  2. How do you test a page with the keyboard only?
  3. How do you show focus for keyboard users but not on mouse click?
  4. Our sticky header hides the focused element when tabbing. Which WCAG 2.2 rule is that and how do you fix it?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview