👀 Live Preview
Press Tab through these controls (positive values run first, then natural order):
In new projects, prefer DOM order and tabindex="0" instead of positive numbers.

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.
Focus flow.
Natural order.
JS focus only.
Use sparingly.
tabIndex prop.
Best practice.
tabindexThe 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.
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.
Add tabindex to any HTML element with an integer value:
<button tabindex="0">Focusable in natural order</button>
<div tabindex="-1" id="modal">Focus via JS only</div>0, -1, or positive).tabindex uses the browser’s default focus behavior for that element type.HTMLElement.tabIndex (note capital I in JavaScript).The tabindex attribute accepts integer values. The three common categories are:
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.1, 2, 3…) — Focused before 0 elements, in ascending numeric order. Lower numbers first. Generally discouraged except rare legacy cases.<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">| Value | Focusable? | Tab key? |
|---|---|---|
| (default on button, link, input) | Yes | Yes, DOM order |
0 | Yes | Yes, DOM order |
-1 | Yes (via JS) | No |
1+ | Yes | Yes, before 0 items |
| JavaScript | el.tabIndex | Read/write integer |
| Focus method | el.focus() | Works on focusable els |
| Element | Default focus? | Common tabindex use |
|---|---|---|
<button>, <input> | Yes | -1 to remove from tab order |
<a href="..."> | Yes | Rarely needs tabindex |
<div>, <span> | No | 0 for custom widgets |
<a> without href | No | 0 if acting as button |
| Most HTML elements | Varies | Global attribute |
tabindex values compared| Value | Best for | Avoid when |
|---|---|---|
0 | Custom interactive components | Native button/link would work |
-1 | Modal focus, skip targets, roving focus | User must Tab to the element |
1+ | Legacy pages only | Building new accessible UI |
| DOM order (no attribute) | Most forms and navigation | Element is not natively focusable |
Custom tab order with positive values (educational), dynamic JavaScript updates, and the recommended tabindex="0" pattern.
Press Tab through these controls (positive values run first, then natural order):
In new projects, prefer DOM order and tabindex="0" instead of positive numbers.
Button, input, link, and a checkbox removed from tab order:
<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">tabindex="1", so it receives focus first among positive values.tabindex="2".tabindex="3".tabindex="-1"—focusable only via JavaScript, not Tab.Adjust focus order at runtime with the tabIndex property:
<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>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.
The recommended way to make a non-native element keyboard-accessible:
<div
role="button"
tabindex="0"
onclick="alert('Clicked!')"
onkeydown="if(event.key==='Enter'||event.key===' ')alert('Activated!')">
Custom button (Tab + Enter/Space)
</div>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.
<button> beats div tabindex="0" for free keyboard support.tabindex="-1" plus focus() to trap and restore focus in dialogs.Keyboard nav.
Positive, then 0.
Next element.
Keyboard UX.
The tabindex attribute is universally supported in all modern browsers. Focus behavior is consistent for standard values 0, -1, and positive integers.
Keyboard focus control works in Chrome, Firefox, Safari, and Edge.
Bottom line: Use tabindex confidently; follow accessibility guidelines for which values to choose.
tabindex="0" for custom interactive elementstabindex="-1" for programmatic focus targets1, 2, 3) on new sitestabindex on non-interactive content without a role and handlersoutline: none) without replacementThe 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.
tabindexBookmark these when building keyboard-accessible UI.
Focus flow
PurposeNatural order
PreferredJS focus
ProgrammaticJavaScript
DynamicUsually bad
A11yelement.focus() but skipped when tabbing. Common in modals and skip links.0.element.tabIndex = 4 (camelCase tabIndex).href, buttons, inputs, selects, textareas, and elements with contenteditable.Try the examples and press Tab to see how different tabindex values behave.
5 people found this page helpful