Aral Code

Text Lesson 11 of 42

HTML Computer Code Elements — code, kbd, samp, var and pre

Show code, keyboard keys, program output and math variables the right way, using the code, kbd, samp, var and pre elements.

When you write about computers — a how-to guide, a school report about programming, or instructions for your classmates — you need to show things like code, keys to press, and messages from a program. HTML has a small family of elements made just for this. They make your page easier to read, and they tell browsers and screen readers what kind of text it is.

Here are the five you'll use:

  • <code> — a piece of computer code
  • <kbd> — a key or keys the user should press
  • <samp> — output from a program (what the computer says back)
  • <var> — a variable, in math or in programming
  • <pre> — preformatted text, where spaces and line breaks are kept

The code element

Wrap any short piece of code inside <code>. Browsers show it in a monospace font, where every letter has the same width, like on an old typewriter. This makes code stand out from normal sentences.

Example Inline code in a sentence
<p>To make a paragraph, type <code>&lt;p&gt;</code> before the text and <code>&lt;/p&gt;</code> after it.</p>
<p>In CSS, <code>color: red;</code> changes the text color to red.</p>

Notice the &lt; and &gt; in the example. If you type a real <p> inside your page, the browser thinks you're starting a new paragraph. To show the less-than and greater-than signs, use the entities &lt; and &gt;. You learned these in the HTML Entities lesson, coming up next.

Showing longer code with pre and code

A single <code> element ignores extra spaces and line breaks, just like a paragraph does. For a block of code with many lines, put <code> inside <pre>. The <pre> element keeps every space and every new line exactly as you typed it.

Example A code block with pre + code
<p>Here is a small HTML page:</p>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;body&gt;
    &lt;h1&gt;Hello, Pilipinas!&lt;/h1&gt;
  &lt;/body&gt;
&lt;/html&gt;</code></pre>

The indentation (the spaces at the start of each line) is kept, so the reader can see which tag is inside which. Try removing the <pre> and </pre> tags in the editor and watch everything collapse onto one line.

The kbd element: keys to press

Use <kbd> for keyboard input — the keys a person should press. It's perfect for computer lab instructions.

Example Keyboard shortcuts for the computer lab
<style>
  kbd { border: 1px solid #aaa; border-bottom-width: 3px; border-radius: 4px; padding: 1px 6px; background: #f6f6f6; }
</style>
<h3>Mga shortcut na dapat alam</h3>
<ul>
  <li>Save your work: <kbd>Ctrl</kbd> + <kbd>S</kbd></li>
  <li>Copy: <kbd>Ctrl</kbd> + <kbd>C</kbd>, then paste: <kbd>Ctrl</kbd> + <kbd>V</kbd></li>
  <li>Undo a mistake: <kbd>Ctrl</kbd> + <kbd>Z</kbd></li>
  <li>Refresh the browser: <kbd>F5</kbd></li>
  <li>On a Mac, use <kbd>⌘ Cmd</kbd> instead of <kbd>Ctrl</kbd>.</li>
</ul>

By default <kbd> just uses a monospace font. The small CSS rule at the top of the example makes each key look like a real keyboard button. You'll learn CSS like this in the CSS course.

The samp element: what the computer says

<samp> stands for "sample output". Use it for messages that a program or computer shows, like error messages or results.

Example Showing an error message
<p>If you type the wrong password, the school portal shows:</p>
<p><samp>Error 401: Invalid username or password.</samp></p>
<p>When the printer runs out of paper, it says <samp>Load paper in Tray 1</samp>.</p>

The var element: variables

<var> marks a variable — a letter or name that stands for a value. It's used in math and in programming. Browsers usually show it in italics.

Example Variables in a math formula
<p>The area of a rectangle is <var>A</var> = <var>l</var> × <var>w</var>.</p>
<p>If a jeepney fare is <var>f</var> pesos and you ride <var>n</var> times, you spend <var>f</var> × <var>n</var> pesos.</p>
<p>In the code, the variable <var>total</var> keeps the sum of all items.</p>

Putting them together

Real how-to pages mix all five. Here's a short guide for checking your computer's IP address, the kind of thing you might do in a Computer Systems Servicing class:

Example A mini how-to guide
<h3>How to find your IP address (Windows)</h3>
<ol>
  <li>Press <kbd>Windows</kbd> + <kbd>R</kbd>, type <kbd>cmd</kbd>, then press <kbd>Enter</kbd>.</li>
  <li>In the black window, type <code>ipconfig</code> and press <kbd>Enter</kbd>.</li>
  <li>Look for a line like this:
<pre><samp>IPv4 Address. . . . . . . . . . . : 192.168.1.15</samp></pre>
  </li>
  <li>Your address follows the pattern <var>a</var>.<var>b</var>.<var>c</var>.<var>d</var>, where each part is a number from 0 to 255.</li>
</ol>

Why not just use a different font?

You could make text look like code with CSS alone. But these elements add meaning. Search engines, screen readers and translation tools know that text inside <code> should not be translated or "corrected", and that <kbd> is something to type. Google Translate, for example, usually leaves <code> content alone, so your code examples won't be turned into Filipino words by accident.

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which element should you use for "Press Ctrl + S to save"?
2. Which element keeps spaces and line breaks exactly as typed?
3. A program prints "File saved successfully." Which element fits best?
4. How do you show the text <h1> on a page without making a heading?

By Sir Reyel · Last updated 2026-12-04