How do you make a div "tabbable"?
The gist is simple, make a div
keyboard-focusable by setting the tabindex="0"
attribute:
This magic line empowers users to approach the div
by tapping the Tab key. The tabindex="0"
attribute places this element orderly in the page's keyboard tab sequence. To polish the user engagement, you might want to deal with the div
focus style using :focus
in your CSS, and for upgraded interactivity, utilize JavaScript click events.
The nitty-gritty of keyboard engagement
Dressing up your focus style
Making divs act like buttons
Now, if you desire to make your div
play the role of a real-deal button, marry JavaScript with your DOM to whip up an event upon focus and keyboard inputs like Enter or Space:
Highlighting clickable friends
Don't play hide and seek. Make your div
scream "I'm clickable!" with onclick
attribute or a distinctive class:
Custom tab sequences - use with caution!
Sometimes, you might need to force a custom tab order with positive numeric values for tabindex
. However, tread lightly and use it as a last resort as this can disrupt the natural flow and confuse your users:
Molding divs for diverse use-cases
Crafting a keyboard-navigable grid
Imagine crafting an interactive grid where each cell is your puppet, dancing on the tunes of keyboard navigation:
Keeping some elements aloof
Keeping user experience in mind, some elements might prefer to keep a low profile. Bless them with the inert
attribute to disable interactivity:
Summoning a handy jQuery plugin
For those still riding the jQuery train, here's a reusable plugin for you:
Was this article helpful?