What is the accesskey Attribute in HTML: Definition and Usage
accesskey attribute in HTML assigns a keyboard shortcut to an element, allowing users to quickly focus or activate it using a key combination. It improves accessibility by enabling faster navigation without a mouse.How It Works
The accesskey attribute lets you set a single key as a shortcut to jump to or activate an element on a webpage. Think of it like a quick button on your keyboard that takes you directly to a link, button, or form field without clicking.
When you press the assigned key along with a modifier key (like Alt, Ctrl, or Shift, depending on the browser and operating system), the browser moves focus to that element or triggers its action. This is similar to how you might use keyboard shortcuts in software to speed up tasks.
It’s like having a special shortcut on your keyboard for your favorite apps, but for parts of a webpage instead.
Example
This example shows a link with an accesskey of "h". Pressing Alt + Shift + H (Windows) or Ctrl + Option + H (Mac) will focus or activate the link.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Accesskey Example</title> </head> <body> <a href="https://example.com" accesskey="h">Go to Example.com (Alt + Shift + H)</a> </body> </html>
When to Use
Use the accesskey attribute to improve keyboard navigation for users who rely on keyboards instead of a mouse. It is helpful for accessibility, especially for people with mobility challenges.
Common use cases include:
- Quickly jumping to main sections like navigation menus or search boxes.
- Providing shortcuts for frequently used buttons or links.
- Enhancing usability in web apps where keyboard efficiency matters.
However, use it sparingly and choose keys carefully to avoid conflicts with browser or screen reader shortcuts.
Key Points
- Assigns keyboard shortcuts to HTML elements for faster access.
- Improves accessibility for keyboard users.
- Requires modifier keys like Alt or Ctrl depending on the browser.
- Use carefully to avoid conflicts with existing shortcuts.
- Not all browsers handle it the same way, so test your shortcuts.