HTML pre Tag

Last Updated : 28 May, 2026

The <pre> tag in HTML is used to display preformatted text exactly as written in the source code. It preserves spaces, line breaks, and indentation in the webpage.

  • <pre> tag displays text in a preformatted style.
  • It preserves spaces, tabs, and line breaks.
  • It is commonly used for displaying code or formatted text.
  • Text inside <pre> is usually shown in a monospace font.
HTML
<!DOCTYPE html>

<html>
<body>
<pre>
        This    is      preformatted
        text.
        It preserves spaces and
        line breaks exactly.
</pre>
</body>
</html>
  • <pre> tag preserves the exact formatting, spaces, and line breaks of text.
  • It is useful for displaying code snippets, sample outputs, and formatted text.
  • Tags like <code>, <kbd>, <samp>, and <var> can be used with <pre>.
  • CSS can be applied to customize the appearance of <pre> content.

Note: The pre tag also supports the Event Attributes in HTML and Global Attributes in HTML.

Tag

Description

<samp>

Defines sample output from a computer program

<var>

Defines a variable

<code>

Defines a piece of computer code.

<kbd>

Defines keyboard input

Using CSS with the <pre> Tag

HTML
<!DOCTYPE html>

<html>
<body>
<pre style="font-family: Arial;color: #009900;margin: 25px;">
        This    is      preformatted
        text.
        It preserves spaces and
        line breaks exactly.
</pre>
</body>
</html>
Comment