html
  1. html-pre-tag

HTML <pre> Tag

  • The <pre> tag is an HTML element that defines preformatted text.
  • It is used to represent text that should keep its original formatting such as line-breaks and spaces.
  • Any text within the <pre> tag is displayed in a monospace font and with whitespace preserved.

Syntax

The basic syntax for the <pre> tag is as follows:

<pre>
    your preformatted text goes here
</pre>

Example

Here's an example of how to use the <pre> tag:

<!DOCTYPE html>
<html>
<head>
    <title>Example of Preformatted Text</title>
</head>
<body>
    <h1>Example of Preformatted Text</h1>
    <pre>
    This is some preformatted
    text with   lots of whitespace
    and line-breaks.

    It will be displayed exactly
    as it is written here.
    </pre>
</body>
</html>
Try Playground

The above example will display the text within the <pre> tag exactly as it is written with line-breaks and spaces preserved. The output will look like this:

Explanation

The <pre> tag is used to preserve the original formatting of the text. By default, HTML will ignore line-breaks and extra whitespace in favor of formatting the text in a single line. This can make it difficult to display text in a way that preserves the original formatting and makes it easily readable.

The <pre> tag solves this problem by specifying that its content should be displayed "as-is" with all the spaces, line-breaks, and tabs preserved. This is especially useful when displaying computer code or other text that requires specific formatting.

Use

The <pre> tag is often used when displaying the following types of content:

  • Computer code
  • ASCII art
  • Text that requires specific formatting

It is also often used to display tables and other information that requires whitespace to be preserved in order to be easily read.

Important Points

The following are some important points to note about the <pre> tag:

  • The content within the <pre> tag is displayed in a monospace font.
  • Any text within the <pre> tag is displayed exactly as it is written, with all line-breaks, tabs, and spaces preserved.
  • The <pre> tag should be used only for preformatted text. It is not intended for use in styling text or formatting content.

Summary

The <pre> tag is an HTML element that is used to represent preformatted text that should be displayed exactly as it is written with all whitespaces and linebreaks preserved. It is often used to display computer code or other information that requires specific formatting. By specifying the content within the <pre> tag, you can ensure that it is displayed in a way that is easily readable and preserves its original formatting.

Published on: