Keyboard navigation is how many forms of assistive technology explore and act on screen content, and Tab is one of its primary mechanisms. Because Tab moves through interactive elements in DOM order, the order of your source code must match the visual hierarchy of your design.

The elements that are tabbable are:

  • Anchors, when the href attribute is present
  • <button>
  • <input> and <textarea>, with an accompanying label
  • <select>
  • <details>
  • <audio> and <video> when controls are present
  • <object>, depending on how it is used
  • Any element with scroll overflow in Firefox
  • Any element with the contenteditable attribute applied to it
  • Any element with the tabindex attribute applied to it

An interactive element gains focus when it is navigated to via the Tab key, when it is clicked or when an anchor linking to it is followed, or when focus is set programmatically through element.focus() in JavaScript. Focus works much like hovering with a mouse cursor — you are identifying the thing you want to activate — which is why visually obvious focus styles matter.

Focus indication moving through a homepage wireframe. It starts on the logo, moves to products, then services, then careers, blog, contact, and stops on a Learn more button.

Focus management

Focus management is the practice of coordinating what can and cannot receive focus events. It is one of the trickier parts of front-end development, but it is important for accessible websites and web apps.

Leaving focus order alone

Most of the time you want to leave focus order alone. If you use <button> for buttons, the anchor element for links, <input> for user input and so on, focus works with no additional effort. Rare cases exist where you might want to apply focus out of order, or make something that typically cannot receive focus events focusable. The guidelines below cover how to do that in an accessible, intuitive way.

Working with tabindex

tabindex lets an element be focused. It accepts an integer, and its behavior changes depending on which integer is used.

  • Avoid tabindex="0" where it isn't needed. Interactive elements that can receive keyboard focus, such as <button>, don't need the attribute. Nor do non-interactive elements need tabindex to be read by assistive technology — in fact, doing so without a role and accessible name is a WCAG failure, and it creates an unexpected, difficult-to-navigate experience for people using assistive technology, who have other expected ways to read the content.
  • Use tabindex="-1" for focusing with JavaScript. It makes an element focusable via JavaScript or click/tap, but it cannot be navigated to via the Tab key. It is the tool for building accessible interactive widgets with JavaScript.
  • Never use a positive integer. A positive value overrides the expected tab order and creates a confusing, disorienting experience. One instance is bad enough; multiple declarations are a nightmare.
  • Don't build a manual focus order. Interactive elements can be tabbed to simply by being used, and the order of elements in the DOM supplies the sequence — no series of incrementing tabindex values required.

Focus trapping

Sometimes you need to prevent things from being focused. Focus trapping is the act of conditionally restricting focus events to an element and its children. It is not the same as a keyboard trap (sometimes called a focus trap), which is a situation where a keyboard navigator cannot escape a widget because of a nasty loop of poorly-written logic.

A practical use for focus trapping is a modal:

Focus indication moving through a homepage wireframe and opening a modal to demonstrate focus trapping. Inside the modal are tab stops for the modal container, a video play button, a cancel button, a purchase button, and a close button. After the modal is closed focus is returned to the button that triggered the modal.

Why it matters

Keeping focus inside a modal communicates its bounds and distinguishes modal content from the rest — analogous to how a sighted person sees a modal "float" over the page. That information matters when you have low or no vision and rely on screen reader announcements to convey the shift in interaction mode; when you have low vision and a magnified display, where focus outside the modal can be confusing and disorienting; and when you navigate solely by keyboard and could otherwise tab out of the modal and get lost on the underlying page trying to get back in.

What trapping requires

Reliable focus management is complicated and takes JavaScript to:

  1. Determine the container elements of all focusable elements on the current page or view.
  2. Identify the bounds of the trapped content, including the first and last focusable item.
  3. Remove both interactivity and discoverability from anything focusable that is outside the trapped set.
  4. Move focus into the trapped content.
  5. Listen for events that signal dismissal of the trapped content (save, cancel, Esc, and so on).
  6. Dismiss the trapped content area when a pertinent event fires.
  7. Restore the interactivity that was removed.
  8. Move focus back to the interactive element that triggered the trapped content.

It is tricky and time-consuming work, but a sensible, usable focus order is a Web Content Accessibility Guideline — important enough to be part of an international, legally-binding standard about usability.

Discoverability and the virtual cursor

Removing interactivity and discoverability at the same time takes some care. Screen readers have an interaction mode that lets them explore the page with a virtual cursor, which also reaches non-interactive content such as headings and lists; unlike Tab and focus styles, the virtual cursor is available only to screen reader users. When managing focus, you may want to restrict what the virtual cursor can discover — for a modal, preventing someone from accidentally "breaking out" of its bounds while reading. Discoverability can be suppressed with aria-hidden="true", but interactivity is more nuanced.

The inert attribute

inert is a global HTML attribute that makes removing, then restoring, the ability of interactive elements to be discovered and focused a lot easier. Here is how it works:

<body>
  <div 
    aria-labelledby="modal-title"
    class="c-modal" 
    id="modal" 
    role="dialog" 
    tabindex="-1">
    <div role="document">
      <h2 id="modal-title">Save changes?</h2>
      <p>The changes you have made will be lost if you do not save them.<p>
      <button type="button">Save</button>
      <button type="button">Discard</button>
    </div>
  </div>
  <main inert>
    <!-- ... -->
  </main>
</body>

The example deliberately avoids the <dialog> element for the modal because of its many assistive technology support issues. With inert declared on <main> following a save confirmation modal, all content within <main> can be neither focused nor clicked, and focus is restricted to the modal. When the modal is dismissed, inert is removed from <main>. This approach to focus trapping is far easier than existing techniques. Note that dismissal can come from the two buttons inside the modal, from pressing Esc, and — with some modals — from clicking outside the modal area.

Browser support

The latest versions of Edge, Chrome, and Opera support inert when experimental web platform features are enabled, and Firefox support is landing soon. The outlier is Safari, on both desktop and mobile. Native implementation by Apple would be welcome: a polyfill exists, but it has non-trivial support issues across all the major screen readers.

The inert polyfill project's README also notes a performance caveat:

The polyfill will be expensive, performance-wise, compared to a native inert implementation, because it requires a fair amount of tree-walking.

Tree-walking can demand a lot of computational power from the JavaScript polyfill and slow the end-user experience. On lower-power devices — budget Android smartphones, older laptops — and on more powerful devices doing computationally-intensive tasks such as running multiple Electron apps, that can mean freezing or crashing. Native browser support is far less taxing on the device because it can reach parts of the browser JavaScript cannot.

The Safari situation

Apple's lack of support for inert is disappointing. Adding features to a browser is genuinely complicated work, but inert seems like something Apple would have supported much earlier: macOS and iOS have historically had strong accessibility support, and assistive technology-friendly features are a regular part of the company's marketing. Apple is also tight-lipped about its roadmap, which leaves the future of inert an open question.

An attempt to fund WebKit support for inert through Igalia's Open Prioritization experiment did not win. However, iOS 15.4 will ship with support for the inert attribute disabled — a signal that Apple is working on it.

Further reading

  • Controlling focus with tabindex (A11ycasts, Episode 04)
  • Using the tabindex attribute (The Paciello Group)
  • Using JavaScript to trap focus in an element (Hidde de Vries)