Skip to content

<pre> - Preformatted Text

Block-level HTML 2.0

The <pre> element represents preformatted text where whitespace, line breaks, and formatting are preserved exactly as written in the HTML. It’s commonly used for code blocks, ASCII art, and text that requires specific formatting.

Result
<pre>
Text with preserved spacing
and line breaks
</pre>

All whitespace inside <pre> is preserved, including spaces, tabs, and newlines.

The <pre> element supports all global attributes like id, class, style, and lang.

<pre id="code-sample" class="language-javascript">
// Code here
</pre>
Result

Combine <pre> with <code> for semantic code blocks:

Result
Result
Result
Result
Result
Result
Result
Result
Result

When displaying HTML code, escape special characters:

Result
Result
Result
Result

Combining <pre> and <code> provides better semantic meaning:

<pre><code>
function example() {
return true;
}
</code></pre>

Help assistive technology by specifying the programming language:

<pre><code class="language-javascript">
const message = "Hello";
</code></pre>
<pre><code class="language-python">
message = "Hello"
print(message)
</code></pre>

Ensure code blocks can scroll horizontally:

Result

Keep code readable:

<pre style="font-size: 0.9rem;">
Code at readable size
</pre>
<p>This is regular text that wraps normally.</p>

Indentation in HTML source affects display:

<pre>
This text is indented
in the source code
</pre>
Result
BrowserVersionNotes
Chrome1+Full support
Firefox1+Full support
Safari1+Full support
Edge12+Full support
IE3+Full support

The <pre> element has been supported since early HTML versions. Modern browsers support overflow-x: auto for horizontal scrolling.