Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. HTML Tag Dictionary
  3. <code> / <kbd> / <var>

<code> / <kbd> / <var>

code marks up a fragment of program code, kbd marks up keyboard input, and var marks up variable names or placeholders — each as an inline semantic element.

Syntax

<!-- Code fragment -->
<code>document.getElementById("id")</code>

<!-- Keyboard input / key name -->
<kbd>Ctrl</kbd> + <kbd>C</kbd>

<!-- Variable / placeholder -->
<var>x</var> = <var>y</var> + 1

<!-- Sample output (samp) -->
<samp>Error: File not found</samp>

Tag List

TagDescription
codeRepresents a fragment of program code, a filename, or a command. Displayed in a monospace font by default.
kbdRepresents text to be typed on a keyboard, or a key name. Used for instructions such as "press the Enter key". Displayed in a monospace font by default.
varRepresents a variable name or placeholder in a mathematical expression or program. Displayed in italic by default.
sampRepresents sample output from a program or command. Displayed in a monospace font by default.

Sample Code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <style>
    /* Style kbd to look like a keyboard key */
    kbd {
      display: inline-block;
      padding: 2px 6px;
      border: 1px solid #ccc;
      border-radius: 3px;
      background: #f6f6f6;
      font-size: 0.9em;
    }
  </style>
</head>
<body>

  <!-- Use code to mark up a code fragment -->
  <p>To get an element in JavaScript, use <code>document.getElementById()</code>.</p>

  <!-- Use code to mark up a filename -->
  <p>Edit the configuration file <code>config.json</code>.</p>

  <!-- Use kbd to describe key operations -->
  <p>To copy, press <kbd>Ctrl</kbd> + <kbd>C</kbd>.</p>
  <p>To save, press <kbd>Ctrl</kbd> + <kbd>S</kbd>.</p>

  <!-- Use var to mark up a variable name -->
  <p>Formula for area: <var>area</var> = <var>width</var> × <var>height</var></p>

  <!-- Use samp to show command output -->
  <p>Running the command produces the following output:</p>
  <samp>Hello, World!</samp>

</body>
</html>

Result

code renders in a monospace font to look like code. kbd also uses a monospace font (and looks like a keyboard key when styled with CSS). var renders in italic.

To get an element in JavaScript, use document.getElementById().
Edit the configuration file config.json.
To copy, press [Ctrl] + [C].
Formula for area: area = width × height  ← italic
Hello, World!

Notes

All of these elements render in a monospace font (or italic) by default, but that is simply the browser's default styling. What matters is conveying the correct meaning. For example, when mentioning a method name in a description, use code not because you want a monospace font, but because you want to communicate "this is code".

To display a multi-line code block, combine pre with code. pre preserves whitespace and line breaks, while code provides the semantic meaning of "this is code" (e.g., <pre><code>...</code></pre>).

kbd is not limited to keyboard input — it can also be used for voice commands, game controller buttons, or any other input from the user. Adding a border and background color via CSS gives it a keyboard-key appearance, which makes instructions easier to follow.

Browser Support

Chrome Chrome
49+
Supported in all versions
Firefox Firefox
57+
Supported in all versions
Safari Safari
18 or earlier ×
Edge Edge
80+
11 or earlier ×
IE IE
11 or earlier ×
Opera Opera
48+
14 or earlier ×
iOS Safari iOS Safari
18 or earlier ×
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .