HTML Computer Code
HTML has a small group of elements for showing computer-related text: <code> for pieces of code, <pre> for preformatted blocks, <kbd> for keyboard input, <samp> for program output and <var> for variables. They tell browsers, screen readers and search engines what kind of text this is, and browsers display most of them in a monospace font by default. You use them in documentation, tutorials, blog posts and help pages.
The five elements at a glance
| Element | Meaning | Example use |
|---|---|---|
<code> | A fragment of computer code | The <code>map()</code> method |
<pre> | Preformatted text; keeps spaces and line breaks | Multi-line code samples, ASCII art |
<kbd> | Text the user types or keys they press | Press Ctrl + S |
<samp> | Sample output from a program | An error message shown in a terminal |
<var> | A variable in maths or code | The area is width times height |
Inline code versus code blocks
<code> on its own is an inline element. Use it inside a sentence for short things like a function name, a file name or a property. It does not preserve line breaks, so multi-line code collapses into one line.
For longer snippets, wrap <code> inside <pre>. The <pre> element keeps every space, tab and newline exactly as typed, which matters for indentation. The <code> inside it keeps the meaning that the content is code. This combination is what most documentation sites and syntax highlighters use.
<p>Run <kbd>npm install</kbd> and wait for the message
<samp>added 42 packages</samp>.</p>
<p>Press <kbd><kbd>Ctrl</kbd> + <kbd>C</kbd></kbd> to copy.</p>
<p>The area is <var>w</var> × <var>h</var>.</p>
<pre><code>function greet(name) {
return `Hello, ${name}!`;
}</code></pre>
<p>Wrap text in a paragraph with <code><p></code>.</p>
Nesting <kbd> inside <kbd>, as in the copy shortcut above, is the documented way to describe a key combination made of individual keys.
All of these elements can be restyled. A common touch is giving <kbd> a light border and rounded corners so it looks like a physical key, and giving inline <code> a soft background colour so it stands out from the surrounding sentence.
The trap: showing HTML inside HTML
The biggest surprise for beginners comes when the code being shown is itself HTML. If you write <code><p>Hello</p></code>, the browser does not display the tags. It treats <p> as a real paragraph and shows only "Hello". The <code> and <pre> elements do not switch off HTML parsing.
To display a less-than sign you must write the entity <, and for greater-than >. An ampersand itself becomes &. The last line of the example shows this: the source contains entities, and the reader sees a literal <p>. When code is inserted with JavaScript, setting textContent instead of innerHTML escapes it for you.
Another small issue is that very long lines inside <pre> do not wrap, which can push the page wider than a phone screen. Adding overflow-x: auto to pre in your CSS gives the block its own scrollbar instead.
Remember
- Use
<code>for inline snippets and<pre><code>for blocks. - Use
<kbd>for input,<samp>for output and<var>for variables. - Escape
<,>and&when the code contains them.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.