<pre>

Term side of the Card.
Topic: Formatting
Unit title: <pre>
Categories: Tag
Explanation side of the Card

Explanation: 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.

Browser compatibility:
  • The chrome browser supports this feature starting from version 1.
  • The edge browser supports this feature starting from version 12.
  • The firefox browser supports this feature starting from version 1.
  • The safari browser supports this feature starting from version 4.
Availability: 

Example

Wronghtml
<pre>
<div class="box">
  <p>Hi</p>
</div>
</pre>
Righthtml
<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.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy