<pre> Tag in HTML

Quick facts

CategoryFlow content, palpable content
Content modelPhrasing content (text and inline elements such as <code>, <span>, <a>)
Tag omissionNeither the start nor the end tag can be omitted
Permitted parentsAny element that accepts flow content
DOM interfaceHTMLPreElement

Syntax

<pre> Text keeps its spacing and its line breaks. </pre>

Live example

Edit the code below and watch the preview update instantly.

index.html
Preview

How whitespace works in <pre>

Normal HTML collapses runs of spaces and ignores line breaks. Inside <pre> the browser applies white-space: pre, so the text renders as typed. One exception: a single newline straight after the opening <pre> tag is dropped, which lets you start the content on its own line without adding a blank first line.

Tags still work inside <pre>. To show literal HTML, escape it: write &lt; for < and &amp; for &.

Attributes

AttributeDescription
—Only the global attributes apply. The old width attribute is deprecated; set the width with CSS instead.

Common examples

A code block with highlighting class

The usual pattern for code: <pre> keeps the formatting, <code> says it’s code.

<pre><code class="language-html">&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt;&lt;title&gt;Hi&lt;/title&gt;&lt;/head&gt; &lt;/html&gt;</code></pre>

Text art and fixed layouts

Anything that relies on exact spacing — ASCII art, aligned columns, poems with indentation — keeps its shape in <pre>.

<pre role="img" aria-label="A cat face"> /\_/\ ( o.o ) > ^ < </pre>

Wrapping long lines

By default long lines run off the side. white-space: pre-wrap keeps spaces but wraps at the edge.

<style> pre { white-space: pre-wrap; overflow-wrap: anywhere; } </style>

<pre> vs CSS white-space

<pre> means “this text is preformatted — its spacing is part of the content”. CSS white-space: pre (or pre-wrap) only changes how spaces and line breaks are displayed, without adding any meaning.

Use <pre> for code, command-line output and text art, where losing the spacing would change the content. Use CSS white-space when you just want normal text, such as a user’s address or a message they typed, to keep its line breaks.

<pre>white-space: pre-wrap
Adds meaningYes (preformatted)No (visual only)
Default fontMonospaceUnchanged
Best forCode, text artUser text with line breaks

Common mistakes to avoid

  • Using pre for layout. Lining up content with spaces breaks on small screens; use CSS grid or flexbox.
  • Unescaped HTML inside pre. Tags inside <pre> still run; write &lt; to show a <.
  • Accidental first-line indentation. Spaces before the first line of code in your HTML file show up in the page.
  • Pages that scroll sideways on phones. Add overflow-x: auto or pre-wrap so long lines don’t widen the whole page.

Accessibility & SEO tips

  • Give text art a role="img" and an aria-label, otherwise screen readers read out every slash and bracket.
  • Make scrollable code blocks reachable by keyboard with tabindex="0", so people without a mouse can scroll them.
  • Code in <pre> is indexed as text, so real, copyable examples help tutorial pages rank for coding questions.

Browser support

Supported in every browser, including Chrome, Edge, Firefox, Safari and their mobile versions — it has been part of HTML since the very first versions.

Related tags

Frequently asked questions

What is the difference between <pre> and <code>?
<pre> preserves whitespace and line breaks, while <code> marks text as computer code without changing how whitespace is handled. For a multi-line code block, use both together: <pre><code>...</code></pre>.
How do I show HTML tags inside <pre>?
Escape the special characters. Write &lt; instead of < and &amp; instead of &, otherwise the browser treats the text as real tags instead of displaying it.
How do I stop long lines in <pre> from overflowing the page?
Add overflow-x: auto in CSS to give the block a horizontal scrollbar, or use white-space: pre-wrap to wrap long lines while still keeping the existing spaces and line breaks.
How do I change the font of <pre>?
Set font-family in CSS, for example pre { font-family: ui-monospace, Consolas, monospace; font-size: 0.9rem; }. Keep it monospace so the columns stay lined up.
Does <pre> keep tabs?
Yes. Tabs are shown as 8 spaces by default; the CSS tab-size property, for example tab-size: 2;, makes them narrower.
How do I add line numbers to a <pre> block?
HTML has no built-in line numbers. Use a syntax-highlighting library such as Prism with its line-numbers plugin, or CSS counters with one <span> per line.