HTML <pre> Tag

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
HTML Text

What You’ll Learn

The <pre> tag preserves spaces and line breaks for code, poetry, and fixed-layout text. This guide covers syntax, pairing with code, common use cases, and best practices for beginners.

01

Preformatted

Keep exact spacing.

02

Code Blocks

Multiline examples.

03

pre + code

Semantic pairing.

04

Whitespace

ASCII art, tables.

05

CSS Styling

Classes on pre.

06

Accessible

Readable code display.

What Is the <pre> Tag?

The <pre> tag, short for preformatted text, is an HTML element designed to preserve both spaces and line breaks. It is particularly useful when you want to display code blocks or any text where formatting and spacing are crucial.

Valid HTML — Block Preformatted Element

Browsers render <pre> with white-space: pre by default, so indentation and line breaks in your source appear exactly on the page.

It replaced the obsolete <plaintext> and <listing> tags as the standard way to show unmodified text layout in modern HTML.

📝 Syntax

Wrap your code or text within opening <pre> and closing </pre> tags:

syntax.html
<pre>
  Your Code or Text Here
</pre>

Syntax Rules

  • Use pre for block content that needs preserved whitespace.
  • Nest code inside pre when displaying programmatic source code.
  • Indentation inside the tag is visible — align your source carefully.
  • Escape < and & when showing raw HTML in a pre block.

⚡ Quick Reference

TopicCode SnippetNotes
Basic pre<pre>Text</pre>Preserves spaces
Code block<pre><code>...</code></pre>Best practice
Stylingclass="code-block"Global attribute
DisplayBlock-levelMonospace font default
vs ppre keeps spacesp collapses whitespace
Browser supportUniversalAll browsers

⚖️ <pre> vs <code> vs <p>

ElementDisplayWhen to use
<pre>Block; preserves whitespaceCode blocks, ASCII art
<code>Inline semantic codeShort snippets in sentences
<p>Block; collapses spacesNormal paragraphs

🧰 Attributes

The <pre> tag has no tag-specific attributes. Combine it with code and global attributes such as class for styling:

attributes.html
<pre>
  <code class="highlight">
    // Your code here
    function exampleFunction() {
      return "Hello, World!";
    }
  </code>
</pre>
class CSS

Apply stylesheet rules to the pre block.

class="code-block"
id Target

Unique identifier for links or scripts.

id="sample-code"
tabindex Focus

Make long code blocks keyboard-focusable when needed.

tabindex="0"

Examples Gallery

Display code blocks and text that must keep every space and line break.

👀 Live Preview

Function example inside pre and code:

function exampleFunction() {
  return "Hello, World!";
}

📚 Common Use Cases

The <pre> tag displays code blocks and fixed-layout text where whitespace and line breaks must be preserved.

Code Blocks

The primary purpose of pre is to display code blocks without losing formatting. It is commonly used when you want to present code examples in a clear and readable manner.

code-blocks.html
<pre>
  <code>
    // Your multiline code here
    function exampleFunction() {
      return "Hello, World!";
    }
  </code>
</pre>
Try It Yourself

Preserving Whitespace

The pre tag preserves both spaces and line breaks, making it ideal for displaying content that relies on specific formatting, such as ASCII art or tables.

preserving-whitespace.html
<pre>
  This      text
  will      be
  displayed exactly
  as       written.
</pre>
Try It Yourself

♿ Accessibility

  • Use pre + code for source — Screen readers announce code semantics correctly.
  • Keep lines readable — Avoid extremely long unbroken lines; enable horizontal scroll with CSS.
  • Provide context — Add a heading or caption describing what the code block demonstrates.
  • Escape HTML entities — Show literal tags using &lt; so content is not parsed.

🧠 How <pre> Works

1

Author writes formatted text

Spaces and line breaks are included inside pre.

Markup
2

Browser applies pre formatting

Default white-space: pre keeps layout intact.

Render
3

CSS styles the block

Classes add backgrounds, fonts, and scroll for long code.

Design
=

Readable formatted output

Code and fixed-layout text display exactly as authors intended.

Browser Support

The <pre> tag is supported in all major browsers, including every version of Internet Explorer.

Baseline · HTML / HTML5

Universal pre support

Every browser preserves whitespace inside <pre> elements.

100% Core tag support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer Fully supported · EOL
Full support
Opera Fully supported
Full support
<pre> tag 100% supported

Bottom line: Use <pre> confidently in any browser for preformatted text.

Conclusion

Mastering the use of the HTML <pre> tag is essential for presenting code blocks and formatted text accurately. By understanding its syntax, leveraging the code element, and adhering to best practices, you can enhance the readability and visual appeal of your content.

💡 Best Practices

✅ Do

  • Use pre when formatting and whitespace matter
  • Combine with code for code block semantics
  • Keep indentation consistent inside blocks
  • Add overflow-x: auto for long lines in CSS

❌ Don’t

  • Use pre for normal paragraphs
  • Put block elements inside pre
  • Forget to escape < when showing HTML source
  • Rely on obsolete plaintext instead of pre

Key Takeaways

Knowledge Unlocked

Six truths every developer should know about <pre>

Bookmark these before you display code on your pages.

6
Core concepts
💻 02

Code blocks

Multiline examples.

Use case
⚙️ 03

pre + code

Semantic pairing.

Pattern
🗃 04

Block-level

Own line + margin.

Layout
🎨 05

CSS styling

class on pre.

Design
🌐 06

100% Support

All browsers.

Compatibility

❓ Frequently Asked Questions

It displays text with preserved spaces and line breaks.
Yes for code blocks: <pre><code>...</code></pre> is the standard pattern.
p collapses extra spaces; pre keeps them exactly.
No. Only global attributes like class and id apply.
Yes. Universal support in every browser.

Display Code Clearly

Practice <pre> with code blocks and whitespace examples in the Try It editor.

Try code block →

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.

6 people found this page helpful