3. Basic WAI-ARIA
Using Tabindex
As you may know, the HTML tabindex
attribute is a way to order the path the cursor takes as users use the Tab key to navigate through a website or web application. In general, however, you want to avoid using tabindex in this way, particularly when it disrupts the default tab order, which may end up creating confusion when the cursor does not follow an expected path (i.e., left to right, top to bottom). That’s not to say don’t ever use them, but be careful.
With HTML5 and the introduction of WAI-ARIA, tabindex="0"
is added to make it possible for developers to add keyboard accessibility to an element that would not normally have keyboard functionality. For example, it might be used to make a <div>
focusable. Likewise, tabindex="-1"
is added to remove keyboard accessibility from an element. The two are likely to be used with scripting to dynamically add and remove keyboard access to elements when focus needs to be strategically placed within a widget or web application. When the tabindex
attribute is used in this way, it is referred to as a roving tabindex
.
Try This: Take a look at the tab panels throughout the Showcase site to see how the tabs in the tabpanels toggle between values “-1” and “0” to control which tab has focus, using your browser’s Inspect feature. This demo works better on a wide screen, before responsiveness kicks in. Either reduce the zoom level, or drag your browser window wider until the menu appears at the side, instead of above the content.
You can also use tabindex="0"
in a static way when context is needed to describe how to use a menu, for instance. A <div>
can be wrapped around the menu, given tabindex="0"
to make it focusable, so, when a user navigates to the <div>
, it announces instructions for using the keyboard to navigate within the menu. The following example demonstrates using tabindex
, along with aria-label
, to provide context information. If you navigate through the Showcase site above with ChromeVox, you’ll notice this strategy with the side menu, announcing how to operate the menu with a keyboard.