Code block

A code block shows code as it is written: in a fixed-width font, with its line breaks and indents kept. It is <pre> around <code>. Put it in a figure with a figcaption to give it a caption beneath, the same as every example on this site. A class on the code names its language, and small spans colour it.

A plain block

On its own, pre and code give a dark block in a fixed-width font, centred and no wider than a comfortable line.

git status
<pre><code>git status</code></pre>
A block with no language and no caption

With a caption

Wrap the block in a figure and add a figcaption after it. The caption sits centred just beneath the block, tied to it rather than spaced like the text around it. Images, video and embeds use the same figure, so every kind of example on a page lines up the same way.

git status
Check the state of a repository
<figure>
  <pre><code>git status</code></pre>
  <figcaption>Check the state of a repository</figcaption>
</figure>
A block with a caption beneath

Language label

Give the code a class of html, css or js and a small tab in the top corner names the language.

p { color: navy; }
Navy paragraphs
<figure>
  <pre><code class="css">p { color: navy; }</code></pre>
  <figcaption>Navy paragraphs</figcaption>
</figure>
The class that adds the label

Highlighting

Colour comes from spans around the parts of the code, each with a class for what it is. The spans are written into the page, by hand or by whatever builds it, so the colours show with no script running in the browser.

p { color: navy; }
A property and its value, each in a span
<figure>
  <pre><code class="css">p { <span class="attr">color</span>: <span class="value">navy</span>; }</code></pre>
  <figcaption>A property and its value, each in a span</figcaption>
</figure>
The two spans that colour it
// add two numbers
function add(a, b) {
  return a + b;
}
add(2, 3); // 5
JavaScript with keywords, names, numbers and comments coloured
<figure>
  <pre><code class="js"><span class="comment">// add two numbers</span>
<span class="kw">function</span> <span class="fn">add</span>(a, b) {
  <span class="kw">return</span> a + b;
}
<span class="fn">add</span>(<span class="num">2</span>, <span class="num">3</span>); <span class="comment">// 5</span></code></pre>
  <figcaption>JavaScript with keywords, names, numbers and comments coloured</figcaption>
</figure>
A span for each keyword, name, number and comment
ClassColours
tagHTML tags and CSS at-rules
attrHTML attributes and CSS properties
valueAttribute values, CSS values and strings
commentComments, in any language
kwJavaScript keywords
fnJavaScript function and method names
numNumbers

Long lines

Code is never wrapped, because a wrapped line can change its meaning. A line too long for the block scrolls sideways inside it instead. Add tabindex="0" so a keyboard user can reach the block and scroll it with the arrow keys.

.card { box-shadow: 0 1px 2px rgb(0 0 0 / 0.05), 0 4px 8px rgb(0 0 0 / 0.06), 0 12px 24px rgb(0 0 0 / 0.08), 0 24px 48px rgb(0 0 0 / 0.1); }
A long line that scrolls within the block
<figure>
  <pre tabindex="0"><code class="css">.card { <span class="attr">box-shadow</span>: <span class="value">0 1px 2px rgb(0 0 0 / 0.05), 0 4px 8px rgb(0 0 0 / 0.06), 0 12px 24px rgb(0 0 0 / 0.08), 0 24px 48px rgb(0 0 0 / 0.1)</span>; }</code></pre>
  <figcaption>A long line that scrolls within the block</figcaption>
</figure>
A block the keyboard can scroll

Output

What a program prints is not code, so mark it with samp instead of code. Here is what git status printed.

On branch main
nothing to commit, working tree clean
What git status prints
<figure>
  <pre><samp>On branch main
nothing to commit, working tree clean</samp></pre>
  <figcaption>What git status prints</figcaption>
</figure>
Program output in a block

Accessibility

Real text
The code is text, never a picture of text, so it can be read aloud, enlarged, searched and copied.
Escaping
Write &lt; for < and &amp; for & inside the block, so the browser shows HTML as code instead of running it.
Colour
The colours are an aid, not the meaning. Every token is still plain text, and each colour is bright enough to read on the dark block.