<pre> Preformatted text
Shows text exactly as typed, spaces and line breaks included.
<pre> displays text exactly as you typed it. Spaces, tabs and line breaks all survive, usually in a monospace font. Code blocks, ASCII art and poems with odd spacing are the classic uses.
For code, put a <code> element inside. Tags are still parsed inside pre, so a literal less-than sign has to be written <. Long lines do not wrap by default, and on a phone that means horizontal scrolling.
The pre element in the HTML Living Standard (opens in a new tab) also explains why a newline right after the start tag is ignored. For code samples, pair it with the code element.
Syntax
<pre><code>line 1
indented line 2</code></pre>When to use it
Pre is for text where spacing and line breaks carry meaning: code samples, command-line output, configuration files, plain-text tables copied from a terminal, ASCII diagrams, poems whose indentation is part of the form. Pair it with <code> for source code, <samp> for program output and <kbd> for what the user types.
A short inline snippet, such as one tag named in a sentence, needs only <code>. A poem with ordinary lines often reads better as paragraphs with <br>, which wrap on small screens. Real tabular data belongs in a table, which is far more accessible than columns lined up with spaces.
Tab characters inside pre display eight spaces wide by default, so code indented with tabs can look much wider than it did in your editor. Set tab-size:2 in CSS or indent with spaces. Readers who copy from a pre also get the exact whitespace, trailing spaces you never meant to publish included.
Live example
Edit the code and watch the preview.
<pre><code><ul>
<li>Milk</li>
<li>Eggs</li>
</ul></code></pre>More examples
Terminal session with kbd and samp
A terminal transcript mixes what the user types with what the computer prints. Inside pre, <kbd> marks the typed command and <samp> the output. CSS white-space:pre-wrap lets long lines wrap on narrow screens instead of scrolling.
<style>
pre{background:#222;color:#eee;padding:10px;white-space:pre-wrap}
kbd{color:#9f9}
</style>
<pre>$ <kbd>ls images</kbd>
<samp>cat.svg dog.svg logo.svg</samp>
$ <kbd>echo "done"</kbd>
<samp>done</samp></pre>ASCII diagram with a text alternative
Screen readers would read an ASCII diagram character by character. Giving the pre role="img" and an aria-label replaces that with one clear sentence, while the figure caption explains the diagram to everyone.
<figure>
<pre role="img" aria-label="Diagram: the browser sends a request to the server, and the server sends back a page.">
+---------+ request +--------+
| Browser | -----------> | Server |
| | <----------- | |
+---------+ page +--------+
</pre>
<figcaption>How a web page is loaded.</figcaption>
</figure>Tip: Code blocks go in <pre><code>...</code></pre>, with < escaped as <.
Browser support
Pre has worked in every browser since the early days of HTML. Skip its obsolete width attribute, and the old listing and xmp elements it replaced. Widths and wrapping are a job for CSS properties such as white-space and overflow-x.
Accessibility
Screen readers read ASCII art and diagrams in pre character by character. Wrap them in a figure with role="img" plus an aria-label or a figcaption. Text alternatives for ASCII art are covered in the W3C note Understanding Non-text Content (opens in a new tab).
Common mistakes
Wrong
<pre><p>Hello</p></pre>Right
<pre><code><p>Hello</p></code></pre>The browser still parses markup inside pre. Escape < and & to show code as text.
Frequently asked questions
How do I show a block of code in HTML?
Wrap it in <pre><code>...</code></pre>. The <pre> keeps whitespace and line breaks, and <code> marks the content as computer code. Escape special characters too: < for a less-than sign and & for an ampersand. Skip that and the browser tries to parse your HTML example as real tags. If the indentation is a mess, run the markup through Pretty HTML, an HTML formatter (opens in a new tab) before pasting it into pre.
How do I stop pre text from overflowing on mobile?
<pre> never wraps lines by default, so long ones run off the screen. You have two fixes. Add overflow-x:auto and the block scrolls sideways, or use white-space:pre-wrap to keep the spacing and allow wrapping. For code, scrolling usually wins, since wrapped code is harder to read.
Practise it
- Quotes and Code (Module 2: Text)