HTML Tab Index Gotcha
The tabindex
attribute on HTML elements behaves differently than one might first expect.
The default tabindex
for an element is 0
, all elements with this index will be tabbed to in the order they appear in the document.
Giving an element a positive tabindex
will prioritize it over all elements with a 0
index, in the order they appear.
Consider the following code
<ul>
{
[0,1,2].map((n) => (
<li><button tabindex={n}>{n}</button></li>
)
}
</ul>
The order that the buttons will be focused when tabbing is 1
, then 2
, and finally 0
.