HTML - <pre> Tag



The HTML <pre> tag is used to define a block of preformatted text that is to be presented exactly as written in the HTML file. Whitespace inside this tag is displayed as written, which means this <pre> tag preserves the text spaces, line breaks, tabs, and other formatting characters that are ignored by the browser.

If you have to display reserved characters within the <pre> tag, such as <, >, &, and ", the characters must be escape using their appropriate HTML entities (An HTML entity is a piece of text called a "string" that begins with an ampersand (&) and ends with a semicolon (;)).

The text inside the <pre> element is displayed in a fixed-width font; however, it can be changed with CSS.

Syntax

Following is the syntax of <pre> tag −

<pre>.....</pre>

Example

In the following example, we are creating an HTML document and demonstrating the usage of the <pre> tag as follows:

<!DOCTYPE html>
<html>
   <body>
      <h2>Example of pre tag </h2>
      <pre>
         Hi, I am tutorialspoint,
         I make the things easy for the users,
         Users can learn here in the easy way.
      </pre>
   </body>
</html>

On running the above code, the output window will pop up displaying the text used with pre tag on the webpage.

Example

Considering the another scenario, where we are going to use the pre tag.

<!DOCTYPE html>
<html>
   <body>
      <figure>
         <pre role="img" aria-label="ASCII COW">
               ___________________________
            < I'm an expert in my field. >
               ---------------------------
                  \   ^__^
                     \  (oo)\_______
                        (__)\       )\/\
                           ||----w |
                           ||     ||
            
			</pre>
         <figcaption id="cow-caption"> A cow saying, "I'm an expert in my field." The cow is illustrated using preformatted text characters. </figcaption>
      </figure>
   </body>
</html>

when we run the above code, it will generate an output consisting of the data that is used in the pre tag displayed on the webpage.

Example

Let's look at the following example, where we are creating an HTML document and using the <pre> tag to write a C program, as follows −

<!DOCTYPE html>
<html>
<body>
   <pre >
      #include <stdio.h>
      int main() {    
         int number1, number2, sum;   
         printf("Enter two integers: ");
         scanf("%d %d", &number1, &number2);
      
         // calculate the sum
         sum = number1 + number2;    
         printf("%d + %d = %d", number1, number2, sum);
         return 0;
      }
   </pre>
</body>
</html>

On running the above code, it will generate an output consisting of the text displaying the addition of two numbers in c program.

html_tags_reference.htm
Advertisements