Introduction to Concept
According to MDN-tabindex, tabindex
is a global DOM element attribute used to indicate if the element is keyboard focusable (via tab) or not:
- By setting
tabindex="-1"
, element is not reachable via sequential keyboard navigation. - By setting
tabindex="0"
, the element should be focusable in sequential keyboard navigation.
(*p.s. You may also set other positive values such as tabindex="2/3/4/5/..."
, to indicate the order/sequance for the focus, but generally this should be AVOIDED, due to its potential of causing difficulty for people solely relying on keyboard for navigation and those who are using other assistive technology to navigate around the page)
HTML Component Focusable by Default
Some of the HTML element by default have a tabindex=0
, these elements are:
<a>
/<area>
: focusable only if you have thehref
attribute set, if you leave it empty it cannot be focused<input>
/<textarea>
: focusable when you do not havehidden
<input>
/<object>
/<select>
/<iframe>
(*p.s. one exception is the <dialog>
element, the tabindex attribute must not be used for <dialog>
element)
Transform Any Component to Focusable
You may add tabindex=0
as an attribute to any HTML component to make then focusable, for instance the below example uses jQuery to transform a <div>
into focusable component that:
|
|
You may also wanna add some styling to that button:
|
|
And ideally some ARIA information since <div>
by default does not contain/provide much information for accessibility tool such as screen reader, especially here we do not even have any information within the <div>
it self (its content is ⌘
):
|
|
You can find a full list of such ARIA roles and ARIA properties/attributes via: the following:
Reference
- https://content.industries/2020/12/22/what-do-developers-need-to-know-about-tab-focus/#:~:text=Most of the commonly used,” elements%2C and text areas.
- https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex
- https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA
- https://tenor.com/view/tab-tab-key-key-keyboard-wide-gif-23888112