HTML kbd Tag: How to Show Keyboard Input with examples

html kbd tag

The HTML<kbd> tag shows the keys that a user must press. This helps you to give instructions for commands or shortcuts. It is common in guides where you explain steps for tools or games.

Understand the <kbd> Tag in HTML

The <kbd> tag marks text as keyboard input in HTML. It is inline and appears inside normal text. Browsers usually display it in monospace style.

Here is the basic tag:

<kbd>Key</kbd>

It wraps the name of the key inside the tag. Here is a quick example:

<p>Press <kbd>Ctrl</kbd> + <kbd>C</kbd> to copy.</p>

The keys are shown inside the kbd element to highlight them as input.

Here are the browsers that support the <kbd> tag:

  • Chrome
  • Firefox
  • Safari
  • Edge,
  • Opera

The <kbd> works in most mobile browsers.

How to Style the <kbd> Tag in HTML with CSS

You can use CSS to change how keys look on the screen. Borders and padding can make them look like real keys in CSS.

Here is an example:

<style>
kbd {
  border: 1px solid #000;
  padding: 3px 5px;
  border-radius: 3px;
  background: #eee;
}
</style>
<p>Use <kbd>Alt</kbd> + <kbd>Tab</kbd> to switch windows.</p>

This adds custom styles to the <kbd> element. Here is an image that shows you that:

kdb tag in html

Nest the HTML<kbd> Tag Inside Other Tags

You can place the <kbd> inside many elements:

  • Paragraphs
  • Headings
  • Tables
  • Lists

Here is an example:

<p>Click <strong>Options</strong> and press <kbd>F10 to open the menu</kbd>.</p>

The Differences Between kbd Tag and Samp Tags

The<kbd> element shows what the user must type, while the<samp> element displays text that a program outputs.

Here is a table that shows you the key differences between them:

Featurekbdsamp
PurposeUser input keysProgram output
Default fontMonospaceMonospace
Typical useShortcuts and commandsMessages and responses

The use case:

  • kbd shows shortcuts or typed commands.
  • samp shows results from a program.

Examples

Copy Shortcut:

<p>Press <kbd>Ctrl</kbd> + <kbd>C</kbd> to copy selected text.</p>

This example shows a key combination that copies text in most systems.

List of Shortcuts:

<ul>
  <li>New Tab: <kbd>Ctrl</kbd> + <kbd>T</kbd></li>
  <li>Close Tab: <kbd>Ctrl</kbd> + <kbd>W</kbd></li>
</ul>

The multiple shortcuts are listed, each with its own kbd tags.

Wrapping Up

You learned how the kbd tag works and how to style it in this article. You also saw how to use it inside other tags and how it differs from samp.

Here is a quick recap:

  • The <kbd> tag marks text as user key input; you can style it with CSS.
  • It works in all browsers.

FAQs

What is the purpose of the <kbd> tag in HTML?

The <kbd> tag defines keyboard input in a document. It is used to display text that represents keys the user should press. Example:
<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save.</p>

How to style the <kbd> tag in HTML?

You can use CSS to style the <kbd> tag. Example:
<style>
kbd {
  background-color: #eee;
  border: 1px solid #ccc;
  padding: 2px 4px;
  border-radius: 3px;
}
</style>
<p>Press <kbd>Enter</kbd> to continue.</p>

What is the difference between <kbd> and <code> tags?

The <kbd> tag shows user keyboard input. The <code> tag shows code snippets. Example:
<p>Use <kbd>Ctrl</kbd> + <kbd>C</kbd> to copy.</p>
<p>Example of code: <code>print("Hello")</code></p>

Can you nest <kbd> tags inside other elements?

Yes, you can nest <kbd> inside elements like <p>, <span>, or <div>. Example:
<p>To refresh the page press <kbd>F5</kbd>.</p>

Similar Reads

The hidden Attribute in HTML: How it Works with Examples

The HTML hidden attribute hides elements from users but keeps them in the code. You can use it when you…

How to Create HTML Nested Lists with Examples

In this article, you will learn how the browsers read nested lists in HTML and how to use them with…

HTML figure Tag: Responsive Design & Media Elements

The HTML figure tag links media with a caption. Use it to group an image, diagram, code block, or video…

The canvas Tag in HTML: How to Draw Graphics with Examples

The HTML canvas tag draws shapes and images. It also helps you draw text with code. It gives control over…

HTML Document Structure: How to Create DOCTYPE in HTML

The DOCTYPE ensures that your HTML is readable by browsers and ensures that pages follow modern standards. Understand the DOCTYPE…

HTML track Tag: How to Add Subtitles to Videos

The track tag in HTML adds text tracks like subtitles and captions to media. It improves video access and helps…

The em Tag in HTML: How to Emphasize Text in HTML

The HTML em tag shows stress or emphasis in text. It changes meaning for readers and screen readers. What is…

HTML5 New Elements with Examples

HTML5 gave the web new elements that describe content with more meaning. Developers now use these elements to build pages…

HTML svg Tag: Scalable Vector Graphics for the Web

The <svg> tag in HTML helps you draw graphics that scale cleanly at any size. You can animate shapes and…

How to Build Responsive HTML Email Templates

Email templates are still the key tools for work and brands in HTML. A simple email can reach many people,…

Previous Article

Understanding JavaScript Objects with Examples

Next Article

PHP array_combine: How to Merge Arrays as Key and Value

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.