--- url: "https://web.dev/articles/control-focus-with-tabindex" titulek: "Control focus with tabindex | web.dev" h1: "Control focus with tabindex Stay organized with collections Save and categorize content based on your preferences." popis: "When building custom interactive components, use tabindex to ensure that they're keyboard accessible." jazyk: "en" kanonicka: "https://web.dev/articles/control-focus-with-tabindex" drobecky: ["web.dev", "Resources"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 00:56:55" znaku: 6786 slov: 1042 kodovani: "iso-8859-2" sha256: "ea36a1509089c748728102d9317cd5084fd3a31de4dbc8de71163d7dddcb6769" --- Control focus with tabindex Stay organized with collections Save and categorize content based on your preferences. Rob Dodson Alexandra Klepper Standard HTML elements such as `` This removes an element from the natural tab order, but the element can still be focused by calling its `focus()` method. Applying `tabindex="-1"` to an element doesn't affect its children; if they're in the tab order naturally or because of a `tabindex` value, they'll remain in the tab order. To remove an element and all its children from the tab order, consider using [the WICG's `inert` polyfill](https://github.com/WICG/inert). The polyfill emulates the behavior of a proposed `inert` attribute, which prevents elements from being selected or read by assistive technologies. Avoid `tabindex > 0` Any `tabindex` greater than 0 jumps the element to the front of the natural tab order. If there are multiple elements with a `tabindex` greater than 0, the tab order starts from the lowest value greater than zero and works its way up. Using a `tabindex` greater than 0 is considered an **anti-pattern** because screen readers navigate the page in DOM order, not tab order. If you need an element to come sooner in the tab order, it should be moved to an earlier spot in the DOM. With Lighthouse, you can identify elements with a `tabindex` > 0. Run the Accessibility Audit (Lighthouse > Options > Accessibility) and look for the results of the "No element has a `[tabindex]` value greater than 0" audit. Use "roving `tabindex`" If you're building a complex component, you may need to add additional keyboard support beyond focus. When possible, use the built-in `select` element. It's focusable and allows for arrow keys to expose additional selectable options. To implement similar functions in your own components, you can use a technique known as "roving `tabindex`". Roving tabindex works by setting `tabindex` to -1 for all children except the currently-active one. The component then uses a keyboard event listener to determine which key the user has pressed. When this happens, the component sets the previously focused child's `tabindex` to -1, sets the to-be-focused child's `tabindex` to 0, and calls the `focus()` method on it. Before
After
Keyboard access recipes If you're unsure what level of keyboard support your custom components might need, you can refer to the [ARIA Authoring Practices 1.1](https://www.w3.org/TR/wai-aria-practices-1.1/). This guide lists common UI patterns and identifies which keys your components should support. Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see the [Google Developers Site Policies](https://developers.google.com/site-policies). Java is a registered trademark of Oracle and/or its affiliates. Last updated 2018-11-18 UTC.