Reputation: 8485
This is more a historical or why question.
Unlike buttons, links tend to have a tabIndex
of -1
as default value. I know this is because:
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex
If not specified default value is -1
But buttons have a tabIndex
automatically, so the question is: What was the rationale behind deciding that links shouldn't need to have a tabIndex
bigger than 0? and because of that the Tab key should ignore the links in a default state.
Upvotes: 5
Views: 6640
Reputation: 18807
The MDN page you quote is the definition of the tabindex
global attribute . This does not apply specifically to button
or links.
In fact, tabindex
can have four type of values:
-1
: the element can be focused programmatically or with the mouse0
: the element can be focused programmatically, with the keyboard or with the mouse>0
: do not use this. Never. Not accessible.The default values for link (a[href]
and button
) or other interactive elements (like form fields) is 0
The assertion that the default value for any other element is -1
is false. The default "value" for those other elements is to not define this attribute
Upvotes: 3
Reputation: 96587
HTML 5.1¹ defines what user agents should do when the tabindex
attribute is omitted:
The user agent should follow platform conventions to determine if the element’s tabindex focus flag is set […]
The spec suggests a few elements that should get this flag, including a
elements (as long as they have an href
attribute) and button
elements.
So user agents that follow the spec’s recommendation² don’t make a distinction between a
and button
elements with regards to the default focus behaviour.
I’m not sure why the linked MDN page contains the quoted statement (it seems to be wrong). Maybe it’s a misunderstanding of what the spec says about the tabIndex
IDL attribute?
The
tabIndex
IDL attribute must reflect the value of thetabindex
content attribute. Its default value is 0 for elements that are focusable and -1 for elements that are not focusable.
¹ HTML 5.1 is, as of now, the latest W3C Recommendation. But previous HTML W3C Recommendations and WHATWG’s HTML Living Standard probably don’t define this aspect differently w.r.t. to a
and button
.
² It’s not a requirement, as the spec uses should instead of must.
Upvotes: 3