TExcelTAD
Home › HTML › HTML Computer Code

HTML Computer Code Elements

Programming tutorials, documentation and tech blogs constantly show code, keyboard shortcuts and program output. HTML has a small family of elements for exactly this. They all display in a monospace font by default (every character the same width), and they tell browsers and assistive technology what kind of text it is.

Element Meaning Example use
<code> A fragment of computer code the <p> element, the print() function
<kbd> Keyboard (or other user) input press Ctrl + S
<samp> Sample output from a program File not found
<var> A variable in maths or programming area = w × h
<pre> Preformatted text: keeps spaces and line breaks multi-line code listings

code: inline code

Use <code> for short pieces of code inside a sentence.

html · try it yourself
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
  <p>To print a message in Python, call <code>print("Hi")</code>.</p>
  <p>The <code>&lt;img&gt;</code> element needs an <code>alt</code> attribute.</p>
</body>
</html>

Notice the second line: to show a tag like <img> as text, you must escape the angle brackets as &lt; and &gt;. Otherwise the browser would treat it as a real image element.

pre: keeping whitespace

Normally HTML collapses runs of spaces and line breaks into a single space. That would destroy code indentation. <pre> keeps whitespace exactly as written. For a block of code, the convention is to put <code> inside <pre>:

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  pre {
    background: #1e1e2e;
    color: #cdd6f4;
    padding: 12px 16px;
    border-radius: 8px;
    overflow-x: auto;
    tab-size: 4;
  }
</style>
</head>
<body style="font-family: sans-serif">
  <p>Without &lt;pre&gt; the indentation is lost:</p>
  <code>
function greet(name) {
    return "Hello, " + name;
}
  </code>

  <p>With &lt;pre&gt;&lt;code&gt; it is preserved:</p>
<pre><code>function greet(name) {
    return "Hello, " + name;
}</code></pre>
</body>
</html>

Tip: Whitespace inside <pre> includes the line break right after the opening tag — browsers ignore one leading newline, but extra spaces before your first line will show up. Start your code right after <pre><code>.

overflow-x: auto adds a horizontal scrollbar when a long line does not fit, instead of breaking the layout.

kbd: keyboard input

<kbd> marks keys or text the user should type. A bit of CSS makes it look like physical keys.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  kbd {
    display: inline-block;
    padding: 2px 6px;
    border: 1px solid #bbb;
    border-bottom-width: 3px;
    border-radius: 4px;
    background: #f7f7f7;
    font-size: 0.9em;
  }
</style>
</head>
<body style="font-family: sans-serif; line-height: 2">
  <p>Save the file with <kbd>Ctrl</kbd> + <kbd>S</kbd> (or <kbd>⌘</kbd> + <kbd>S</kbd> on a Mac).</p>
  <p>Run a playground on ExcelTAD with <kbd>Ctrl</kbd> + <kbd>Enter</kbd>.</p>
  <p>In the terminal, type <kbd>npm run dev</kbd> and press <kbd>Enter</kbd>.</p>
</body>
</html>

samp and var

<samp> is for output a program produces; <var> names a variable in a formula or a placeholder in a command.

html · try it yourself
<!DOCTYPE html>
<html>
<body style="font-family: sans-serif">
  <p>The area of a rectangle is <var>w</var> × <var>h</var>.</p>
  <p>To copy a file: <code>cp <var>source</var> <var>destination</var></code></p>
  <p>If the file is missing, the terminal prints:</p>
  <pre><samp>cp: cannot stat 'notes.txt': No such file or directory</samp></pre>
</body>
</html>

By default <var> is shown in italics, which matches how variables are written in maths.

Putting it together

A typical documentation snippet mixes these elements:

html
<p>Type <kbd>python3 hello.py</kbd> to run the script. You should see:</p>
<pre><samp>Hello, World!</samp></pre>
<p>Edit the <code>message</code> variable to change the greeting.</p>

Note: These elements describe meaning; they do not add syntax highlighting. Coloured code on sites like this one comes from JavaScript libraries that wrap keywords in <span>s with CSS classes.

Test yourself

Which element should mark a keyboard shortcut like Ctrl + C?

  1. <code>
  2. <kbd>
  3. <samp>
  4. <key>

What does <pre> do that a normal element does not?

  1. Highlights syntax
  2. Preserves spaces and line breaks
  3. Runs the code
  4. Hides the text

How do you display the literal text <b> on a page?

  1. <code><b></code>
  2. &lt;b&gt;
  3. <pre><b></pre>
  4. \<b\>