HTML <pre> Tag

Beginner
7 min read
Updated: Nov 2026
4 examples
Live preview

Definition and Usage

The <pre> element represents preformatted text shown exactly as written in the HTML file. Spaces, tabs, and line breaks stay visible. Browsers typically use a monospace font.

For code listings, nest <code> inside <pre>. Use <samp> for sample output and <kbd> for typed input when that meaning fits. Content inside <pre> is still HTML — escape special characters when needed.

Baseline · Widely available.

Not deprecated. Stable across browsers since about July 2015 (and much earlier in practice).

Deprecated attributes.

Do not use width or wrap on <pre>. Prefer CSS width and white-space.

Note.

A leading newline right after the opening <pre> tag is stripped by the HTML parser (not in XHTML). Put the first line of text on the same line as <pre> when every blank line matters.

HTML Syntax

syntax.html
<pre> text…</pre>

Both start and end tags are required. Content is phrasing content only (no nested blocks like <div> or <p>). Escape &lt; and &amp; when those characters should appear as text.

Attributes

Modern <pre> has no useful element-specific attributes — only globals. Two legacy attributes remain for reference:

AttributeDescriptionNotes
widthDeprecated. Preferred character width for a line — no reliable visual effect todayUse CSS width instead
wrapDeprecated. Old overflow / wrap hint — ignored in modern browsersUse CSS white-space instead

Global Attributes

The <pre> tag also supports the Global Attributes in HTML.

Live Preview

Switch presets and see real <pre> whitespace (and optional <code>) update live.

<pre> keeps spaces; nest <code> for code blocks.

Preview
Line 1
  indented
    more

<pre> … </pre>

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Preserve Spaces and Line Breaks

Show text exactly as typed, including extra spaces.

HTML
<pre>This      text
  will      be
  displayed exactly
  as       written.</pre>
Try It Yourself

How It Works

  1. Open <pre> so the browser keeps whitespace.
  2. Type spaces and newlines the way you want them to look.
  3. Close </pre>. Both tags are required.
  4. Compare with a normal <p> — paragraphs collapse extra spaces.

Example 2 — Code Block with pre + code

Keep indentation and mark the text as computer code.

HTML
<pre><code>function greet(name) {
  return "Hello, " + name;
}</code></pre>
Try It Yourself

How It Works

  1. Open <pre> for a block that keeps line breaks.
  2. Put <code> inside to mark the content as code.
  3. Write the program text with real newlines and indent spaces.
  4. Close </code> then </pre> in that order.

Example 3 — Escape Angle Brackets

Show HTML tags as text inside a code sample.

HTML
<pre><code>&lt;button&gt;Save&lt;/button&gt;
&lt;button type="reset"&gt;Reset&lt;/button&gt;</code></pre>
Try It Yourself

How It Works

  1. Decide what readers should see (for example <button>).
  2. In the HTML file, write &lt; (and &amp; for &) so tags are not parsed.
  3. Keep the sample in <pre><code> for layout plus meaning.
  4. The browser displays angle brackets as characters, not as real elements.

Example 4 — Accessible ASCII Art

Describe diagrams made of characters so screen readers get the meaning.

HTML
<figure>
  <pre role="img" aria-label="A simple house drawn with text characters">
  /\
 /  \
/____\
|    |
| [] |
|____|
  </pre>
  <figcaption>A house drawn with ASCII characters.</figcaption>
</figure>
Try It Yourself

How It Works

  1. Wrap the art in <figure> when it is a self-contained illustration.
  2. Put the characters in <pre> so spacing stays aligned.
  3. Add role="img" and aria-label so assistive tech can announce a short description.
  4. Use <figcaption> for a visible caption.

Edge Cases & Pitfalls

Most pre bugs are missing escapes, wrong nesting, or treating character art as plain text for everyone.

escape

Escape < and & in source

Unescaped markup can create real elements or break the page. Prefer entities inside HTML source, especially for HTML samples.

newline

First newline after <pre> is stripped

The HTML parser removes the first leading newline after the opening tag. Start content on the same line when that blank line matters.

content

Only phrasing content inside

Do not nest block elements. Use <code>, <samp>, <kbd>, and similar phrasing tags when needed.

a11y

Describe ASCII diagrams

Screen readers may read character art letter by letter. Pair with aria-label (and often a caption) so the meaning is clear.

layout

Long lines can overflow

Default white-space: pre does not wrap. Add CSS such as overflow-x: auto or adjust white-space for narrow screens.

Browser Support

The <pre> element is core HTML flow content in every browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<pre>

Widely available since about July 2015 (and long before). Chrome, Firefox, Safari, Edge, and Opera all support preformatted text with default monospace styling.

98% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
<pre> Widely supported

Bottom line: Safe to use everywhere for preformatted text. Nest code for listings, escape special HTML characters, and describe ASCII art for accessibility.

Key Takeaways

  • Role: <pre> keeps spaces and line breaks; default look is monospace.
  • Code: use <pre><code>…</code></pre> for multi-line samples.
  • Escape: write &lt; and &amp; in HTML source when needed.
  • Legacy: skip deprecated width / wrap; style with CSS instead.

One line: use <pre> when whitespace matters; nest <code> for code blocks.

Default CSS Settings

Typical user-agent styling for pre:

default.css
pre {
  display: block;
  font-family: monospace;
  white-space: pre;
  margin: 1em 0;
}

You can restyle with CSS (background, padding, overflow). User font preferences may override your font-family choice.

Frequently Asked Questions

The pre element shows preformatted text: spaces and line breaks are kept as written in the HTML source. Browsers usually render it in a monospace font.
Yes for code blocks. Nest code inside pre: pre keeps layout; code marks the text as computer code. Alone, code is for short inline phrases.
Yes when those characters would start real HTML tags. Write &lt; and usually &amp; for & so the browser shows them as text.
No modern element-specific attributes. Use global attributes. The old width and wrap attributes are deprecated and have no useful visual effect — use CSS instead.
If one or more newlines come right after the opening pre tag, the HTML parser strips the first newline. Put content on the same line as <pre> or accept that strip.
No. It is Baseline and widely available. Prefer it for preserved whitespace; obsolete peers like listing and plaintext are not for new pages.

Next: progress

Show task completion with the HTML progress element.

progress tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful