Table of Contents
INTRODUCTION
The HTML <pre> element is used to display text, exactly as they appear in the HTML file by preserving spaces, line breaks, and indentation. It is often used to display code snippets, ASCII art, or any content where whitespace formatting is important. The text is shown by browser in its default monospace font. It is a block-level element, by default.
Whitespace inside this element is displayed exactly as written, with one exception: if one or more newline characters appear immediately after the opening <pre> tag, the first newline is removed.
The <pre> element often includes <code>, <samp>, and <kbd> elements, which represent computer code, output, and user input, respectively.
Syntax:
<pre> --TEXT-- </pre>
Example:
<pre>
/\
/ \
/____\
/ \
</pre>
Example: Without <pre> tag
/\
/ \
/____\
/ \
Example: With <pre> tag
/\
/ \
/____\
/ \
Best Practises
It's important to give a clear description of any images or diagrams made with preformatted text. This helps people understand what the image or diagram shows, even if they can't see it.
People with low vision who use screen readers may not understand the meaning of the text when it's read out loud in order. A good description will help them know what the image or diagram represents. You can use the <figure> and <figcaption> elements for this purpose.
Tag Omission
The HTML <pre> element must have both start tag and end tag.
ATTRIBUTES
The <pre> element only has global attributes.
Frequently Asked Questions (FAQ)
What is the use of pre tag in html?
The HTML <pre> element is used to display text, as they appear in the HTML file by preserving spaces, line breaks, and indentation.
Why does text inside a <pre> tag display with line breaks in the browser?
What is the difference between the <p> and <pre> tags in HTML?
Purpose: Represents a paragraph of text.
Text Wrapping: Automatically wraps text.
Whitespace Handling: Collapses extra spaces and line breaks.
Preformatted Text Tag
Purpose: Displays preformatted text exactly as typed.
Text Wrapping: Preserves line breaks, spaces, and tabs.
Whitespace Handling: All whitespace is preserved.
How can I enable text wrapping inside a <pre> tag in HTML?
white-space: pre-wrap tells the browser to wrap lines when they reach the container's edge