HTML <kbd> tag
The <kbd>
tag in HTML is used to represent user input from a keyboard, such as keyboard shortcuts or text that a user is expected to type. It provides semantic meaning that the enclosed text is meant to be input by the user, often used in technical documentation or tutorials to illustrate how users should interact with a system.
Syntax:
<kbd>Ctrl + C</kbd>
Key Characteristics:
Semantics:
- The
<kbd>
tag is used to convey that the text inside represents keyboard input. It provides a visual and semantic cue that the enclosed text is related to user interaction via a keyboard.
- The
Presentation:
- By default, most browsers render the
<kbd>
tag with a monospace font and a slight visual styling, often including a border or background to distinguish it from regular text. This helps to make keyboard input stand out.
- By default, most browsers render the
Example Usage:
Basic Example:
<p>To copy selected text, press <kbd>Ctrl + C</kbd>.</p>
In this example, "Ctrl + C" is enclosed in the <kbd>
tag to indicate that it is a keyboard shortcut for copying text.
Example with Multiple Inputs:
<p>To save your document, use <kbd>Ctrl + S</kbd>. To open a new document, press <kbd>Ctrl + N</kbd>.</p>
In this example, multiple keyboard shortcuts are provided, each enclosed in a <kbd>
tag to highlight the user input.
Accessibility and Best Practices:
Accessibility: The
<kbd>
tag helps screen readers and other assistive technologies understand that the text represents keyboard input. It provides semantic meaning to users who might not be able to see the visual styling.Consistency: Use the
<kbd>
tag consistently to represent keyboard input across your content. This helps users quickly recognize and understand the expected keyboard actions.Styling: You can use CSS to customize the appearance of the
<kbd>
tag if the default styling does not meet your needs. For example, you can change the font size, background color, or border style:kbd { font-family: monospace; background-color: #f0f0f0; border: 1px solid #ccc; padding: 2px 4px; border-radius: 4px; }