pre element

Use pre when spaces and line breaks are part of the meaning of the text, such as source code or a text layout.

What you will learn

What does pre do?

pre means “preformatted text.” Browsers normally collapse repeated spaces and line breaks, but text inside pre is displayed with its whitespace preserved, usually in a monospace font.

Example: source code

<pre><code>
const message = "Hello";
console.log(message);
</code></pre>

Use code inside pre when the content is computer code. The pre element controls the preserved layout; code communicates what the content means.

Important details

A simple style for long code

pre {
  overflow-x: auto;
  padding: 1rem;
  white-space: pre;
}

Related pages