tabindex (HTML attribute)

Share this article

Description

The tabindex is used to define a sequence that users follow when they use the Tab key to navigate through a page. By default, the natural tabbing order will match the source order in the markup. In certain circumstances it may be necessary to override the default tabbing order, but it’s strongly recommended that you craft a page in a logical flow and let the browser work through it in the default order—an approach that negates the need for the tabindex attribute. A tabindex can start at 0 and increment in any value. As such, the sequence 1, 2, 3, 4, 5 would be fine, as would 10, 20, 30, 40, 50. If you need to introduce a tabindex, it’s advisable to use a sequence that contains intervals (like the second example provided), as this will give you the opportunity to inject other controls later if need be (for example, 10, 15, 20) without having to reindex all the tabindex values on the page. Should a given tabindex value be applied to more than one element (e.g. all links in one section given a tabindex
of “1”, and sidebar links given a tabindex of “2”), the tabbing order of those affected elements will be as per the source markup order. Many people will choose to use this approach rather than a sequence with a defined interval, such as 5, 10, 15, because it allows for additional links or form controls to be added without the headache of re-numbering. If a tabindex of “-1” is used, the element it’s applied to will no longer be keyboard focusable. If a tabindex is set anywhere on a page—even if it’s the hundredth link or the fiftieth form control—the tab order will start at the element with the lowest tabindex value, and work through the increments. Only then will the tab order take in the remaining elements for which no tabindex has been set. As such, great care must be taken to ensure that adding a tabindex doesn’t harm the usability of the page as a whole. If the disabled attribute is set on an element which has a tabindex, that tabindex
will be ignored.

Example

The tabindex is set to "3" for the link below:

<p>You can try our <a href="cakes.html" tabindex="3">lovely
     range of cakes</a>.</p>

Value

This attribute can take any numeric value.

Frequently Asked Questions about the Tabindex HTML Attribute

What is the purpose of the tabindex attribute in HTML?

The tabindex attribute in HTML is used to specify the order in which elements will receive focus when a user navigates through a webpage using the keyboard. This attribute is particularly useful for improving the accessibility of a webpage, as it allows users who rely on keyboard navigation to interact with the page in a logical and intuitive order.

How does the tabindex attribute work?

The tabindex attribute works by assigning a numerical value to each focusable element on a webpage. Elements with a tabindex value of 0 are included in the default navigation order, while elements with a positive tabindex value are navigated to in the order of their values. Elements with a negative tabindex value are excluded from the default navigation order.

Can I use the tabindex attribute on any HTML element?

The tabindex attribute can be used on any HTML element, but it is most commonly used on interactive elements such as links, form inputs, and buttons. Using the tabindex attribute on non-interactive elements can lead to a confusing navigation experience for users, so it is generally recommended to avoid doing so.

What happens if I don’t specify a tabindex value for an element?

If you don’t specify a tabindex value for an element, the browser will determine the navigation order based on the element’s position in the HTML document. Elements that come earlier in the document will receive focus before elements that come later.

Can I use negative values for the tabindex attribute?

Yes, you can use negative values for the tabindex attribute. An element with a tabindex value of -1 can still receive focus programmatically (for example, through JavaScript), but it will be excluded from the default keyboard navigation order.

How can I use the tabindex attribute to improve accessibility?

You can use the tabindex attribute to improve accessibility by ensuring that all interactive elements on your webpage can be reached and operated using the keyboard. This is particularly important for users who rely on assistive technologies, as these technologies often rely on keyboard navigation.

What are some common mistakes to avoid when using the tabindex attribute?

Some common mistakes to avoid when using the tabindex attribute include using positive values unnecessarily, using the attribute on non-interactive elements, and failing to specify a tabindex value for interactive elements. These mistakes can lead to a confusing or inaccessible navigation experience for users.

How does the tabindex attribute interact with other HTML attributes?

The tabindex attribute can interact with other HTML attributes in a number of ways. For example, if an element has a tabindex value and is also disabled (using the disabled attribute), the element will be excluded from the keyboard navigation order.

Can I use the tabindex attribute with JavaScript?

Yes, you can use the tabindex attribute with JavaScript. For example, you can use JavaScript to dynamically change the tabindex value of an element, or to programmatically focus on an element with a specific tabindex value.

How can I test the effectiveness of my tabindex implementation?

You can test the effectiveness of your tabindex implementation by navigating through your webpage using only the keyboard. If you can reach and operate all interactive elements in a logical order, your tabindex implementation is likely effective.

Adam RobertsAdam Roberts
View Author

Adam is SitePoint's head of newsletters, who mainly writes Versioning, a daily newsletter covering everything new and interesting in the world of web development. He has a beard and will talk to you about beer and Star Wars, if you let him.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week