References

Beginner-friendly references for web development, with live, editable examples.

The HTML <code> tag

Element All modern browsers Updated
Quick answer

The HTML <code> element marks up a fragment of computer code — a function name, tag, or snippet — rendered in a monospace font. For multi-line code blocks, wrap a <code> inside a <pre> to preserve whitespace.

Overview

The <code> element identifies a fragment of computer code inline — an element name, a CSS property, a variable, a function name, or a short expression. Browsers style it in a monospace font, and it signals to assistive technology and search engines that the text is code rather than prose.

On its own it does not preserve line breaks or indentation, so it is meant for short, inline snippets within a sentence. For a multi-line code block, nest it inside a <pre> — the <pre><code>…</code></pre> pairing keeps the whitespace intact while still marking the content as code.

When the snippet contains literal HTML, remember to escape the special characters: write &lt; for < and &amp; for &, or the browser will try to render them as markup. It is distinct from its cousins <kbd> (user keyboard input) and <samp> (program output).

Syntax

<p>Use the <code>map()</code> method.</p>

Example

Live example
<p>Install with <code>npm install</code>, then run <code>npm start</code>.</p>

Best practices

  • Use <code> for short, inline code references — tags, properties, function names, values.
  • For a multi-line code block, wrap a <code> in a <pre> to preserve whitespace.
  • Escape literal HTML inside it (&lt;, &amp;) so it displays as text rather than rendering.
  • Style it with the CSS font-family and a subtle background if the browser default does not suit your design.

Frequently asked questions

What is the code element used for?
To mark a fragment of computer code inline — an element name, property, variable or expression — which browsers render in a monospace font.
How do I create a multi-line code block?
Nest a <code> inside a <pre>. The <pre> preserves line breaks and indentation; the <code> adds the code semantics.
How do I show literal HTML tags inside code?
Escape the angle brackets and ampersands: use &lt; for < and &amp; for &, so they display as text.
What is the difference between code, kbd and samp?
<code> is source code, <kbd> is what the user types, and <samp> is program output.