Applied Accessibility -14 Use tabindex to Add Keyboard Focus to an Element
Use tabindex to Add Keyboard Focus to an Element
The HTML
tabindex
attribute has three distinct functions relating to an element's
keyboard focus. When it's on a tag, it indicates that element can be
focused on. The value (an integer that's positive, negative, or zero)
determines the behavior.
Certain elements, such as links and form controls, automatically receive
keyboard focus when a user tabs through a page. It's in the same order
as the elements come in the HTML source markup. This same functionality
can be given to other elements, such as
div, span, and p, by placing a tabindex="0" attribute on them. Here's an example:<div tabindex="0">I need keyboard focus!</div>
Note
A negative tabindex value (typically -1) indicates that an element is focusable, but is not reachable by the keyboard. This method is generally used to bring focus to content programmatically (like when a div used for a pop-up window is activated), and is beyond the scope of these challenges
The
tabindex attribute also specifies
the exact tab order of elements. This is achieved when the value of the
attribute is set to a positive number of 1 or higher.
Setting a tabindex="1" will bring keyboard focus to that element first. Then it cycles through the sequence of specified
tabindex values (2, 3, etc.), before moving to default and tabindex="0" items.
It's
important to note that when the tab order is set this way, it overrides
the default order (which uses the HTML source). This may confuse users
who are expecting to start navigation from the top of the page. This
technique may be necessary in some circumstances, but in terms of
accessibility, take care before applying it.
Here's an example:
I get keyboard focus, and I get it first!
I get keyboard focus, and I get it second!
<div tabindex="1">I get keyboard focus, and I get it first!</div> <div tabindex="2">I get keyboard focus, and I get it second!</div>
source
Comments
Post a Comment