HTML Attributes
HTML accesskey Attribute
The accesskey attribute assigns a keyboard shortcut that a user can press to activate or focus an element. It is a global attribute usable on any element, though it fits interactive ones best.
The accesskey Attribute
accesskey is a global attribute, meaning it can be applied to any HTML element, though it is most useful on interactive elements such as links, buttons and form fields. It defines one or more single characters that, combined with a browser-specific modifier, act as a keyboard shortcut to focus or click the element.
The modifier key needed to trigger an accesskey differs by browser and operating system. Common combinations are Alt on Windows, Alt+Shift in Firefox, and Control+Option in Safari on macOS.
Syntax
Syntax: <element accesskey="s">. The value is a space-separated list of single characters; the browser uses the first one it supports.
<button accesskey="s">Save</button>Example
Run the example, then try the browser shortcut (for example Alt+S on Windows) to trigger the Save button.
<!DOCTYPE html>
<html>
<body>
<p>Try your browser's accesskey combination with these:</p>
<button accesskey="s" onclick="alert('Saved!')">Save (accesskey s)</button>
<a href="https://myinternships.in" accesskey="h">Help (accesskey h)</a>
</body>
</html>Attribute Values
| Value | Description |
|---|---|
| s | A single character used as the shortcut key |
| s h | Space-separated list; the browser picks the first supported character |
Use accesskey sparingly. Chosen keys often clash with browser, operating-system or assistive-technology shortcuts, which can harm accessibility rather than help it. Always tell users which keys exist.
Key Takeaways
- accesskey is a global attribute that sets a keyboard shortcut.
- It needs a browser-specific modifier, which varies by platform.
- The value can list several characters; the browser picks a supported one.
- It works best on links, buttons and form controls.
- Overusing it can conflict with existing shortcuts and hurt accessibility.
