Code Block HTML: pre and code Tags with Examples

November 11, 2026 · 9 min read

Code Block HTML: How to Use the pre and code Tags

The standard code block HTML pattern nests a code element inside a pre element. The pre keeps your spaces and line breaks, and code marks the text as computer code. Escape every < as &lt; and every & as &amp;, add a little CSS, and the block displays exactly what you typed.

The Basic Code Block HTML Pattern: code Inside pre

Each element does one job. The WHATWG HTML spec defines pre as a block of preformatted text, and code as a fragment of computer code. Put them together and you get both behaviours:

<pre><code>function greet(name) {
  return 'Hello, ' + name;
}</code></pre>

The browser shows those three lines in a monospace font, with the two-space indent intact. That comes from the default stylesheet in the spec's rendering section, which gives pre the rules font-family: monospace and white-space: pre. Without pre, the browser collapses the indent and joins the lines into one.

The order matters. Put code inside pre, not the other way round, because pre is a block element and code is inline. The spec's own example uses exactly this nesting.

You'll still find pages that fake a block with a div, br tags and &nbsp; for every indent space. Skip that. It's hard to edit, it copies badly, and it loses the meaning that pre and code carry.

To give a block a caption, such as a file name, wrap it in figure and add a figcaption. The spec treats the figcaption as the caption for the rest of the figure, and it gives you a styling hook for a title bar.

<figure>
  <figcaption>greet.js</figcaption>
<pre><code class="language-js">greet('Ada');</code></pre>
</figure>

The pre line starts at column 0 on purpose. You'll see why in the whitespace section below.

Inline Code vs Block Code in HTML

On its own, code is inline. It sits inside a sentence like any other phrase:

<p>Call <code>greet()</code> after the page loads.</p>

That renders as a normal paragraph with greet() in monospace. To show a block of code in HTML, wrap the same code element in a pre. One tag covers both cases; the pre around it is what makes it a block.

Three related tags round out the set. Use kbd for keys the reader presses, samp for program output, and var for a variable name. A terminal session can mix them inside one pre, with code for the command and samp for what it prints.

<pre><code>node --version</code>
<samp>v22.14.0</samp></pre>

Both lines render in monospace, one under the other. The markup now tells a script or a stylesheet which line is input and which is output, even though they look the same by default.

How Do You Show HTML Code on a Web Page Without It Rendering?

You escape the characters the parser treats as markup. Inside pre and code, the browser still parses tags, so a raw <a> turns into a working link and the tag itself never shows. Replace each < with &lt; and each & with &amp;.

This first version renders a clickable link instead of showing the markup.

<pre><code><a href="/docs">Docs</a></code></pre>

This second version displays the anchor tag as text.

<pre><code>&lt;a href="/docs"&gt;Docs&lt;/a&gt;</code></pre>

Escaping > as &gt; is optional, since a lone > can't start a tag, but most tools do it for symmetry. The & matters when your code contains something like &copy;, which would otherwise turn into a copyright sign.

You might see old tutorials suggest the xmp element to skip escaping. It's obsolete. The spec's list of obsolete features says to use pre and code instead and escape < and &. For the Markdown side of the same problem, see our Markdown code block guide, where backticks do the escaping for you.

Whitespace and Line Breaks Inside pre

Four whitespace behaviours catch people out, because the browser shows everything inside pre exactly as the parser hands it over.

The first newline is dropped. The HTML parser strips one newline that comes directly after the <pre> start tag. These two blocks render identically, with no blank line on top:

<pre>first line
second line</pre>

<pre>
first line
second line</pre>

Source indentation counts. Everything between the tags is shown verbatim. That includes the spaces you used to indent your HTML file.

<div>
  <pre><code>if (ready) {
    start();
  }</code></pre>
</div>

Here if (ready) { starts at the left edge, while start(); gets four spaces and the closing brace gets two. The code looks misaligned. Start every line of the code at column 0 of your file, even if that breaks your template's indentation.

Tabs are 8 columns wide. The CSS tab-size property has an initial value of 8, per MDN's tab-size reference. Set tab-size: 2 or 4 on the pre if your code uses tab characters.

Long lines are the last gotcha. With white-space: pre, nothing wraps, so a long line runs past the edge of its box. The next section fixes that.

Styling a Code Block with CSS

You style pre and code like any other element. This short rule set covers most sites:

pre {
  background: #f6f8fa;
  color: #1f2328;
  padding: 1rem;
  border-radius: 6px;
  line-height: 1.5;
  overflow-x: auto;
  tab-size: 2;
}

pre code {
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.875rem;
}

:not(pre) > code {
  background: #eff1f3;
  padding: 0.1em 0.35em;
  border-radius: 4px;
}

overflow-x: auto adds a horizontal scrollbar only when a line is too long. The last rule gives inline code its own light background and skips code inside a pre, so a block never shows a second box inside the first.

For a dark theme, swap the colours inside a media query so the block follows the reader's system setting.

@media (prefers-color-scheme: dark) {
  pre { background: #161b22; color: #e6edf3; }
  :not(pre) > code { background: #262c36; }
}

If you'd rather wrap than scroll, use white-space: pre-wrap. It keeps your spaces and line breaks but lets long lines wrap. We prefer scrolling for code, because a wrapped line looks like two lines and readers misread where a statement ends.

Adding Line Numbers to an HTML Code Block

HTML has no built-in line numbers, but CSS counters can add them. Wrap each line in a span, then count the spans:

<pre class="numbered"><code><span class="line">const a = 1;</span>
<span class="line">const b = 2;</span>
<span class="line">console.log(a + b);</span></code></pre>
.numbered { counter-reset: line; }
.numbered .line { counter-increment: line; }
.numbered .line::before {
  content: counter(line);
  display: inline-block;
  width: 2em;
  margin-right: 1em;
  text-align: right;
  color: #8b949e;
  user-select: none;
}

The result shows 1, 2 and 3 in a grey gutter. Because the numbers are generated content, most browsers leave them out when a reader copies the code, and user-select: none makes that explicit.

The honest limitation: you have to wrap every line in a span, which gets tedious by hand. A build script or highlighter plugin can do it for you. Our code block generator runs in your browser and outputs HTML with line numbers and highlight.js themes already applied.

How Do You Add Syntax Highlighting to an HTML Code Block?

Add a class that starts with language- to the code element, such as class="language-js", which is the convention the HTML spec itself suggests. Then load a highlighter like highlight.js or Prism. It reads the class, wraps keywords and strings in spans, and a theme stylesheet colours them. For the names that go after language-, see our list of code block language identifiers.

From a Markdown Fence to pre and code

If you write in Markdown, you never type this markup yourself. A fenced block with an info string compiles to pre and code with a language- class. The CommonMark spec shows this output in its examples.

```html
<p>Read the <a href='/docs'>docs</a> & say hi.</p>
```

Our editor renders Markdown with the marked library, which turns that fence into this HTML.

<pre><code class="language-html">&lt;p&gt;Read the &lt;a href=&#39;/docs&#39;&gt;docs&lt;/a&gt; &amp; say hi.&lt;/p&gt;
</code></pre>

Every <, >, & and single quote is escaped for you. The demo below uses a tilde fence, which CommonMark treats the same way as a backtick fence.

A fenced HTML snippet:

<p>Read the <a href='/docs'>docs</a> & say hi.</p>
<ul>
<li>One</li>
<li>Two</li>
</ul>

Press Export HTML. In the output, look for pre, code and the language-html class. Every angle bracket in the snippet comes out as an entity, so the browser shows the tags instead of running them.

52 words320 characters11 lines
Markdown

The Markdown to HTML converter runs in your browser with no usage limit. One thing it won't do is colour the code: our editor's preview and its HTML output keep code blocks plain, so add a highlighter yourself if you need colours. For a .docx file instead of a web page, the same fence can become a code block in Word.

Good code block HTML comes down to three habits: nest code inside pre, escape < and &, and start each line at column 0. Add the CSS above for padding and scrolling, and a highlighter if you want colour. If you'd rather write a fence and let a converter produce the markup, try it in the editor, where Ctrl+E (Cmd+E on Mac) inserts a fenced code block.