- 1Accessibility in HTML
- 2Alt Text for Images
- 3ARIA Roles in HTML
- 4Semantic HTML for Accessibility
- 5Keyboard Navigation in HTML
- 6Screen Reader Accessibility in HTML
- 7HTML Best Practices for SEO
- 8HTML Meta Tags
- 9HTML Headings Best Practices
- 10Title and Meta Description Tags in HTML
- 11HTML Linking Structure
- 12HTML Clean Code Standards
Keyboard Navigation in HTML
tabindex, Focus, Accessibility Best Practices
Keyboard Navigation in HTML: Let Everyone Explore Your Interface
Not everyone uses a mouse. Some users rely entirely on keyboards — either by choice or necessity. Building a website that’s easy to navigate via keyboard means more accessible, inclusive experiences for all.
1. What Is Keyboard Navigation?
Keyboard navigation allows users to move through interactive elements (like links, buttons, and forms) using keys like Tab, Enter, Arrow keys, and Escape.
Core Keys:
- Tab — Move focus to the next interactive element
- Shift + Tab — Move focus to the previous element
- Enter — Activate a button or link
- Arrow keys — Navigate within components like menus or sliders
2. Natural Tab Order
By default, browsers create a logical tab order based on HTML structure. Elements like <a>
, <button>
, <input>
, and <textarea>
are focusable.
<a href="#apple">Apple</a>
<button>Banana</button>
<input type="text" placeholder="Cherry">
[ Tab cycles through: Apple → Banana → Cherry input ]
3. tabindex: Controlling Focus Order
tabindex
is an attribute that controls the element’s focus behavior.
tabindex="0"
| Adds element to tab order (natural position) |
| tabindex="-1"
| Element can be focused programmatically, but not via Tab |
| tabindex="1+
| Forces custom tab order (generally discouraged) |
Example: Making a Div Focusable
<div tabindex="0">Focus me with Tab key</div>
[ User presses Tab → this div becomes focusable ]
4. Focus Management with JavaScript
You can move focus manually using JavaScript — helpful in modals, alerts, or custom widgets.
<button onclick="document.getElementById('msg').focus()">
Focus on Message
</button>
<div id="msg" tabindex="-1">Hello World, focus is here!</div>
5. Avoiding Keyboard Traps
A keyboard trap occurs when focus enters a section and can't escape. This is frustrating and can block users entirely.
Good Example: Modal Focus
<div id="modal" role="dialog" aria-modal="true">
<button>Close Modal</button>
<button>Confirm Cherry 🍒</button>
</div>
Best Practices:
- Trap focus inside modals, but allow exiting with Escape or Close
- Restore focus to the triggering element when modal closes
- Use libraries like
focus-trap
or write custom handlers for complex components
6. Skip Links for Faster Navigation
Provide "Skip to main content" links to let keyboard users bypass repetitive content.
<a href="#main-content" class="skip-link">Skip to main content</a>
<main id="main-content">
<h1>Welcome to Apple Grove</h1>
</main>
CSS Suggestion:
.skip-link {
position: absolute;
left: -9999px;
}
.skip-link:focus {
left: 0;
background: yellow;
}
7. Summary
Accessible keyboard navigation means a smoother experience for all users — including those with mobility challenges or screen readers. You’ve learned how to:
- Respect and use natural tab order
- Control focus using
tabindex
and JavaScript - Avoid traps that hinder navigation
- Enhance UX with skip links and focus management
What’s Next?
Next up: We’ll build a fully accessible modal dialog that supports keyboard navigation, screen readers, and ARIA roles — from scratch.