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>
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
<figure>
<pre><code>git status</code></pre>
<figcaption>Check the state of a repository</figcaption>
</figure>
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; }
<figure>
<pre><code class="css">p { color: navy; }</code></pre>
<figcaption>Navy paragraphs</figcaption>
</figure>
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; }
<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>
// add two numbers
function add(a, b) {
return a + b;
}
add(2, 3); // 5
<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>
| Class | Colours |
|---|---|
tag | HTML tags and CSS at-rules |
attr | HTML attributes and CSS properties |
value | Attribute values, CSS values and strings |
comment | Comments, in any language |
kw | JavaScript keywords |
fn | JavaScript function and method names |
num | Numbers |
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); }
<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>
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
<figure>
<pre><samp>On branch main
nothing to commit, working tree clean</samp></pre>
<figcaption>What git status prints</figcaption>
</figure>
Accessibility
- Real text
- The code is text, never a picture of text, so it can be read aloud, enlarged, searched and copied.
- Escaping
- Write
<for<and&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.