tabindex global attribute allows developers to make HTML elements focusable, allow or prevent them from being sequentially focusable (usually with the Tab key, hence the name) and determine their relative ordering for sequential focus navigation.
It accepts an integer as a value, with different results depending on the integer's value:
Note: If an HTML element renders and has
focus() method) or visually by clicking with the mouse. The particular
tabindex value controls whether the element is
tabbable (i.e. reachable via sequential keyboard navigation, usually with the Tab key).
- A negative value (the exact negative value doesn't actually matter, usually
tabindex="-1") means that the element is not reachable via sequential keyboard navigation.
tabindex="-1"may be useful for elements that should not be navigated to directly using the Tab key, but need to have keyboard focus set to them. Examples include an off-screen modal window that should be focused when it comes into view, or a form submission error message that should be immediately focused when an errant form is submitted.
tabindex="0"means that the element should be focusable in sequential keyboard navigation, after any positive
tabindexvalues. The focus navigation order of these elements is defined by their order in the document source.
- A positive value means the element should be focusable in sequential keyboard navigation, with its order defined by the value of the number. That is,
tabindex="4"is focused before
tabindex="0", but after
tabindex="3". If multiple elements share the same positive
tabindexvalue, their order relative to each other follows their position in the document source. The maximum value for
- If the
tabindexattribute is included with no value set, whether the element is focusable is determined by the user agent.
Warning: You are recommended to only use
tabindexvalues. Avoid using
tabindexvalues greater than
0and CSS properties that can change the order of focusable HTML elements (Ordering flex items). Doing so makes it difficult for people who rely on using keyboard for navigation or assistive technology to navigate and operate page content. Instead, write the document with the elements in a logical sequence.
Some focusable HTML elements have a default
tabindex value of
0 set under the hood by the user agent. These elements are an
<textarea>, and SVG
<a> element, or a
<summary> element that provides summary for a
<details> element. Developers shouldn't add the
tabindex attribute to these elements unless it changes the default behavior (for example, including a negative value will remove the element from the focus navigation order).
Warning: The tabindex attribute must not be used on the
Avoid using the
tabindex attribute in conjunction with non-interactive content to make something intended to be interactive focusable by keyboard input. An example of this would be using a
<div> element to describe a button, instead of the
Interactive components authored using non-interactive elements are not listed in the accessibility tree. This prevents assistive technology from being able to navigate to and manipulate those components. The content should be semantically described using interactive elements (
<textarea>, etc.) instead. These elements have built-in roles and states that communicate status to the accessibility that would otherwise have to be managed by ARIA.
|HTML Standard |
BCD tables only load in the browser
- All global attributes
HTMLElement.tabIndexthat reflects this attribute
- Accessibility problems with
tabindex: see Don't Use Tabindex Greater than 0 by Adrian Roselli