tabindex

I have some situations on this site that I don't know what the best practices are in regards to using the tabindex HTML attribute. I am going to read some blog posts (or only one) on the topic to learn more about the best practices.

Date Created:

References



Using tabindex


The default tab order provided by the DOM position of semantic HTML elements is convenient, but there may be times you need to modify the tab order. Moving elements in the HTML is ideal, but might not be feasible. In these cases, you can use the tabindex HTML attribute to explicitly set an element's tab position.

tabindex can be applied to any element, although it's not necessarily useful on every element and takes a range of integer values. With tabindex, you can specify an explicit order for focusable page elements, insert an otherwise un-focusable element into the tab order, and remove elements from the tab order:

  • tabindex="0": Inserts an element into the natural tab order. The element can be focused by pressing TAB, and the element can be focused by calling its focus() method.
  • tabindex="-1": Removes element from natural tab order, but the element can still be focused by calling its focus() method
  • tabindex="5": Any tabindex greater than 0 brings that 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 that is greater than 0 and works its way up.

When possible, it's best to arrange your source code so the DOM sequence provides a logical tab order. Only add tabindex to content that is interactive.

The select element can receive basic focus, but once there, you can use the arrow keys to expose additional selectable options. If you build a custom select element, it's important to replicate that behavior, so keyboard users can still interact with your control.

Further Reading


When To Use tabindex="0"


When a web page includes interactive controls such as buttons, links, and form fields, it is important that they are focusable. Unless a keyboard accessible alternative is provided, these elements must be reachable using a keyboard alone. This is important for people browsing with a desktop screen reader and people browsing with a keyboard or other input device.

People navigate focusable controls on a web page using the TAB and SHIFT+TAB keys, or, for some controls like radio buttons, the arrow keys.

Some elements, like <button>s, are focusable by default while others, like <img> and <span> elements, are not.

Except for a minority of cases, avoid applying tabindex="0" to static content.

Only apply tabindex="0" to HTML elements that:

  • Are not keyboard focusable by default
  • Are provided with an interactive role
  • Have an action associated with them that requires them to be reachable using the keyboard

You need to apply tabindex="0" to elements used as containers for content where the containing element contains the CSS overflow property. In addition, you need to provide an appropriate role and accessible name to the container so that people using a screen reader are made aware of what the content represents and why the element has received keyboard focus.


tabindex Mozilla Docs


  • The 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 the relative ordering for sequential focus navigation.
  • It accepts an integer as a value.
  • A negative value means that the element is not reachable via sequential keyboard navigation.
  • tabindex="0" means that the element should be focusable in sequential keyboard navigation, after any positive tabindex values. The focus navigation order of these elements is defined by their order in the document source,
  • A positive value means that the element should be focusable in sequential keyboard navigation, with its order defined by the value of the number.
  • If the tabindex attribute is included with no value set, whether the element is focusable us determined by the user agent.

Some focusable elements that have a default tabindex value of 0 set under the hood by the user agent. These elements are:

  • <a>
  • <area>
  • <button>
  • <frame>
  • <iframe>
  • <input>
  • <object>
  • <select>
  • <textarea>
  • <summary>


User Agent


A user agent is a computer program representing a person, for example, a browser in a Web context. Besides a browser, a user agent could be a bot scraping webpages, a download manager, or another app accessing the Web. Along with each request they make to the server, browsers include a self-identifying User-Agent HTTP header called a user agent (UA) string. Spam bots, download managers, and some browsers often send a fake UA string to announce themselves as a different client. This is known as user agent spoofing.

Insert Math Markup

ESC
About Inserting Math Content
Display Style:

Embed News Content

ESC
About Embedding News Content

Embed Youtube Video

ESC
Embedding Youtube Videos

Embed TikTok Video

ESC
Embedding TikTok Videos

Embed X Post

ESC
Embedding X Posts

Embed Instagram Post

ESC
Embedding Instagram Posts

Insert Details Element

ESC

Example Output:

Summary Title
You will be able to insert content here after confirming the title of the <details> element.

Insert Table

ESC
Customization
Align:
Preview:

Insert Horizontal Rule

#000000

Preview:


Insert Chart

ESC

View Content At Different Sizes

ESC

Edit Style of Block Nodes

ESC

Edit the background color, default text color, margin, padding, and border of block nodes. Editable block nodes include paragraphs, headers, and lists.

#ffffff
#000000

Edit Selected Cells

Change the background color, vertical align, and borders of the cells in the current selection.

#ffffff
Vertical Align:
Border
#000000
Border Style:

Edit Table

ESC
Customization:
Align:

Upload Lexical State

ESC

Upload a .lexical file. If the file type matches the type of the current editor, then a preview will be shown below the file input.

Upload 3D Object

ESC

Upload Jupyter Notebook

ESC

Upload a Jupyter notebook and embed the resulting HTML in the text editor.

Insert Custom HTML

ESC

Edit Image Background Color

ESC
#ffffff

Insert Columns Layout

ESC
Column Type:

Select Code Language

ESC
Select Coding Language