pre
Preformatted text preserving whitespace and line breaks. VB distinguishes code blocks (pre+code) from bare preformatted text (poetry, ASCII art) with different visual treatments.
Description
The <pre> element preserves whitespace and line breaks exactly as written in the HTML source. VB makes an important distinction:
<pre><code>(code blocks) — Full treatment with background, padding, border-radius, andwhite-space: prewith horizontal scroll- Bare
<pre>(no<code>child) — Lighter treatment withpre-wrapand no background, for ASCII art, poetry, or other preformatted text that isn't code
This distinction uses pre:has(code) — no classes needed.
When to Use
- Code blocks: Wrap
<code>inside<pre>for multi-line source code - Terminal output: Wrap
<samp>inside<pre>for command output - ASCII art: Bare
<pre>for art that depends on character alignment - Poetry: Bare
<pre>when line breaks are part of the content's meaning - Data tables: Plain-text tabular data (though
<table>is usually better)
When NOT to Use
Examples
Code Blocks (pre + code)
The canonical pattern for displaying source code. VB detects the <code> child via pre:has(code) and applies full code-block styling:
<pre><code>function greet(name) { return `Hello, ${name}!`;} console.log(greet('World'));</code></pre>
Bold Highlighting in Code
<b> and <strong> inside code blocks get a subtle background highlight instead of relying on font weight alone:
/* Bold in code — highlight background */:where(code, pre) :where(b, strong) { background-color: oklch(from var(--color-interactive) l c h / 0.12); border-radius: var(--radius-s); padding-inline: var(--size-2xs);}
Syntax Highlighting
For colored syntax highlighting, VB provides the <code-block> web component (used throughout this documentation site). For external tools, add class="language-*" to the <code> element and integrate Prism.js or highlight.js.
Terminal Output (pre + samp)
Use <samp> inside <pre> for command-line output. This gets code-block styling since samp behaves like code in this context:
<pre><samp>$ npm installadded 125 packages in 3.2s $ npm run buildBuild completed successfully.</samp></pre>
Bare Pre (No Code Child)
Without a <code> or <samp> child, <pre> gets lighter styling — no background, no padding, and white-space: pre-wrap so long lines wrap rather than scroll. This is appropriate for ASCII art, poetry, and other preformatted text that isn't code.
<pre> * *** ***** **************** |||</pre>
<pre>Two roads diverged in a wood, and I—I took the one less traveled by,And that has made all the difference.</pre>
CSS Reference
pre { font-family: var(--font-mono); font-size: var(--font-size-sm); overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;} /* Code blocks: pre containing code gets full treatment */pre:has(code) { background: var(--color-surface-raised); padding: var(--size-m); border-radius: var(--radius-m); white-space: pre; word-wrap: normal; & code { background: transparent; padding: 0; font-size: inherit; }}
| Context | Background | White-space | Scroll |
|---|---|---|---|
pre:has(code) |
var(--color-surface-raised) |
pre (exact) |
Horizontal |
Bare pre |
None | pre-wrap (wraps) |
None |
/* Print: pre blocks avoid page breaks */@media print { pre { break-inside: avoid; }}
Accessibility
- Screen readers read
<pre>content in its linear order - Add
tabindex="0"to scrollable code blocks so keyboard users can scroll with arrow keys - Add
role="region"andaria-labelfor complex code blocks that benefit from a landmark - ASCII art is inaccessible to screen readers — add
aria-labeloralttext describing what it depicts