HTML preformated text

HTML <PRE> Tag

The HTML <pre> Tag: Preformatted Text

The <pre> tag in HTML stands for "preformatted text." It is used to display text in a way that preserves both whitespace and line breaks exactly as they appear in the HTML source code. This makes the <pre> tag particularly useful when you want to display code, poetry, or any text where precise formatting is important. Unlike other HTML elements that typically collapse extra spaces and line breaks, the <pre> tag maintains the formatting exactly as it is typed in the HTML document.

Basic Structure of the <pre> Tag :-

The <pre> tag is quite simple in its usage. It works by wrapping the content you want to display while keeping the formatting intact.

HTML Code :


In this example, the spaces between words and the line breaks are preserved exactly as they are written within the <pre> tag.

How the <pre> Tag Works :-

By default, HTML collapses multiple spaces into a single space and removes any line breaks in the content. This is a behavior that allows HTML to display content in a more compact form. However, when using the <pre> tag, this behavior is overridden, and the content inside the tag is displayed exactly as it is written in the source code.

HTML Code :

The <p> tag will collapse the extra spaces and treat them as a single space, while the <pre> tag will preserve the spaces, line breaks, and formatting, displaying them exactly as typed.


Common Use Cases for the <pre> Tag

Displaying Code

One of the most common uses of the <pre> tag is for displaying computer code, such as HTML, JavaScript, or CSS. This is because the precise formatting, including indentation and line breaks, is essential to code readability. The <pre> tag ensures that the code is displayed in the browser exactly as it appears in the source file.

HTML Code :

example, the HTML entities (< and >) are used to prevent the browser from interpreting the tags, allowing the raw HTML code to be displayed as text.
         

Showing Preformatted Text in Documents :-

Another common use for the <pre> tag is to display text that relies on specific formatting, such as poetry, tabular data, or ASCII art. Since the <pre> tag respects both spaces and line breaks, it makes it possible to display these kinds of content in a way that is faithful to their original structure.


HTML and CSS Code :


Here, the line breaks and spaces between words are critical for the structure of the poem, and using the <pre> tag ensures that the text will appear exactly as intended.

Here, the line breaks and spaces between words are critical for the structure of the poem, and using the
 tag ensures that the text will appear exactly as intended :- 

ASCII art is another example of content that requires careful spacing and line breaks. The <pregt; tag can be used to display intricate designs that would otherwise lose their shape if HTML’s default behavior were applied.

HTML Code :


In this case, the <pre> tag preserves the alignment of the characters, allowing the ASCII art to appear as intended on the page.





Styling the <pre> Tag:-

Though the <pre> tag preserves whitespace and formatting, you can also style it with CSS. By default, text within a <pre> tag is displayed using a monospace font (like Courier), which ensures that all characters have the same width, making it easier to align text. However, you can modify this behavior by applying custom styles.

HTML Code :


In this example, CSS is used to change the background color, padding, and border of the <pre> tag, while keeping the original text formatting intact.

Limitations and Considerations:-

While the <pre> tag is powerful and useful, there are a few considerations and limitations to keep in mind:

Text Overflow :-

If the content inside a <pre> tag is too wide for the container or screen, it will overflow and can cause horizontal scrolling. This might not always be desirable, especially on smaller devices.

CSS Code :

The white-space: pre-wrap property ensures that the content wraps properly while preserving spaces and line breaks. The word-wrap: break-word ensures that long words will break to the next line if necessary.



CSS Code :





OUTPUT :

Comments