HTML Computer Code Elements


Lesson 40 of 58

When you write tutorials or documentation, you often need to show keyboard shortcuts, computer code, program output, or mathematical variables. HTML has special elements for exactly these situations.


HTML Computer Code Elements

Here are the elements we will cover in this chapter:

  • <kbd> – Keyboard input (like Ctrl+S)
  • <samp> – Sample output from a computer program
  • <code> – A piece of computer code
  • <var> – A variable in math or programming
  • <pre> – Preformatted text (keeps spaces and line breaks)

The kbd Element (Keyboard Input)

The <kbd> tag is used to show keyboard keys or keyboard shortcuts. It tells the reader “press these keys on your keyboard”.

kbd Element Example

By default, <kbd> text appears in a monospace font (like typewriter style). You can style it with CSS to look like real keyboard keys.

Styled Keyboard Keys

The samp Element (Sample Output)

The <samp> tag is used to show sample output from a computer program. This is great for showing error messages, command line output, or program results.

samp Element Example

Like <kbd>, <samp> also uses a monospace font by default.


The code Element (Computer Code)

The <code> tag is used to show actual computer code. This can be HTML, CSS, JavaScript, Python, or any programming language.

One important thing to know: The <code> tag does NOT keep spaces and line breaks. All code will appear on one line unless you use <pre>.

code Element Example (No line breaks)

Notice how all the code is on one line? That is because <code> ignores line breaks. To fix this, wrap your <code> inside a <pre> tag.


Using pre + code Together (Best for Code Blocks)

The <pre> tag preserves spaces and line breaks. When you combine <pre> and <code>, you get nicely formatted code blocks.

pre + code Example

Notice that HTML tags inside <code> need to be escaped. You write &lt; for < and &gt; for >. Otherwise the browser will try to render them as real HTML tags.


The var Element (Variables)

The <var> tag is used for variables in math or programming. By default, it appears in italic font.

var Element Example

Complete Example (Putting It All Together)

Here is a complete example showing all the computer code elements in one place:

All Computer Code Elements

When to Use Each Element

Element When to Use Example
<kbd> Keyboard shortcuts, keys to press Ctrl + S
<samp> Program output, error messages File not found
<code> Code snippets (use with pre for multi-line) console.log()
<var> Math variables, programming variables x = 5
<pre> Keeping spaces and line breaks exactly as typed
  indented text

Quick Summary

  • <kbd> – Shows keyboard keys and shortcuts
  • <samp> – Shows sample output from programs
  • <code> – Shows computer code
  • <var> – Shows variables in math or programming
  • <pre> – Preserves spaces and line breaks (great with code)
  • Use <pre><code>...</code></pre> for multi-line code blocks
  • Remember to escape HTML characters inside code blocks (&lt; for <, &gt; for >)

What Just Happened? Let Me Explain

  • These elements are mostly for semantic meaning. They help screen readers and search engines understand what the text represents.
  • By default, these elements look similar (monospace font), but you can style them differently with CSS.
  • The <pre> tag is very useful for code because it keeps your indentation and line breaks.
  • If you are writing a blog about programming, you will use these elements all the time.
  • For large code blocks, consider using a syntax highlighting library to make your code look colorful and professional.
Tip: Open VS Code and create a file called code-elements.html. Write a short tutorial about a programming concept you know. Use <kbd> for keyboard shortcuts. Use <code> inside <pre> for code examples. Use <samp> to show what the output should be. This is exactly how documentation websites are written.

HTML Computer Code Reference

Here are all the tags we covered in this chapter:

Tag Description
<code> Defines a piece of computer code
<kbd> Defines keyboard input
<samp> Defines sample output from a computer program
<var> Defines a variable in math or programming
<pre> Defines preformatted text (preserves spaces and line breaks)

Lesson 40 of 58