Normally, browsers collapse multiple spaces and ignore line breaks in HTML source. **`<pre>`** turns that off: whatever whitespace, indentation, and line breaks you write inside it are rendered exactly as-is, in a monospace font by default. This makes it ideal for displaying code blocks, ASCII art, or any content where the exact layout matters. It's very commonly paired with `<code>` (`<pre><code>...</code></pre>`) to represent a formatted, multi-line code snippet.
1Understanding <pre>
Normally, browsers collapse multiple spaces and ignore line breaks in HTML source. `<pre>` turns that off: whatever whitespace, indentation, and line breaks you write inside it are rendered exactly as-is, in a monospace font by default. This makes it ideal for displaying code blocks, ASCII art, or any content where the exact layout matters. It's very commonly paired with <code> (<pre><code>...</code></pre>) to represent a formatted, multi-line code snippet.
Remember that special characters like < and & still need to be escaped (<, &) inside <pre>, exactly as anywhere else in HTML — 'preformatted' only means whitespace is preserved, not that HTML parsing is disabled.
<pre><code>function add(a, b) {
return a + b;
}</code></pre>2Practical Example
Here is a real-world application of <pre> showing how it is used in production HTML.
<!-- Preserving ASCII art / exact spacing -->
<pre>
/\_/\
( o.o )
> ^ <
</pre>3Best Practices
Follow these guidelines when working with <pre>:
1. Use <pre><code>...</code></pre> together for multi-line code blocks
2. Escape <, >, and & characters inside <pre>, since it's still parsed as HTML
3. Don't rely on <pre> for layout spacing outside of genuinely preformatted content
Tip: Remember that special characters like < and & still need to be escaped (<, &) inside <pre>, exactly as anywhere else in HTML — 'preformatted' only means whitespace is preserved, not that HTML parsing is disabled.
<pre><code>function add(a, b) {
return a + b;
}</code></pre>