Pre Tag
The `<pre>` tag in HTML is used to define preformatted text, which preserves the original formatting of the text. Learn how to use the `<pre>` tag to display code snippets and other preformatted text in HTML.
What Does the pre Tag Do
The <pre> tag preserves the original formatting of text, making it an excellent choice for displaying code where spacing and indentation are key.
Syntax of the pre Tag
The syntax of the <pre> tag looks like this:
<pre>
<!-- code snippet in any programming language -->
</pre>Key Features
- The
<pre>tag maintains both spaces and line breaks, ensuring that text appears exactly as it was originally formatted. - The
<pre>tag has both an opening tag<pre>and a closing tag</pre>. - Additional attributes can also be added for further customization.
When to Use the pre Tag
The <pre> tag is most effective when you want the text to display on your HTML page exactly as it was typed, without any formatting changes. It is especially useful for displaying code snippets or preformatted text from data files.
Example: Using the pre Tag
Here is an example of how the <pre> tag can be used to display a code snippet on an HTML page:
Program
<pre>
{
// A simple JavaScript program to print "Hello, World!"
function greet() {
console.log("Hello, World!");
}
greet();
}
</pre>Output
{
// A simple JavaScript program to print "Hello, World!"
function greet() {
console.log("Hello, World!");
}
greet();
}Conclusion
In summary, the <pre> tag is a versatile tool for preserving the original formatting of text in HTML. Whether you are displaying code snippets or preformatted text, this tag ensures that your content appears exactly as intended.
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on
Paragraph Tag
HTML paragraph tag are used to define paragraphs of text on a web page. Learn how to use the `<p>` tag to create paragraphs in HTML.
Skeletal Tags
HTML skeletal tags are used to structure the content of a web page. They include the `<html>`, `<head>`, `<title>`, and `<body>` tags. Learn how to use these tags to create a well-structured HTML document.