2.6 — Code, Preformatted, and Computer Output Elements
Technical documentation, programming tutorials, and scientific writing demand far more than ordinary paragraphs. Standard <p> elements collapse multiple spaces, discard tabs, and merge line breaks. HTML solves this with a dedicated set of elements that preserve exact text geometry while adding machine‑readable semantics.
Computer Output & Ingestion Elements
<code> – Inline Code
Use <code> for short syntax fragments, variable names, or commands. It renders in a monospace font and announces itself as a code fragment to screen readers.
<p>Use <code>console.log()</code> to print output.</p>
Use console.log() to print output.
<pre> – Preformatted Block
The <pre> element is block‑level and preserves every space, tab, and line break exactly as written. For a whole code listing, nest <code> inside <pre> – the combination is the standard for code blocks.
<pre>
<code>
function greet() {
console.log("Hello, world!");
}
</code>
</pre>
function greet() {
console.log("Hello, world!");
}
<kbd> and <samp> – Interaction Simulation
<kbd> marks explicit keyboard input (or voice commands). <samp> represents sample output from a program or terminal.
To depict a terminal session, nest <kbd> inside <samp>:
<samp>
user@host:~$ <kbd>ls -l</kbd>
total 12
drwxr-xr-x 2 user user 4096 Jul 13 10:00 .
</samp>
total 12
drwxr-xr-x 2 user user 4096 Jul 13 10:00 .
<var> – Variables
Wrap mathematical or programming variables in <var> to visually differentiate them (italic by default) and signal their role to parsers.
The area is <var>w</var> × <var>h</var>.
Special Text Markers
<mark> – Highlighted Relevance
Use <mark> to highlight text for reference, like search‑match strings. It carries strong semantic weight; screen readers may announce "highlighted". Never use it purely as decoration.
<p>The quick brown fox jumps over the <mark>lazy dog</mark>.</p>
The quick brown fox jumps over the lazy dog.
<small> – Fine Print
The <small> element downgrades text priority – typically for disclaimers, copyright lines, or side comments. It reduces font size by default but remains block‑inline; the important part is the semantic role.
<p>All rights reserved. <small>© 2026 Example Corp.</small></p>
All rights reserved. © 2026 Example Corp.
<u> – Unarticulated Annotation
Today <u> is no longer merely an underline; it marks text that has a non‑textual annotation (e.g., a misspelling indicator). It draws an underline to draw attention but shouldn't be confused with a hyperlink.
<p>Please review the <u>speling</u> of this word.</p>
Please review the speling of this word.
<sub> and <sup> – Baseline Shifts
<sub> drops text below the baseline (chemical formulas, indices). <sup> raises it above (exponents, ordinal indicators).
<p>Water: H<sub>2</sub>O. Energy: E = mc<sup>2</sup>.</p>
Water: H2O. Energy: E = mc2.
<wbr> – Word Break Opportunity
The void element <wbr> suggests a safe line‑break spot in long strings or URLs. It has no visible rendering but tells the layout engine, “break here if necessary”.
<p>https://verylong.example.com/path/<wbr>to/<wbr>resource</p>
https://verylong.example.com/path/
Ruby Annotations – Phonetic Guides
The <ruby> element provides phonetic annotations (Furigana, Pinyin) above or alongside base characters. It uses three child elements:
<rt>– the annotation text (pronunciation).<rp>– fallback parentheses shown only if the browser doesn’t support ruby. Inside<ruby>, you place<rp>(</rp><rt>…</rt><rp>)</rp>so that older browsers display(reading)inline.
<ruby>
漢字 <rp>(</rp><rt>kanji</rt><rp>)</rp>
</ruby>
Modern browsers display the ruby text above the base; older ones display 漢字 (kanji) – a perfect graceful degradation.
HTML’s text‑level semantics go far beyond bold and italic. Elements like <code>, <pre>, <kbd>, <samp>, and <var> give technical content a proper voice. Markers like <mark>, <small>, <u>, and baseline‑shifters <sub>/<sup> fine‑tune meaning without visual hacks. <wbr> manages layout, and <ruby> handles complex typography. Use them correctly, and your documents become a precise, accessible data structure.