# `<pre>`

> The `<pre>` HTML element defines preformatted text, preserving both spaces and line breaks as they appear in the HTML code.

## How do I show code blocks with pre and code in HTML?

The element that displays preformatted text, preserving spaces, line breaks and indentation as written in the HTML. It's often used to display code snippets or text that requires a specific format.

- Topic: Formatting
- Type: tag
- Browser support: Chrome 1+, Chrome Android 18+, Edge 12+, Firefox 1+, Firefox Android 4+, Safari 4+, Safari iOS 3.2+

## Example

Wrong:

```html
<pre>
<div class="box">
  <p>Hi</p>
</div>
</pre>
```

Right:

```html
<pre><code>&lt;div class="box"&gt;
  &lt;p&gt;Hi&lt;/p&gt;
&lt;/div&gt;</code></pre>
```

Escaped tags inside `<pre><code>` show as text instead of being parsed and rendered.

## Common mistakes

- Pasting raw HTML into `<pre>`, so tags render instead of showing as text.
- Indenting the content to match the source, which adds spaces to the output.

## Key points

- Code blocks use `<pre><code>`, with `<code>` inside `<pre>`.
- `<`, `>` and `&` inside the block are escaped as `&lt;`, `&gt;` and `&amp;`.
- Long lines scroll with `overflow-x: auto` instead of breaking the layout.

## Interview questions

### Why is the first line break in pre ignored?

Browsers drop one newline right after `<pre>`, but indentation and other line breaks inside are kept as written.

### How do I wrap long lines in pre?

Use CSS `white-space: pre-wrap`, or keep the lines and add `overflow-x: auto` for scrolling.

## Related cards

- [`<code>`](https://wonderium.dev/html/code.md)
- [`<samp>`](https://wonderium.dev/html/samp.md)
- [white-space](https://wonderium.dev/css/white-space.md)
- [tab-size](https://wonderium.dev/css/tab-size.md)

---

- Level: [HTML](https://wonderium.dev/html.md)
- HTML version: https://wonderium.dev/html/pre
- Last updated: 2026-11-25
