0

Considering the following code:

button{background:yellow;}
button:focus{outline:red 5px solid;}

button:nth-of-type(1){color:green;}
button:nth-of-type(2){color:orange;}
button:nth-of-type(3){color:blue;}
button:nth-of-type(4){color:grey;}
button:nth-of-type(5){color:purple;}
<section>
  <button type="button">Button 1</button>
  <button type="button">Button 2</button>
  <button type="button">Button 3</button>
  <button type="button">Button 4</button>
  <button type="button">Button 5</button>
</section>

Note that when clicking on the key tab on your keyboard it make the focus jump from the first button to the next and so on all the way to the last before it moves to the browser elements.

My question is if is there a way to skip "Button 3" when focus is on "Bottom 2" and I press the key tab? I would be happy to know that there is a CSS property to control this but if not javascript solutions are also welcome.

cimmanon
  • 67,211
  • 17
  • 165
  • 171
Vinicius Santana
  • 3,936
  • 3
  • 25
  • 42
  • https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex – Derek 朕會功夫 Nov 25 '15 at 08:07
  • 1
    Possible duplicate of [HTML remove element from tab index](http://stackoverflow.com/questions/5192859/html-remove-element-from-tab-index) – Joel Almeida Nov 25 '15 at 08:09
  • Not sure what you're trying to achieve ("why is there a 3rd button between 2nd and 4th buttons and focusable if it shouldn't be focused in-between these 2nd and 4th buttons?") but messing with what is perceivable not being focusable causes accessibility problems (and ergonomics ones I guess). [W3C/WAI Creating Logical Tab Order with the Tabindex Attribute](http://www.w3.org/WAI/GL/wiki/Creating_Logical_Tab_Order_with_the_Tabindex_Attribute) has a description of the problems and interesting resources about it. You can hide the 3rd button or disable it if it makes sense in your real project. – FelipeAls Nov 25 '15 at 09:17
  • I just used buttons to illustrate the problem. I mostly need a way to make a certain element that would normally be focused when pressing TAB as inputs, buttons and tables to be skipped when pressing TAB. Tabindex does it but it goes on the markup. I was hopping to find a way to do it with pure css but as per conversation with @Derek朕會功夫 , ```nav-index``` never became a thing. :( – Vinicius Santana Nov 25 '15 at 09:42

1 Answers1

6

You would be surprised, but this time it's actually not CSS or JS, but HTML doing the work.

button{background:yellow;}
button:focus{outline:red 5px solid;}

button:nth-of-type(1){color:green;}
button:nth-of-type(2){color:orange;}
button:nth-of-type(3){color:blue;}
button:nth-of-type(4){color:grey;}
button:nth-of-type(5){color:purple;}
<section>
  <button type="button">Button 1</button>
  <button type="button">Button 2</button>
  <button type="button" tabindex="-1">Button 3</button>
  <button type="button">Button 4</button>
  <button type="button">Button 5</button>
</section>

Or with JS,

[].map.call(document.getElementsByClassName("no-tab"), function(e){ e.tabIndex = -1; });
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • Oh wow!! This is very awesome. Thanks for the link too. Do you know if is there anyway to control it with CSS? – Vinicius Santana Nov 25 '15 at 08:12
  • @ViniciusSantana `nav-index` was suggested and [included in CSS3 draft, but was later removed.](http://www.w3.org/TR/css3-ui/#changes-since-2012) So basically you are sticked with HTML to control your tab sequences. – Derek 朕會功夫 Nov 25 '15 at 08:15
  • I meant something like button:nth-of-type(3){ tabindex:-1; } instead of – Vinicius Santana Nov 25 '15 at 08:16
  • @ViniciusSantana Read my comment, it answered your question. – Derek 朕會功夫 Nov 25 '15 at 08:17
  • Yeah. We replied at the same time. You are right. No browser support at all for it http://www.w3schools.com/cssref/css3_pr_nav-index.asp . :( – Vinicius Santana Nov 25 '15 at 08:18
  • @ViniciusSantana I guess you are trying to remove a lot of elements from the tab sequence. In that case, just set them with JS `[].map.call(document.getElementsByClassName("no-tab"), function(e){ e.tabIndex = -1; })` – Derek 朕會功夫 Nov 25 '15 at 08:21
  • That's an alternative but still would require me to edit the markup adding and removing classes. I would like to be able to control that property with the styles only without the use of classes. – Vinicius Santana Nov 25 '15 at 08:36
  • @ViniciusSantana Well you can always pass in whatever CSS selector you are using to `.querySelectorAll` like this `document.querySelectorAll("button:nth-of-type(4)")` – Derek 朕會功夫 Nov 25 '15 at 08:38