Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Make List Items "Tabbable" (Use Tab Key To :focus On Them)

Tags:

html

How can I make List Items tabbable? (meaning: using the TAB key to :focus on them). I need to know for Handicap Accessibility purposes.

I added 2 text fields to give a reference point to TAB from; if you tab from the textarea it goes to the next one, then skips over all the list items.

HTML:

<textarea></textarea>
<textarea></textarea>
<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>

CSS:

li {
  display: block;
  height: 100px;
  margin: 0 auto;
  width: 95%;
}
a:active, a:focus,
li:active, li:focus {border: 5px solid orange}

li:nth-of-type(1) {background-color: red}
li:nth-of-type(2) {background-color: yellow}
li:nth-of-type(3) {background-color: blue}
li:nth-of-type(4) {background-color: green}

jsFiddle: https://jsfiddle.net/h815zLnp/

like image 222
CSS Apprentice Avatar asked Jun 18 '16 01:06

CSS Apprentice


People also ask

How do I set my tab to focus?

Setting the value to “-1” also allows you to programmatically enable tab focus. Say you have an element that you don't want focusable on page load but after some event, you'd like to be focusable—you'd use tabindex=“-1” and the “. focus()” function to add that behavior.

Can I use Tabindex for Button?

The tabindex specifies the focus order of the <button> relative to the other buttons or controls on the page. This attribute indicates the sequential order of elements using the tab key. With a negative tabindex value, the button is not reachable by tabbing but can be accessed by mouse or JavaScript.

How do I make a div tab selectable?

Add the tabindex="0" attribute to each div you want tabbable. Then use CSS pseudo classes :hover and :focus, for example to signify to the app user that the div is in focus and clickable, for example. Use JavaScript to handle the click. Save this answer.

How do I use the Tab key in HTML?

The tab character can be inserted by holding the Alt and pressing 0 and 9 together.


2 Answers

Use the tabindex attribute - generally its used only for navigating around inputs in forms and so forth - but it can be used to determine the order of focus to any HTML element. I have used your existing text areas and li's and if you start in the first text area and use the tab key - you will be able to see the focus change to the funky order I have listed in the tabindex codes. Also shift-tab moves through the items in reverse tab index order:

li {
  display: block;
  height: 100px;
  margin: 0 auto;
  width: 95%;
}

a:active, a:focus,
li:active, li:focus {border: 5px solid orange}

li:nth-of-type(1) {background-color: red}
li:nth-of-type(2) {background-color: yellow}
li:nth-of-type(3) {background-color: blue}
li:nth-of-type(4) {background-color: green}
<textarea  tabindex="1"></textarea>
<textarea  tabindex="3"></textarea>
<ul>
  <li tabindex="2"></li>
  <li tabindex="5"></li>
  <li tabindex="4"></li>
  <li tabindex="6"></li>
</ul>
like image 117
gavgrif Avatar answered Sep 28 '22 13:09

gavgrif


The easiest way to accomplish this is to use <li tabindex="0"> with a JavaScript function to add keyboard behaviors for in-focus elements.

tabindex="0" will make sure that the tabs are accessed in the natural flow order. A solid JavaScript solution accounting for both enter and space keys is available here: https://karlgroves.com/2014/11/24/ridiculously-easy-trick-for-keyboard-accessibility

like image 34
grommit Avatar answered Sep 28 '22 11:09

grommit