View video tutorial

HTML <pre> Tag

HTML

The <pre> tag is used to display pre-formatted text.

HTML <pre> Tag


➔ The <pre> element represents the preformatted text where the text preserves both space and line breaks and the text will appear as written in the source code.

➔ To display special characters inside <pre> element, the characters have to escape using their respective HTML entities.

pre tag syntax

<pre>
  This text is
        preformatted.
  with all random
       white spaces
   and line breaks.
</pre>

Key features of the HTML pre tag.


➔ One behavior of the html p tag is that any multiple spaces are converted to a single space unless entity characters are used for whitespace and line breaks are ignored, while the pre tag preserves whitespace and line breaks as they are.

➔ The pre tag is a block level element which is why by default the pre tag always starts with a new line and occupies the full space available, although this behavior can be customized or changed.

The pre tag example.

Example

<!DOCTYPE html>
<html>
<head>
    <title>HTML pre tag example</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
</head>
<body>
    <h1>HTML pre tag example</h1>
    <pre>
      This text is
            preformatted.
      with all random
           white spaces
       and line breaks.
    </pre>
</body>
</html>
Try it Now »

Copy the code and click on the "Try it Now" button to see how it works.


Common uses of the pre tag.


➔ The pre tag is most useful when the visual layout of the text is difficult to display in its original size and shape.

➔ The pre tag is used to display character-based art or diagrams where character spacing is important for proper display.

➔ To display any raw data, error messages, or output from a system in its original form, including any line breaks, the pre tag works perfectly in this case.

➔ To display random text, such as poetry or songs, preserving all white spaces and line breaks, the pre tag is used.

Supported Global attributes

Global attributes may be applied on all elements, although some elements may have no effect on them.

<accesskey>, <class>, <contenteditable>, <contextmenu>, <data-*>, <dir>, <draggable>, <dropzone>, <hidden>, <id>, <lang>, <spellcheck>, <style>, <tabindex>, <title>, <translate>.

Learning with HTML Editor "Try it Now"

You can edit the HTML code and view the result using online editor.

Example

<pre>
    The pre element represents
       the preformatted text where the text 
         preserves both space and line breaks 
              and the text will 
    appear as written 
         in the source code.
</pre>
Try it Now »

Click on the "Try it Now" button to see how it works.