HTML Keyboard Shortcuts
In this page:
Standard Tab Key Navigation
Webpages can be navigated chronologically using the Tab key. Modern browsers automatically include interactive elements (like buttons, links, and inputs) in the tab sequence natively.
Example: Standard Tab Key Navigation
<a href="/home">Home</a>
<button>Click me</button>
<input type="text">
Custom Hotkeys with accesskey
The global accesskey attribute allows you to define custom keyboard shortcuts for specific elements, letting users trigger actions or place focus inside input fields using simple key combinations.
- Assign easy, memorable letters as accesskey shortcuts for your primary navigation links.
- Accepted values:
- accesskey — one character or space-separated list of characters
- Activated with browser-specific modifier keys (e.g. Alt+key)
Example: Custom Hotkeys with accesskey
<a href="/home" accesskey="h">Home</a>
Focus Management with tabindex
The global tabindex attribute allows you to manually control the chronological keyboard tab sequence. Set tabindex="0" to include custom elements in the sequence, or tabindex="-1" to remove them.
- Only use tabindex="0" or tabindex="-1"; avoid using positive numbers as they can disrupt the logical tab navigation flow.
- Accepted values:
- 0 — focusable in DOM order
- 1 — focusable by script only, skipped by Tab
- positive integer — explicit tab order (avoid)
Example: Focus Management with tabindex
<div tabindex="0">Focusable custom element</div>
<div tabindex="-1">Not in tab order</div>
Custom Event Hotkeys in JavaScript
You can write custom JavaScript scripts to listen for keypress events (like Keydown or Keyup) and trigger specific actions on your page, allowing you to create rich, application-specific hotkey shortcuts.
Example: Custom Event Hotkeys in JavaScript
<input type="text" onkeydown="if (event.key === 'Enter') console.log('submitted')">
Designing Clear Focus Indicators
Keyboard users need a visible focus indicator to see which element is currently selected as they navigate. You can use CSS pseudo-classes to customize the style of this indicator and make it look clean.
Example: Designing Clear Focus Indicators
<style>
a:focus {
outline: 2px solid orange;
}
</style>
<a href="/home">Home</a>
- Hiding browser focus outlines using CSS (outline: none) without replacing them with custom focus styles.
- Using positive tabindex values (like tabindex="1"), which disrupts the logical tab navigation flow.
- Designing keyboard shortcuts that conflict with standard browser or screen-reader commands.
- Use native interactive tags like button and anchor links to inherit built-in keyboard focus support automatically.
- Define custom hotkey shortcuts using the accesskey attribute, and control tab sequences using tabindex.
- Never remove browser focus outlines without replacing them with high-contrast, custom focus styles.
Standard HTML keyboard attributes and CSS focus selectors are supported natively by all web browsers.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- HTML Keyboard Shortcuts
- HTML Browser Support
- HTML Character Sets
- HTML Doctypes
- HTML Audio/Video Reference
- HTML Meta Tags
- HTML ARIA Roles
- HTML Input Validation
- HTML Iframe Security
- HTML Responsive Images
- HTML Web Fonts
- HTML Icon Fonts
- HTML Progress & Meter
- HTML Output Element
- HTML Datalist
- HTML Image Maps
- HTML Browser DevTools
- HTML Validation
- HTML Quiz
- HTML Interview Prep