This is a card in Dave's Virtual Box of Cards.

Formatting code blocks in HTML

Page created: 2026-12-05

Today I finally decided to make up my mind once and for all on how I display source code blocks in HTML. I’d take a look at the options and pick a solution that would be "the right thing" 99% of the time.

This has vexed me for years, by the way. Ever since I got on the "HTML should be used semantically" train. It turns out that once you get on that train, the basic ride is correct and important, but only regret awaits those who follow the train to its final destination. Ride the train, but get off at the first reasonable stop and enjoy your life. That train is destined to a semantic hell constructed block-by-block with good intentions.

Let’s begin.

Inline code

Displaying inline code correctly is absurdly easy and obvious:

You can average a list of numbers
with <code>avg=: +/ % #</code> in
the J programming language.

Here’s what it looks like with my styling on this page:

You can average a list of numbers with avg=: +/ % # in the J programming language.

The expected and desired rendering is as one single line with the snippet of code often styled a little differently (typically in a "monospace" or "typewriter" font). In my case, I’ve added a background color to make it stand out even more.

Code blocks with <pre>

Blocks of code are traditionally ("gather around little ones to hear about HTML 2.0") done with the <pre> tag. Here’s a Tcl language example:

<pre>
#include <stdio.h>
int main() {
   printf("Hello, World!");
   return 0;
}
</pre>

That will display pretty much just like you see it on this page. I’ve added padding and a background color to my code blocks to make them stand out.

The theoretical problem with pre is that it’s a general tag for any pre-formatted text including poetry, ASCII art diagrams, and any email-like text formatted with spaces and hard line-breaks to separate paragraphs, etc.

You wouldn’t always want to display all of these things as "code". I might want a poem on my page and a code sample and I wouldn’t necessarily want them to look the same.

Nesting <code> in <pre>

The HTML5 spec calls for enclosing your code with a <code> tag inside a <pre> tag:

<pre><code>
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
</code></pre>

But good luck styling that! Seriously, you can do it, of course. Anything is possible with CSS. But it’s not trivial. Go ahead and try it right now from scratch.

And yes, I can pass that test. But I’ve been writing CSS since browsers began to support it.

My point is that styling the "nested tags" solution is not simple or easy. It will always be a challenge to make everything look good on a page in which you are using <pre> and <code> separately elsewhere for their intended purposes.

At the very least, prescribing these nested tags sets up a very difficult and non-intuitive styling challenge for beginners!

(It feels a little ironic to me somehow that the only time <pre><code> …​ </code></pre> looks okay out of the box is when you don’t try to style them at all.)

Using classes

Which leaves two other less pure options:

  • <code class="block"> …​ </code>

  • <pre class="code"> …​ </pre>

Semantically, "code" is clearly the correct answer. But…​

With the <code> with class solution, you have to use white-space: pre; in CSS to get the pre-formatting feature and display: block; to make it a block element.

With the <pre> with class solution, you just style your block as you normally would.

I’ve tried both on many different occasions and <pre class="code"> wins every time.

Why? It’s super simple: The "pre-formatted" part is more important for most readers than the semantic "this is code" part.

What’s that? You need convincing?

Okay, let’s make a simple example page. I’ll style it ever so slightly to format the code blocks for legibility.

<html>
<head>
        <style>
        body {
                background: white;
        }
        code.block {
                display: block;
                white-space: pre;
                background: silver;
                padding: 1em;
        }
        pre.code {
                background: silver;
                padding: 1em;
        }
        </style>
</head>
<body>

<p>Here is some code with &lt;code&gt;:</p>

<code class="block">
#include &lt;iostream&gt;

int main() {
    std::cout &lt;&lt; "Hello World";
    return 0;
}
</code>

<p>And here is some code with &lt;pre&gt;:</p>

<pre class="code">
program HelloWorld;
begin
  WriteLn('Hello World');
end.
</pre>

</body>
</html>

As you can see, I have CSS styles for the classes code.block and pre.code. Then a <code> example followed by a <pre> example.

Let’s look at it in a graphical browser, Dillo:

screenshot of page showing code in silver blocks as expected

Looks exactly as desired.

Now, let’s take a look at that same page in text-mode terminal browser, w3m:

screenshot of terminal showing same page as pure text

The <code> example got all smashed into one line. Because of course it did! The code element is not a block element and it is not a pre-formatted text element.

When CSS isn’t available (or isn’t delivered to your GUI browser, which happens all the time), you’re going to end up with whatever these tags do by default.

This is where the semantics butt heads with reality. I don’t make the rules. Oh boy do I ever not make the rules. This is just the way it is. I’m sorry.

(Of course, if you are only targeting text-mode browsers or plan to not use styles, then, again, I will concede that the "official" semantically-sinless nested <pre><code> …​ </code></pre> solution is just fine!)

Conclusion: I’ve picked <pre> with a class

So that’s what I’m picking. Nobody will be perfectly happy with it. I’m not. But I’ve learned when to get off the semantics train.

With a class, poetry and code can live peacefully side-by-side. (And <code> remains for inline use, of course.)

Poetry pre:

<pre>
    The Tcl language tickles the brain.
         Every
         thing
            is
             a
        string.
                Everything.
</pre>

Code pre:

<pre class="code">
    proc decr {varName {decrement 1}} {
        upvar 1 $varName var
        incr var [expr {-$decrement}]
    }
</pre>

It’s a compromise. I can live with it.

Related pages at: The Web.

TODO: link to my "how to make a website" page when it’s finished.