HTML Tags

HTML <pre> Tag

The <pre> tag in HTML defines preformatted text — meaning the text inside it is displayed exactly as written in the HTML file, including spaces, tabs, and line breaks.
It is often used to display code snippets, poems, or any text where spacing and alignment matter.

Syntax

<pre>Preformatted text goes here</pre>

Attributes

AttributeDescription
classAssigns one or more class names for styling.
idAssigns a unique identifier to the preformatted block.
styleAdds inline CSS styles to customize appearance.
titleProvides additional information as a tooltip.

Example

<!DOCTYPE html>
<html>
<head>
  <title>PRE Tag Example</title>
  <style>
    .code-sample {
      background-color: #f4f4f4;
      border: 1px solid #ccc;
      padding: 10px;
      font-family: "Courier New", monospace;
      color: #333;
      border-radius: 6px;
    }
  </style>
</head>
<body>
  <h1>Preformatted Text Example</h1>
  <p>The &lt;pre&gt; tag preserves spacing and line breaks exactly as they appear in the source code.</p>

  <pre class="code-sample" id="example1" title="Sample Code">
function greetUser() {
    console.log("Hello, World!");
}
  </pre>

  <p>Notice how indentation and line breaks are preserved inside the &lt;pre&gt; tag.</p>
</body>
</html>

Output

Browser Output

Use our TryIt Editor to see the output.

Browser Support

Chrome
Chrome
Firefox
Firefox
Edge
Edge
Safari
Safari
Opera
Opera
IE
IE9+
✅Yes✅Yes✅Yes✅Yes✅Yes✅Yes

The <pre> tag displays text in a fixed-width font, maintaining spaces and line breaks exactly as written.
This makes it ideal for displaying source code or formatted text blocks.

Notes

  • The <pre> tag maintains white spaces, line breaks, and indentation.
  • Text inside <pre> is typically shown in a monospaced font (like Courier).
  • You can combine <pre> with <code> to represent formatted code blocks semantically.
  • Avoid excessive use for regular text — it’s meant for preformatted or technical content.

Conclusion

The <pre> tag is essential for displaying preformatted or code-style text where spacing and alignment are crucial.
It ensures that text appears exactly as written in the HTML, making it perfect for code snippets, ASCII art, or structured data displays.

Sign In Form

User your email and password to singn in

Provide your email and we will send you a password reset link.

Don’t have an account, signup here : 

HTML5 & CSS3

Tools and Tutorials

Want a Website

Want a website for your business ?

Please fill out the details below, so we can reach out to you.

Registration Form

Signup to track your record and much more.

We have sent you an email with a registration link. Please click the link to verify your email address, or enter the 6-digit OTP provided in the email.

Didn't receive the OTP. Regenerate OTP Resend