Skip to content
Tutorials Center
My learning

Topics

Lesson 107 of 122 ยท Accessibility and Best Practices

Keyboard Access and Focus

Make every control reachable without a mouse. Learn tab order, tabindex 0 and -1, visible focus with :focus-visible, skip links, inert and focus management.

Many people never use a mouse. Some have a motor disability and use a keyboard, a switch device or voice control, which all act like a keyboard under the hood. Screen-reader users mostly navigate with keys. And plenty of power users simply prefer Tab and Enter because it is faster.

For all of them, two things must be true: every control can be reached and used from the keyboard, and it is always clear where the keyboard focus is. This lesson shows how HTML handles both, and how to fix things when you build custom behavior.

How keyboard navigation works

At any moment, one element on the page has focus: it receives the keys you press. Users move focus and act on it with a small set of keys:

Key What it usually does
Tab / Shift+Tab Move to the next / previous focusable element
Enter Follow a link, press a button, submit a form
Space Press a button, toggle a checkbox, scroll the page
Arrow keys Move within a group: radio buttons, select options, sliders, tabs
Esc Close a dialog, popover or menu

WCAG 2.2 turns these expectations into rules. Everything must work from the keyboard (2.1.1), focus must never get stuck (2.1.2, No Keyboard Trap), focus must move in a meaningful order (2.4.3), the focused element must be visible (2.4.7) and, new in 2.2, it must not be completely hidden behind things like sticky headers or cookie banners (2.4.11).

Focusable elements and tab order

Interactive elements are focusable by default: links with an href, buttons, form fields (<input>, <select>, <textarea>), <summary>, elements with contenteditable, and a few others. Non-interactive elements like <p>, <div> and <h2> are not, and they should not be; screen readers have their own ways to read text.

The tab order follows the order of elements in the HTML source. That is one more reason to write your HTML in a logical reading order and do the visual arranging with CSS.

Watch out for CSS reordering. Flexbox order, flex-direction: row-reverse and Grid placement can move things visually without changing the source order. Focus then jumps around the screen in a confusing way. If the visual order needs to change, change the HTML order too.

The tabindex attribute

tabindex adjusts whether and how an element takes focus. It has three kinds of values:

  • tabindex="0" puts an element into the tab order at its natural source position. Use it only for custom interactive widgets that have no native element, and give them a role and keyboard handling too.
  • tabindex="-1" makes an element focusable by script (element.focus()) but leaves it out of the tab order. Perfect for headings, panels or error summaries you want to move focus to.
  • A positive value like tabindex="3" forces the element to the front of the tab order. Avoid it: it breaks the natural order for the whole page and is very hard to maintain.

Try it below. Click inside the preview, then press Tab repeatedly and watch the log.

Tab order with tabindex

The two positive values jump the queue, so the focus order no longer matches what you see. That is exactly the confusion tabindex greater than zero causes on real pages.

Make focus visible

Browsers draw a focus indicator, usually an outline, around the focused element. Removing it with outline: none and no replacement leaves keyboard users lost. Instead, style it to match your design.

The :focus-visible pseudo-class helps. It matches when the browser decides a visible indicator is useful, typically for keyboard navigation, and not for a mouse click on a button. That lets you show a strong ring for keyboard users without distracting mouse users.

Custom focus styles

A good focus indicator has strong contrast against the background (WCAG asks for 3:1 for the visual parts of controls) and is thick enough to notice. The Global Attributes lesson lists tabindex alongside the other attributes that every element accepts.

On most sites, every page starts with the same header and navigation. A keyboard user would have to tab through all of it on every page before reaching the content. A skip link solves this: the very first focusable element on the page, linking to the main content.

HTML
<body>
  <a class="skip-link" href="#main">Skip to main content</a>
  <header>...logo and twenty navigation links...</header>
  <main id="main" tabindex="-1">
    ...
  </main>
</body>

It is common to hide the link visually until it receives focus, so it appears exactly when a keyboard user needs it. The tabindex="-1" on <main> makes sure focus really moves there in every browser, so the next Tab continues from the content. The Building Navigation Menus lesson shows a complete styled skip link.

Removing whole areas with inert

Sometimes part of the page should be temporarily unreachable, for example the page behind an open side drawer. The boolean inert attribute does this in one step: the element and everything inside it can no longer be focused or clicked, and it is hidden from assistive technology.

HTML
<main inert>...</main>
<aside class="drawer">...menu that is now open...</aside>

Remove the attribute (main.inert = false) when the drawer closes. A <dialog> opened with showModal() makes the rest of the page inert automatically; see Dialogs and Modals.

Hidden things must not be focusable. Content hidden with hidden, display: none or visibility: hidden is skipped by the keyboard automatically. Content that is only moved off-screen or made transparent is still focusable, so add inert to it.

Managing focus with JavaScript

Whenever your script changes the page in a big way, ask: where should focus go now? Some rules of thumb:

  • When opening something (a menu, a dialog, a panel), move focus into it if the user is expected to interact with it.
  • When closing it, send focus back to the control that opened it. Otherwise focus falls back to the top of the page.
  • After an action that removes the focused element (deleting a list item, for example), move focus to something sensible nearby.
  • Don't steal focus when nothing the user did asked for it.

The menu below follows these rules. It is a simple disclosure: Enter or Space opens it and moves focus to the first link, Esc closes it and returns focus to the button, and when it is closed the links are hidden, so Tab never wanders into invisible items.

A keyboard-friendly menu

Roving tabindex. Complex widgets such as tab lists, toolbars and grids act as a single tab stop: Tab enters the widget, and arrow keys move between its items. This is done by giving the active item tabindex="0" and the others tabindex="-1", then moving those values as the arrows are pressed. The ARIA Authoring Practices Guide describes the expected keys for each widget.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. You want to move focus to an error summary with element.focus(), but it should not become a Tab stop. Which value do you use?

    A negative tabindex makes an element focusable by script while keeping it out of the sequential tab order.
  2. 2. Why should you avoid positive tabindex values?

    Positive values reorder focus for the whole page, so it no longer matches the visual reading order and is hard to maintain.
  3. 3. What does :focus-visible let you do?

    The browser uses heuristics, so keyboard users get a visible ring while mouse clicks on buttons usually do not show one.
  4. 4. A dropdown menu closes after the user presses Esc. Where should focus go?

    Returning focus to the opener keeps the user's place, so they can continue from where they were.
  5. 5. What does the inert attribute do to an element and its descendants?

    inert takes a whole subtree out of interaction, which is useful for content behind an open drawer or overlay.

Summary

  • Everything must work with the keyboard, focus must never get trapped, and the focused element must be visible.
  • Tab order follows the HTML source order; keep visual order and source order the same.
  • tabindex="0" adds an element to the tab order, tabindex="-1" makes it focusable by script only, and positive values should be avoided.
  • Style focus with :focus-visible instead of removing the outline.
  • Skip links let keyboard users bypass repeated navigation; inert removes whole areas from interaction.
  • When scripts open or close things, move focus on purpose and return it to where the user was.

Next up: HTML Style Guide.