Focusable Elements — Native and Custom Focusable Elements
In this tutorial, you will learn about Focusable Elements. We cover key concepts, practical examples, and best practices to help you master this topic.
Native HTML elements like links, buttons, and form controls are focusable by default, while custom elements need tabindex and keyboard handlers for focusability.
In this tutorial, you'll learn which elements are focusable and how to make custom elements keyboard accessible in Keyboard Navigation.
What You'll Learn
By the end of this lesson, you'll know the native focusable elements, how to make non-focusable elements keyboard accessible, and when to use each approach.
Why It Matters
Only focusable elements can receive keyboard input. Incorrectly assuming an element is focusable leads to keyboard traps.
Real-World Use
Doda Browser's custom toolbar buttons use tabindex="0" and role="button" to ensure they are keyboard accessible.
Focusable Elements Tree
flowchart TD A[Focusable Elements] --> B[Native] A --> C[Custom] B --> D[] B --> E[
Native Focusable Elements
<!-- Natively focusable: no tabindex needed -->
<a href="/page">Link</a>
<button type="button">Click</button>
<input type="text" />
<select><option>Option</option></select>
<textarea></textarea>
<details><summary>Toggle</summary>Content</details>
These elements are automatically in the tab order and respond to keyboard events.
Making Custom Elements Focusable
To make a non-interactive element focusable:
<div
role="button"
tabindex="0"
onclick="scan()"
onkeydown="if(event.key==='Enter'||event.key===' ')scan()"
>
Start Scan
</div>
Required: tabindex="0" (for Tab reachability), role (for screen reader), and keyboard event handlers.
Elements That Should NOT Be Focusable
<!-- Should not be focusable -->
<span>Text content</span>
<p>Paragraph</p>
<div>Container</div>
<h1>Heading</h1>
Adding tabindex to these removes the focus outline from real interactive elements and confuses keyboard users.
Common Mistakes
- Making all elements focusable: Too many tab stops makes navigation tedious.
- Not adding keyboard handlers to custom focusable elements: Users can focus the element but cannot activate it.
- Focusing non-interactive elements: Users expect focusable elements to be interactive.
- Forgetting that disabled elements are not focusable: A disabled button cannot receive focus.
- Using tabindex on everything that has a click handler: Native elements already handle this.
Practice and Challenge
1. List five natively focusable HTML elements. a, button, input, select, textarea.
2. What two attributes are needed to make a div keyboard accessible? tabindex="0" and role="button" (or appropriate role).
3. Should you make a heading focusable? No. Headings are for navigation, not interaction.
4. What happens when you tab to a disabled button? It is skipped in the tab order.
5. Challenge: Audit your website for custom interactive elements. Verify each has tabindex, role, and keyboard handlers.