ncubica
ncubica

Reputation: 8485

Why does tabindex default to -1 on anchor links but not buttons?

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

Answers (2)

Adam
Adam

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:

  • when unset : the element can't be focused programmatically, with the keyboard or with the mouse
  • -1 : the element can be focused programmatically or with the mouse
  • 0 : the element can be focused programmatically, with the keyboard or with the mouse
  • any value >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

unor
unor

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 the tabindex 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

Related Questions