The tabindex
attribute controls the tab order of elements, making them focusable and defining their order in the tabbing sequence. This attribute can be applied to any HTML element, making it an essential tool for improving web accessibility and navigation.
tabindex="0"
includes the element in the natural tab order. The element will be focusable by keyboard navigation and will follow the order of elements as they appear in the HTML document.
tabindex="-1"
makes the element focusable but excludes it from the tab order. Use tabindex="-1"
to manage focus programmatically (e.g., via JavaScript). This is useful for modal dialogs or other elements that should receive focus based on user actions.
tabindex="1"
or higher: The element is focusable and placed in the tab order according to the number.
Elements with a tabindex
attribute are included in the tab order and can be navigated using the Tab key.
Example of using the tabindex
attribute
<div tabindex="0">Element is focusable and part of the tab order.</div>
<div tabindex="-1">Element is focusable but not part of the tab order.</div>
<div tabindex="1">Element is focusable and will be focused before elements with tabindex="0".</div>