HTML tabindex Attribute: How it Works with Examples

html tabindex attribute

The HTML tabindex Attribute lets you change the tab order on a web page. It helps control keyboard access.

What Is the HTML tabindex Attribute?

The HTML tabindex Attribute sets the order of elements when someone uses the Tab key. It helps users move from one part to another on the keyboard.

Some users use a keyboard instead of a mouse. This attribute helps them move through form fields or buttons without trouble.

You add the attribute to any element that supports focus. Just write tabindex with a number inside the tag.

<button tabindex="1">Click Me</button>

This button now gets focus before any element with a higher number.

You can set tabindex to three types of values. Each one changes how the focus works on a page.

  • A positive number like 1, 2, or 3 places the element in a set order.
  • A zero lets the element join the default tab order.
  • A negative like -1 removes the element from the tab chain, but still allows manual focus.

When you use tabindex, you take control of the path a user follows with the Tab key. The browser follows numbers in order. After those, it goes to items without tabindex.

When to Use the tabindex Attribute (and When Not To)

You can use tabindex when you need to set how users move through parts of your page by keyboard. It helps when the default tab order does not fit the layout or flow.

Custom Parts That Need Focus

Some HTML elements do not receive focus with the tab. Add tabindex="0" to make them focusable. That will make them act like form fields.

<div tabindex="0">Click or focus this part</div>

You can do this when you build a menu or tab with just <div> and want users to move to it with a keyboard.

Make Element Focusable Only by Script

You can stop a user from reaching a part by Tab, but still let the script send focus there. To do that, use tabindex="-1". It tells the browser to skip the part when the user presses Tab.

<div tabindex="-1" id="box">Hidden content here</div>

This helps with modals or alerts. The page does not send focus to them unless a script says so.

Change the Form Flow

When you use a form, each field gets a spot in the tab order. But sometimes you want one field to come before another. You can set a number in tabindex to move fields as you like.

<input type="text" tabindex="2"> <input type="text" tabindex="1">

The second field will come first now. But this works best when the form has a short list. Too many numbers may break the flow.

Keep Focus Inside a Box

You can create a focus trap inside a part of the page. This ensures keyboard users stay inside a modal or dialog until it closes.

Set it on the box and return it to the first button when the user presses Tab after the last one. You need a script for the loop, but tabindex="0" makes the part focusable.

<div tabindex="0" id="popup"> 
  <button>Yes</button> <button>No</button> 
</div>

This helps when you show a pop-up or form. You want the user to stay inside it until the task ends.

Examples

Set Custom Tab Order for Form Fields:

<input type="text" tabindex="2" placeholder="Last Name">
<input type="text" tabindex="1" placeholder="First Name">
<input type="submit" value="Submit">

This example places the “First Name” field first. The browser moves to “Last Name” second. It skips natural order and follows the numbers.

Remove Item from Tab Flow:

<button>Save</button>
<button tabindex="-1">Cancel</button>

The “Cancel” button does not get focus when the user presses Tab. It still works with other actions like mouse click or script focus.

Add tabindex to Custom Element:

<div tabindex="0">Press Tab to focus this box</div>

Elements like <div> or <span> are not focusable by default. Use tabindex="0" to make them part of the tab order. This example allows the div to receive focus when you press Tab.

Control Focus in Interactive Widgets:

<div role="menu">
  <div role="menuitem" tabindex="0">Open</div>
  <div role="menuitem" tabindex="-1">Save</div>
  <div role="menuitem" tabindex="-1">Exit</div>
</div>

Only the first item gets focus at first. You can change focus with the arrow keys or JavaScript later. This helps when you build custom widgets.

Wrapping Up

In this article, you learned what the tabindex attribute does and how to use it. You also saw how it works with focus and keyboard flow.

Here is a quick recap:

  • You can set tab order with tabindex.
  • You can remove items from the tab chain.
  • You can add focus to custom elements.
  • Use it to help people who use keyboards.

FAQs

What does the HTML <tabindex> attribute do?

The HTML <tabindex> attribute controls the keyboard navigation order. You can add it to elements to change how the Tab key moves through content.
<button tabindex="1">First</button>
<button tabindex="2">Second</button>
Elements with lower tabindex values come first when tabbing.

How to make an element focusable using <tabindex>?

Use <tabindex="0"> to make non-focusable elements focusable in natural order. Use negative values to remove them from tab order.
<div tabindex="0">Focusable Div</div>
tabindex="-1" allows focus by script only, not with the Tab key.

What is the difference between <tabindex="0"> and <tabindex="-1">?

tabindex="0" adds an element to the normal tab order. tabindex="-1" removes it from tab order but still allows focus via JavaScript.
<div tabindex="0">Focusable by Tab</div>
<div tabindex="-1">Focusable by JS only</div>

Can you use <tabindex> with <input> and <button> tags?

Yes, you can override the default tab order by setting tabindex manually on form elements like <input> or <button>.
<input type="text" tabindex="2">
<button tabindex="1">Click Me First</button>

Similar Reads

HTML link Tag: Linking Stylesheets

The link tag in HTML connects a web page to external resources like stylesheets or icons. It's placed in the…

HTML noscript Tag: How to Display Fallback When JS Is Disabled

The HTML noscript tag tells the browser what to show when JavaScript does not run. This tag targets users who…

HTML code Tag: How to Mark Up Code Snippets

The <code> tag in HTML shows short code, command names, or output. Use it to display text that the browser…

HTML head Tag: How to Include Metadata and Links in head

The HTML head tag contains information and loads tools for the web page. What Is the <head> Tag in HTML?…

HTML samp Tag: How to Show Sample Program Output

The HTML samp tag shows the result of a computer program or command. It helps readers know which text came…

How to Build Responsive HTML Email Templates

Email templates are still the key tools for work and brands in HTML. A simple email can reach many people,…

HTML p Tag: How to Create a Paragraph in HTML

Paragraphs help group-related text. They build clear, readable pages. You use them to split ideas or topics. The HTML p…

HTML meter Tag: Measurements with Examples

Purpose and use cases: The HTML meter tag shows a scalar value within a known range. You can use it…

The Button Tag in HTML: How it Works with Examples

The HTML button tag creates interactive controls on a web page. You can use this element to trigger actions or…

HTML Heading Elements: Understanding h1 to h6 with Examples

The heading elements define a content structure in HTML. Use <h1> to <h6> to show levels. <h1> means the main…

Previous Article

HTML progress Tag: How to Create Progress Bars with Examples

Next Article

HTML meter Tag: Measurements with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.