HTML tabindex Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Keyboard & Accessibility

Introduction

The tabindex attribute lets you control whether an HTML element can receive keyboard focus and where it appears when users press the Tab key. It is a global attribute, meaning it can be used on most elements.

Used thoughtfully, tabindex improves accessibility for keyboard and assistive technology users. Used carelessly—especially with positive numbers like 1, 2, 3—it can create confusing focus jumps.

What You’ll Learn

01

Tab order

Focus flow.

02

tabindex 0

Natural order.

03

tabindex -1

JS focus only.

04

Positive #

Use sparingly.

05

JavaScript

tabIndex prop.

06

A11y tips

Best practice.

Purpose of tabindex

The primary purpose of the tabindex attribute is to influence focus behavior when a user navigates with the keyboard. Interactive elements like <button>, <a href>, and <input> are focusable by default; tabindex can extend focus to custom widgets or remove elements from the tab sequence.

It is particularly useful for accessibility: ensuring custom controls participate in keyboard navigation, moving focus into modals with JavaScript, or keeping decorative elements out of the tab order.

💡
Prefer tabindex=“0” over positive numbers

Values 1, 2, 3… create a separate tab queue that runs before naturally focusable elements. Modern accessibility guidance recommends DOM order plus tabindex="0" when you need a non-native element to be focusable.

📝 Syntax

Add tabindex to any HTML element with an integer value:

tabindex.html
<button tabindex="0">Focusable in natural order</button>
<div tabindex="-1" id="modal">Focus via JS only</div>

Syntax Rules

  • Global attribute — valid on nearly all HTML elements.
  • Value must be a valid integer (typically 0, -1, or positive).
  • Omitting tabindex uses the browser’s default focus behavior for that element type.
  • IDL property: HTMLElement.tabIndex (note capital I in JavaScript).
  • Pair custom focusable elements with keyboard event handlers and ARIA roles where appropriate.
  • Never use positive tabindex as a default styling trick.

💎 Values

The tabindex attribute accepts integer values. The three common categories are:

  • No attribute / browser default — Native interactive elements (links with href, buttons, inputs) are focusable in DOM order. Non-interactive elements are not.
  • tabindex="0" — Element becomes focusable and is inserted into the natural tab order at its DOM position. Recommended for custom widgets like a div acting as a button.
  • tabindex="-1" — Element can receive focus via JavaScript (element.focus()) but is skipped by the Tab key. Useful for modals, skip targets, or moving focus programmatically.
  • Positive integers (1, 2, 3…) — Focused before 0 elements, in ascending numeric order. Lower numbers first. Generally discouraged except rare legacy cases.
tabindex-values.html
<button tabindex="1">First (positive queue)</button>
<input type="text" tabindex="2">
<a href="#" tabindex="3">Link</a>
<input type="checkbox" tabindex="-1" id="hiddenCheckbox">

⚡ Quick Reference

ValueFocusable?Tab key?
(default on button, link, input)YesYes, DOM order
0YesYes, DOM order
-1Yes (via JS)No
1+YesYes, before 0 items
JavaScriptel.tabIndexRead/write integer
Focus methodel.focus()Works on focusable els

Applicable Elements

ElementDefault focus?Common tabindex use
<button>, <input>Yes-1 to remove from tab order
<a href="...">YesRarely needs tabindex
<div>, <span>No0 for custom widgets
<a> without hrefNo0 if acting as button
Most HTML elementsVariesGlobal attribute

tabindex values compared

ValueBest forAvoid when
0Custom interactive componentsNative button/link would work
-1Modal focus, skip targets, roving focusUser must Tab to the element
1+Legacy pages onlyBuilding new accessible UI
DOM order (no attribute)Most forms and navigationElement is not natively focusable

Examples Gallery

Custom tab order with positive values (educational), dynamic JavaScript updates, and the recommended tabindex="0" pattern.

👀 Live Preview

Press Tab through these controls (positive values run first, then natural order):

tabindex 3

In new projects, prefer DOM order and tabindex="0" instead of positive numbers.

Example — custom tab order

Button, input, link, and a checkbox removed from tab order:

tabindex.html
<button tabindex="1">Click me</button>
<input type="text" tabindex="2" placeholder="Type something">
<a href="#" tabindex="3">Visit our website</a>
<input type="checkbox" tabindex="-1" id="hiddenCheckbox">
Try It Yourself

How It Works

  • The button has tabindex="1", so it receives focus first among positive values.
  • The text input has tabindex="2".
  • The link has tabindex="3".
  • The checkbox has tabindex="-1"—focusable only via JavaScript, not Tab.

Dynamic Values with JavaScript

Adjust focus order at runtime with the tabIndex property:

dynamic-tabindex.html
<input type="text" placeholder="First" tabindex="1">
<input type="text" id="dynamicElement" placeholder="Moved by JS">
<input type="text" placeholder="Third" tabindex="3">

<script>
  document.getElementById("dynamicElement").tabIndex = 2;
</script>

How It Works

In this script, tabIndex is set to 2 on dynamicElement, inserting it between the first and third fields in the positive tab queue. Useful when UI state changes (such as revealing a panel) require a new focus path.

Example — tabindex=“0” on a custom control

The recommended way to make a non-native element keyboard-accessible:

tabindex-zero.html
<div
  role="button"
  tabindex="0"
  onclick="alert('Clicked!')"
  onkeydown="if(event.key==='Enter'||event.key===' ')alert('Activated!')">
  Custom button (Tab + Enter/Space)
</div>

How It Works

tabindex="0" adds the div to the natural tab order. Pair it with role="button" and keyboard handlers—or better yet, use a real <button> when possible.

♿ Accessibility

  • Preserve logical DOM order — Reordering with positive tabindex often harms predictability for screen reader users.
  • Use native elements first — A real <button> beats div tabindex="0" for free keyboard support.
  • Visible focus indicators — Never remove focus outlines without providing an accessible alternative.
  • Manage modal focus — Use tabindex="-1" plus focus() to trap and restore focus in dialogs.
  • Test with keyboard only — Unplug the mouse and Tab through every interactive path.

🧠 How tabindex Works

1

User presses Tab

Keyboard nav.

Input
2

Browser builds queue

Positive, then 0.

Order
3

Focus moves

Next element.

Focus
=

Accessible flow

Keyboard UX.

Browser Support

The tabindex attribute is universally supported in all modern browsers. Focus behavior is consistent for standard values 0, -1, and positive integers.

Universal · Fully supported

Reliable everywhere

Keyboard focus control works in Chrome, Firefox, Safari, and Edge.

100% Modern browsers
Google Chrome All versions
Supported
Mozilla Firefox All versions
Supported
Apple Safari All versions
Supported
Microsoft Edge All versions
Supported
tabindex attribute Excellent

Bottom line: Use tabindex confidently; follow accessibility guidelines for which values to choose.

💡 Best Practices

✅ Do

  • Use tabindex="0" for custom interactive elements
  • Use tabindex="-1" for programmatic focus targets
  • Keep focus order matching visual layout
  • Test pages with keyboard-only navigation
  • Prefer semantic HTML elements over tabindex hacks

❌ Don’t

  • Use positive tabindex (1, 2, 3) on new sites
  • Set tabindex on non-interactive content without a role and handlers
  • Remove visible focus styles (outline: none) without replacement
  • Trap users in broken tab loops
  • Rely on tabindex when reordering DOM is simpler

Conclusion

The tabindex attribute is a valuable tool for improving keyboard accessibility and user experience. Understanding the difference between 0, -1, and positive values helps you make inclusive, navigable interfaces.

Use it thoughtfully: default DOM order for most forms, 0 for custom widgets, and -1 when JavaScript manages focus. That approach keeps your pages predictable for everyone.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about tabindex

Bookmark these when building keyboard-accessible UI.

5
Core concepts
0 02

tabindex 0

Natural order

Preferred
−1 03

tabindex -1

JS focus

Programmatic
⚙️ 04

tabIndex

JavaScript

Dynamic
05

Avoid 1+

Usually bad

A11y

❓ Frequently Asked Questions

It controls whether an element is focusable and its position in the keyboard Tab sequence.
Makes an element focusable in the natural DOM tab order. Best choice for most custom widgets.
Focusable with element.focus() but skipped when tabbing. Common in modals and skip links.
Rarely. Positive values jump ahead of natural order and confuse users. Prefer DOM order or 0.
Yes. Use element.tabIndex = 4 (camelCase tabIndex).
Links with href, buttons, inputs, selects, textareas, and elements with contenteditable.

Practice keyboard focus

Try the examples and press Tab to see how different tabindex values behave.

Try basic tab order →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful