pre Tag in HTML


The `<pre>` tag is an HTML tag that stands for “preformatted text”. It is used to display text in a fixed-width font and preserve all spaces and line breaks as they appear in the source code. This makes it ideal for displaying code snippets, ASCII art, or any other type of text that requires precise formatting.

Here’s how to use the `<pre>` tag in your HTML code:

1. Open a new HTML file in your favorite text editor.
2. Inside the `<body>` tag, create a `<pre>` tag.
3. Inside the `<pre>` tag, add the text that you want to display.
4. Close the `<pre>` tag.

Here’s an example:

<!DOCTYPE html>
<title>My Preformatted Text Example</title>
This is some preformatted text.
It preserves all spaces and line breaks.
This is ideal for displaying code snippets:
function sayHello() {
alert(‘Hello, world!’);

In this example, we have created a simple HTML file that displays preformatted text. The `<pre>` tag contains some sample text, including a code snippet. Because we have used the `<pre>` tag, all of the spaces and line breaks in the text are preserved, and the code snippet is displayed in a fixed-width font.

Also Read:  s Tag in HTML

Here are some additional tips and best practices for using the `<pre>` tag:

– Use the `<pre>` tag only when you need to preserve exact formatting. If you don’t need to preserve spaces or line breaks, you can use normal text instead.
– Be careful not to include any HTML tags inside the `<pre>` tag. If you need to display HTML code, you can use the `<code>` tag instead.
– You can add CSS styles to the `<pre>` tag to customize its appearance. For example, you might want to change the font family or background color.
– Use the `<pre>` tag sparingly. While it’s useful for displaying code snippets or ASCII art, it can make regular text harder to read.

Leave a Reply