Home HTML Tags <kbd> HTML <kbd> Tag Definition
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.
Syntax
HTML Syntax Both start and end tags are required. Content is phrasing content. Nesting other <kbd> (and sometimes <samp>) elements is allowed and useful for shortcuts.
Reference
Attributes The <kbd> element has no element-specific attributes — only global attributes.
Attribute Description Notes None specific No dedicated attributes on <kbd> itself. Use class (global) when you style keys like buttons.
Try it
Live Preview Pick a key or shortcut and see it marked with real <kbd> elements in a sentence.
Code
HTML Examples Short patterns for keys, shortcuts, and echoed input. Use Try It Yourself for a live editor.
1. Single key 2. Shortcut 3. With samp Example 1 — Single Key or Command Mark one key, or a short command the user should type.
<p>
Press <kbd>Enter</kbd> to continue.
</p>
<p>
Run <kbd>npm start</kbd> in your terminal.
</p> Press Enter to continue.
Run npm start in your terminal.
How It Works Wrap the input the reader should press or type in <kbd>…</kbd>.Keep surrounding text outside so only the key/command is marked.Browsers usually render kbd in monospace; CSS can style it like a keycap.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.
<p>
Create a new document with
<kbd><kbd>Ctrl</kbd>+<kbd>N</kbd></kbd>.
</p> Create a new document with Ctrl +N .
How It Works One kbd per key (Ctrl, N) keeps each unit clear.Join with + (or “then”) as plain text between keys.Optional outer kbd groups the whole shortcut for CSS like kbd > kbd.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.
<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> If a syntax error occurs, the tool echoes your command:
custom-git ad my-new-file.cpp
How It Works <samp> marks sample program output.<kbd> inside marks that the echoed text was user input.Opposite nesting (samp inside kbd) can mark on-screen labels the user chooses (menus, buttons).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.
Compatibility
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.
Remember
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.
UA style
Default CSS Settings Most browsers render kbd like other monospace phrase elements. A typical idea:
kbd {
font-family: monospace;
} Keycap borders and shadows are author CSS — not required by the HTML spec.
Frequently Asked Questions What is the HTML kbd tag used for? 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.
Does kbd have special attributes? No. It only supports global attributes such as class, id, lang, and title.
How do I mark a Ctrl+N shortcut? 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.
What is the difference between kbd and code? code marks a fragment of computer code. kbd marks input the user provides (keys or commands). Prefer the element that matches meaning.
When should I nest kbd inside samp? 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).
Does kbd change keyboard behavior? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion