HTML <kbd> Tag

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The <kbd> element marks user input — typically a keyboard key, shortcut, or typed command. Browsers usually show it in a monospace font.

Use it in docs and tutorials so readers see what to press or type. Prefer <code> for source snippets and <samp> for program output when that meaning fits better. You can nest kbd with samp for echoed input or on-screen choices.

Baseline · Widely available.

Not deprecated. Stable across browsers since about July 2015 (and much earlier in practice).

Note.

<kbd> is semantic markup only — it does not capture keys or register shortcuts by itself.

HTML Syntax

syntax.html
<kbd> Ctrl</kbd>

Both start and end tags are required. Content is phrasing content. Nesting other <kbd> (and sometimes <samp>) elements is allowed and useful for shortcuts.

Attributes

The <kbd> element has no element-specific attributes — only global attributes.

AttributeDescriptionNotes
None specificNo dedicated attributes on <kbd> itself.Use class (global) when you style keys like buttons.

Global Attributes

The <kbd> tag also supports the Global Attributes in HTML.

Live Preview

Pick a key or shortcut and see it marked with real <kbd> elements in a sentence.

Each key gets its own <kbd>. Joining with + is plain text.

Sentence

Press Ctrl + S to save.

<kbd>Ctrl</kbd> + <kbd>S</kbd>

HTML Examples

Short patterns for keys, shortcuts, and echoed input. Use Try It Yourself for a live editor.

Example 1 — Single Key or Command

Mark one key, or a short command the user should type.

HTML
<p>
  Press <kbd>Enter</kbd> to continue.
</p>
<p>
  Run <kbd>npm start</kbd> in your terminal.
</p>
Try It Yourself

How It Works

  1. Wrap the input the reader should press or type in <kbd>…</kbd>.
  2. Keep surrounding text outside so only the key/command is marked.
  3. Browsers usually render kbd in monospace; CSS can style it like a keycap.
  4. Meaning first: use kbd for user input, not for arbitrary bold monospace text.

Example 2 — Keyboard Shortcut

Give each key its own <kbd>. An outer kbd is optional for styling groups.

HTML
<p>
  Create a new document with
  <kbd><kbd>Ctrl</kbd>+<kbd>N</kbd></kbd>.
</p>
Try It Yourself

How It Works

  1. One kbd per key (Ctrl, N) keeps each unit clear.
  2. Join with + (or “then”) as plain text between keys.
  3. Optional outer kbd groups the whole shortcut for CSS like kbd > kbd.
  4. Simpler form <kbd>Ctrl</kbd> + <kbd>N</kbd> is also valid.

Example 3 — Echoed Input with samp

Nest kbd inside samp when the system shows the user’s input again as sample output.

HTML
<p>If a syntax error occurs, the tool echoes your command:</p>
<p>
  <samp><kbd>custom-git ad my-new-file.cpp</kbd></samp>
</p>
Try It Yourself

How It Works

  1. <samp> marks sample program output.
  2. <kbd> inside marks that the echoed text was user input.
  3. Opposite nesting (samp inside kbd) can mark on-screen labels the user chooses (menus, buttons).
  4. Pick nesting by meaning — do not nest for decoration alone.

Edge Cases & Pitfalls

Most mistakes mix up kbd, code, and visual styling.

meaning

Do not use kbd for source code

Mark snippets with <code>. Reserve kbd for what the user types or presses.

platform

Name keys carefully

Prefer clear labels (Ctrl, Cmd, Enter). Mention platform differences when shortcuts differ (Windows vs macOS).

a11y

Keep surrounding sentence clear

kbd has no special ARIA role. Write full instructions in the sentence; do not rely on keycap styling alone.

style

Styling is optional CSS

Keycap look (border, shadow) comes from your stylesheet. Default UA style is usually just monospace.

nesting

Do not over-nest

Nest kbd/samp only when meaning needs it. Flat <kbd>Ctrl</kbd> + <kbd>S</kbd> is fine for most docs.

Browser Support

The <kbd> element is Baseline · Widely available. Support has been stable across major browsers since about July 2015.

✓ Baseline · Widely available

<kbd>

Supported in modern Chrome, Firefox, Safari, Edge, and Opera. Rendering is typically monospace; keycap styling is up to your CSS.

100% Modern browser coverage
Google Chrome Yes
Supported
Mozilla Firefox Yes
Supported
Apple Safari Yes
Supported
Microsoft Edge Yes
Supported
Opera Yes
Supported
Internet Explorer Yes · EOL
Supported
<kbd> Widely available

Bottom line: Use <kbd> freely in documentation. Pair with CSS when you want a keycap look.

Key Takeaways

  • Role: <kbd> marks user input — keys, shortcuts, or typed commands.
  • Attrs: global only — use class when styling keycaps.
  • Vs others: code = source; samp = output; kbd = user input.
  • Shortcuts: one kbd per key; nest with samp only when meaning needs it.

One line: Use <kbd> when showing what the reader should press or type.

Default CSS Settings

Most browsers render kbd like other monospace phrase elements. A typical idea:

default.css
kbd {
  font-family: monospace;
}

Keycap borders and shadows are author CSS — not required by the HTML spec.

Frequently Asked Questions

The kbd element marks user input — usually a keyboard key, shortcut, or typed command — so docs can show what the reader should press or type.
No. It only supports global attributes such as class, id, lang, and title.
Use separate kbd elements for each key, for example <kbd>Ctrl</kbd>+<kbd>N</kbd>. You may wrap the whole sequence in an outer kbd for styling.
code marks a fragment of computer code. kbd marks input the user provides (keys or commands). Prefer the element that matches meaning.
When the system echoes the user’s input back as sample output. Nesting samp inside kbd can mark on-screen labels the user chooses (menus, buttons).
No. It is semantic markup for display and meaning. It does not capture keys or create shortcuts by itself.

Next: keygen

Learn the obsolete keygen element and modern alternatives.

keygen tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful