tabindex #
::: section-content
The tabindex
global attribute allows
developers to make HTML elements focusable, allow or prevent them from
being sequentially focusable (usually with the [Tab]{.kbd} key, hence
the name) and determine their relative ordering for sequential focus
navigation.
:::
Try it #
::: section-content ::: iframe ::: {.output-header .border-rounded-top}
HTML Demo: tabindex #
Reset :::
::: {#warning-no-script .warning-container} ::: warning The interactive example cannot be shown because JavaScript is disabled. ::: :::
::: {#warning-mathml-not-supported .warning-container .hidden} ::: warning The interactive example cannot be shown because MathML is not supported by your browser. ::: :::
::: {#editor-container .editor-container .tabbed-standard .hidden .border-rounded-bottom editor-type=“tabbed”} ::: {#tab-container .section .tabs} ::: {#tablist .tab-list role=“tablist”} HTML
CSS
JavaScript :::
::: {#html-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“html” aria-hidden=“true”} ::: {#html-editor}
Click anywhere in this pane, then try tabbing through the elements.
<label>First in tab order:<input type="text" /></label>
<div tabindex="0">Tabbable due to tabindex.
Not tabbable: no tabindex.
<label>Third in tab order:<input type="text" /></label>
::: :::
::: {#css-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“css” aria-hidden=“true”} ::: {#css-editor} p { font-style: italic; font-weight: bold; }
div,
label {
display: block;
letter-spacing: 0.5px;
margin-bottom: 1rem;
}
div:focus {
font-weight: bold;
}
::: :::
::: {#js-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“js” aria-hidden=“true”} ::: {#js-editor} ::: ::: :::
::: {#output .output-container}
Output #
::: :::
::: {.section .console-container .hidden aria-hidden=“true”}
Console Output #
![] clear console
::: {#console .console} ::: :::
::: {#html-output .output .editor-tabbed} %html-content% ::: :::
It accepts an integer as a value, with different results depending on the integer's value:
::: {#sect1 .notecard .note}
Note: If an HTML element renders and has tabindex
attribute with
any valid integer value, the element can be focused with JavaScript (by
calling the
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]{.kbd} 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.::: {#sect2 .notecard .note} Note:
tabindex="-1"
may be useful for elements that should not be navigated to directly using the [Tab]{.kbd} 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 positivetabindex
values. 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 beforetabindex="5"
andtabindex="0"
, but aftertabindex="3"
. If multiple elements share the same positivetabindex
value, their order relative to each other follows their position in the document source. The maximum value fortabindex
is 32767.If the
tabindex
attribute is included with no value set, whether the element is focusable is determined by the user agent.::: {#sect3 .notecard .warning} Warning: You are recommended to only use
0
and-1
astabindex
values. Avoid usingtabindex
values greater than0
and 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
<a>
or
<area>
with href
attribute,
<button>
,
<frame>
[Deprecated]{.visually-hidden} ,
<iframe>
,
<input>
,
<object>
,
<select>
,
<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).
::: {#sect4 .notecard .warning}
Warning: The tabindex attribute must not be used on the
<dialog>
element.
:::
:::
Accessibility concerns #
::: section-content
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
`` element to
describe a button, instead of the
<button>
element.
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 (
<a>
,
<button>
,
<details>
,
<input>
,
<select>
,
<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.
- Using the tabindex attribute | The Paciello Group{target="_blank"} :::
Specifications #
::: _table #
Specification #
HTML Standard
[#
attr-tabindex]{.small}
:::
Browser compatibility #
::: _table Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
tabindex
1 12 1.5 Yes 15 ≤4 4.4 18 4 14 ≤3.2 1.0
:::
See also #
::: section-content
- All global attributes
HTMLElement.tabIndex
that reflects this attribute- Accessibility problems with
tabindex
: see Don't Use Tabindex Greater than 0{target="_blank"} by Adrian Roselli :::
::: _attribution
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5
or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex{._attribution-link}
:::