tabindex in CSS
Solution 1:
Update 2017
As pointed out by @Wallop in the comments, the nav-index
property was dropped from the spec in 2015 due to "lack of implementation interest".
Take a look at the nav-index
property introduced by W3C in CSS3-UI.
This property has exactly the same behavior as a tabindex
and is applicable to any element.
The ‘nav-index’ property is an input-method-neutral way of specifying the sequential navigation order (also known as "tabbing order"). This property is a replacement for the HTML4/XHTML1 attribute ‘tabindex’
Being probably the best standards-compliant solution for the use case, nav-index
is interpreted only by Opera so far (as of June 2012) and is also marked as "Feature at risk" by W3C, therefore may be dropped any time.
Alternative cross-browser solutions are:
non-standards-compliant: set the
tabindex
attribute on aDIV
. This will work in all common browsers.standards-compliant: replace
DIV
by an anchor element (A
) without ahref
attribute set, style it withdisplay: block
and add thetabindex
attribute.
With respect to BoltClock´s point, I agree that the tabbing order is very logical (both the text selection order and tabbing order are closely related to the sequence in which elements are aranged in the document). On the other hand, CSS has a wider purpose today. It can manipulate not just the content of a document (content
property) but also the behavior when and if events are to be fired: i.e. using pointer-events
, display
or z-index
the pointer event's order will change. If these are very basic CSS properties, why you should not be able to influence KeyBoardEvents, too?
Solution 2:
This is a bit old but now there are css options like -moz-user-focus. I'm sure there is a webkit equivalent.
https://developer.mozilla.org/en-US/docs/CSS/-moz-user-focus
user-focus
is the standard cross browser attribute.