tabindex in CSS

Boris Hamanov picture Boris Hamanov · Feb 11, 2011 · Viewed 91.7k times · Source

Is it possible to control tabindex with CSS and if yes, which browsers support it and on which elements?

EDIT

I should say, my goal is to catch keydown events on a div. I saw this page http://www.quirksmode.org/js/events/keys.html# that tests keyboard events and it shows that the only way keydown fires on anything other than document and body or some kind of form element or link is to have tabindex declared on it. But I read on W3C site:

The following elements support the tabindex attribute: A, AREA, BUTTON, INPUT, OBJECT, SELECT, and TEXTAREA.

So I am a little confused, what to do in order to be standarts compliant and make my use case work?

EDIT2

My whole use case is a div with a lot of content with an artificial scroll bar. I am able to scroll it with mouse events but no luck with the keyboard so far.

Answer

filip picture filip · Jun 18, 2012

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 a DIV. This will work in all common browsers.

  • standards-compliant: replace DIV by an anchor element (A) without a href attribute set, style it with display: block and add the tabindex 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?