files/en-us/web/html/reference/elements/pre/index.md
The <pre> HTML element represents preformatted text which is to be presented exactly as written in the HTML file. The text is typically rendered using a non-proportional, or monospaced font.
Whitespace inside this element is displayed as written, with one exception. If one or more leading newline characters are included immediately following the opening <pre> tag, the first newline character is stripped. This transformation is done by the HTML parser and does not apply when using {{Glossary("XHTML")}}.
<pre> elements' text content is parsed as HTML, so if you want to ensure that your text content stays as plain text, some syntax characters, such as <, may need to be escaped using their respective {{glossary("character reference", "character references")}}. See escaping ambiguous characters for more information.
<pre> elements commonly contain {{HTMLElement("code")}}, {{HTMLElement("samp")}}, and {{HTMLElement("kbd")}} elements, to represent computer code, computer output, and user input, respectively.
By default, <pre> is a block-level element, i.e., its default {{cssxref("display")}} value is block.
{{InteractiveExample("HTML Demo: <pre>", "tabbed-standard")}}
<pre>
S
A
LUT
M
O N
D E
DONT
JE SUIS
LA LAN
G U E É
L O Q U E N
TE QUESA
B O U C H E
O P A R I S
T I R E ET TIRERA
T O U JOURS
AUX A L
LEM ANDS - Apollinaire
</pre>
pre {
font-size: 0.7rem;
margin: 0;
}
This element only includes the global attributes.
width {{deprecated_inline}} {{Non-standard_Inline}}
wrap {{non-standard_inline}} {{Deprecated_Inline}}
It is important to provide an alternate description for any images or diagrams created using preformatted text. The alternate description should clearly and concisely describe the image or diagram's content.
People experiencing low vision conditions and browsing with the aid of assistive technology such as a screen reader may not understand what the preformatted text characters are representing when they are read out in sequence.
A combination of the {{HTMLElement("figure")}} and {{HTMLElement("figcaption")}} elements, supplemented by the ARIA role and aria-label attributes on the pre element allow the preformatted {{Glossary("ASCII")}} art to be announced as an image with alternative text, and the figcaption serving as the image's caption.
<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>
<p>Using CSS to change the font color is easy.</p>
<pre><code>
body {
color: red;
}
</code></pre>
{{EmbedLiveSample("Basic_example")}}
Suppose you want to demonstrate HTML code in a <pre> element. The character sequences that define valid HTML tags (starting with < and ending with >) will not be displayed. To display the tag characters as text, you need to escape (at least) the < character using its character reference, so that the sequences no longer define valid tags.
In reality, the HTML parser treats most characters as plain text unless in specific contexts. For example, < code is fine, but <code would be misparsed; &am; is fine, but & is not. However, it's a good practice to escape all ambiguous characters to avoid any confusion, especially if you are programmatically generating HTML and injecting the <pre> content. In this case, here's a good rule of thumb for how to escape characters:
&) with &. Do this step first, so that new & characters generated in the next step don't get escaped.< characters with <.This should result in the content being displayed as you intended. The replacement of other HTML syntax characters is optional (like > to >, " to ", and ' to '), but will do no harm.
<pre><code>
let i = 5;
if (i < 10 && i > 0)
return "Single Digit Number"
</code></pre>
{{EmbedLiveSample("Escaping_reserved_characters")}}
{{Specifications}}
{{Compat}}