Which HTML elements can receive focus?
Focusable elements in HTML are those which users can interact with. By default, HTML specifies certain key focusable elements:
<a href="">
: The hyperlinks in your HTML code.<button>
: Your web page buttons.<input>
: All input fields, with the exception of ones withtype="hidden"
to keep secrets.<select>
: Dropdown lists that provide user options.<textarea>
: Larger text fields for user input.<iframe>
: Embedded content frames, like the Youtube videos you embed in your site.
Any element can be made focusable by simply adding tabindex="0"
to it. For instance, <div tabindex="0">
would then be focusable via keyboard. Make sure to use tabindex
judiciously as it could affect accessibility.
Getting jquery serious with focus
Since we've dealt with the quick answer, let's dig a bit deeper. We'll look at some special cases and then let you in on some neat tricks to manage focusable elements like a pro.
Focusable odd ones out
- Think
<area>
tags with ahref
as clickable parts of an image map. - Elements with the attribute
contentEditable="true"
tell the browser the user can edit them. - Non-interactive elements can join the focus party if given a
tabindex
attribute or set ascontentEditable
.
Steering the focus to visibility
A defective lighthouse doesn't help sailors. Similarly, focusable elements should have visible focus:
The good, the bad and the tabindex
- Positive
tabindex
values rearrange the tabbing order. It's like cutting in line, frowned upon, unless absolutely necessary. - A negative
tabindex
(liketabindex="-1"
) is an unsung hero. It makes an element programmatically focusable but keeps it away from keyboard spotlight. - Need to style interactive elements? Try this CSS sorcery:
Active focus and enhanced accessibility
Use the poignant document.activeElement
property to find out which element currently holds the focus trophy. Also, ensuring a clear and logical tabbing order is your first step towards making your website more accessible.
Browser-specific quirks
Like the house rules at a friends place, each browser has its own quirks in handling focus. Always test your content on different browsers to ensure a smooth sail for all users.
Checking focus and enhancing accessibility
document.activeElement
returns the currently focused element. Basically, it tells you which element is the life of the party.- For accessibility, ensuring a logical focus order and that focus state is visible, is like arranging good lighting and directions in your house party.
Focus Theory and Practical Application
Managing focus in complex web applications requires a combination of theory and practice. Here are some key areas to consider:
Using JavaScript to control focus
- The
HTMLElement.focus()
method is like a movie director. It directs the spotlight to specific elements in your HTML movie script. - When introducing a new character (dynamically updating content), make sure they don't steal the spotlight (cause focus loss).
Pushing for Performance
- When adding a batch of new interactive elements, encouragement means making them focusable.
MutationObserver
is your backstage crew. It watches for changes and allows you to adjust focus accordingly.
Accessories and Styling of focusable elements
- You can use SASS or LESS to maintain the styling of your focus elements across your stylesheets.
- Accommodating users with motion sensitivities? Use
@media (prefers-reduced-motion)
.
Was this article helpful?