<kbd>: The Keyboard Input element - HTML: HyperText Markup Language 编辑

The HTML Keyboard Input element (<kbd>) represents a span of inline text denoting textual user input from a keyboard, voice input, or any other text entry device. By convention, the user agent defaults to rendering the contents of a <kbd> element using its default monospace font, although this is not mandated by the HTML standard.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

<kbd> may be nested in various combinations with the <samp> (Sample Output) element to represent various forms of input or output based on visual cues.

Content categoriesFlow content, phrasing content, palpable content.
Permitted contentPhrasing content.
Tag omissionNone, both the starting and ending tag are mandatory.
Permitted parentsAny element that accepts phrasing content.
Implicit ARIA roleNo corresponding role
Permitted ARIA rolesAny
DOM interfaceHTMLElement

Attributes

This element only includes the global attributes.

Usage notes

Other elements can be used in tandem with <kbd> to represent more specific scenarios:

  • Nesting a <kbd> element within another <kbd> element represents an actual key or other unit of input as a portion of a larger input. See Representing keystrokes within an input below.
  • Nesting a <kbd> element inside a <samp> element represents input that has been echoed back to the user by the system. See Echoed input, below, for an example.
  • Nesting a <samp> element inside a <kbd> element, on the other hand, represents input which is based on text presented by the system, such as the names of menus and menu items, or the names of buttons displayed on the screen. See the example under Representing onscreen input options below.

You can define a custom style to override the browser's default font selection for the <kbd> element, although the user's preferences may potentially override your CSS.

Examples

Basic example

<p>Use the command <kbd>help mycommand</kbd> to view documentation
for the command "mycommand".</p>

Representing keystrokes within an input

To describe an input comprised of multiple keystrokes, you can nest multiple <kbd> elements, with an outer <kbd> element representing the overall input and each individual keystroke or component of the input enclosed within its own <kbd>.

Unstyled

First, let's look at what this looks like as just plain HTML.

HTML
<p>You can also create a new document using the keyboard shortcut
<kbd><kbd>Ctrl</kbd>+<kbd>N</kbd></kbd>.</p>

This wraps the entire key sequence in an outer <kbd> element, then each individual key within its own, in order to denote the components of the sequence.

Note: You don't need to do all this wrapping; you can choose to simplify it by leaving out the external <kbd> element. In other words, simplifying this to just <kbd>Ctrl</kbd>+<kbd>N</kbd> would be perfectly valid.

Depending on your style sheet, though, you may find it useful to do this kind of nesting.

Result

The output looks like this without a style sheet applied:

With custom styles

We can make more sense of this by adding some CSS:

CSS

We add a new style for <kbd> elements, key, which we can apply when rendering keyboard keys:

kbd.key {
  border-radius: 3px;
  padding: 1px 2px 0;
  border: 1px solid black;
}
HTML

Then we update the HTML to use this class on the keys in the output to be presented:

<p>You can also create a new document by pressing <kbd><kbd class="key">Ctrl</kbd>+<kbd class="key">N</kbd></kbd>.</p>
Result

The result is just what we want!

Echoed input

Nesting a <kbd> element inside a <samp> element represents input that has been echoed back to the user by the system.

<p>If a syntax error occurs, the tool will output the initial
command you typed for your review:</p>
<blockquote>
  <samp><kbd>custom-git ad my-new-file.cpp</kbd></samp>
</blockquote>

Representing onscreen input options

Nesting a <samp> element inside a <kbd> element represents input which is based on text presented by the system, such as the names of menus and menu items, or the names of buttons displayed on the screen.

For example, you can explain how to choose the "New Document" option in the "File" menu using HTML that looks like this:

<p>To create a new file, choose the menu option
<kbd><kbd><samp>File</samp></kbd>⇒<kbd><samp>New
Document</samp></kbd></kbd>.</p>

<p>Don't forget to click the <kbd><samp>OK</samp></kbd> button
to confirm once you've entered the name of the new file.</p>

This does some interesting nesting. For the menu option description, the entire input is enclosed in a <kbd> element. Then, inside that, both the menu and menu item names are  contained within both <kbd> and <samp>, indicating an input which is selected from a screen widget.

Result

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of '<kbd>' in that specification.
Living Standard
HTML5
The definition of '<kbd>' in that specification.
RecommendationExpanded to include any user input, like voice input and individual keystrokes.
HTML 4.01 Specification
The definition of '<kbd>' in that specification.
Recommendation

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:77 次

字数:12072

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文