<pre>: The Preformatted Text element #
::: section-content
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{target="_blank"},
font. Whitespace inside this element is displayed as written.
:::
Try it #
::: section-content ::: iframe ::: {.output-header .border-rounded-top}
HTML Demo: <pre> #
Reset :::
::: {#warning-no-script .warning-container} ::: warning The interactive example cannot be shown because JavaScript is disabled. ::: :::
::: {#warning-mathml-not-supported .warning-container .hidden} ::: warning The interactive example cannot be shown because MathML is not supported by your browser. ::: :::
::: {#editor-container .editor-container .tabbed-standard .hidden .border-rounded-bottom editor-type=“tabbed”} ::: {#tab-container .section .tabs} ::: {#tablist .tab-list role=“tablist”} HTML
CSS
JavaScript :::
::: {#html-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“html” aria-hidden=“true”} ::: {#html-editor}
L TE A A C V R A DOU LOU REUSE QUE TU PORTES ET QUI T' ORNE O CI VILISÉ OTE- TU VEUX LA BIEN SI RESPI RER - Apollinaire::: :::
::: {#css-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“css” aria-hidden=“true”} ::: {#css-editor} pre { font-size: 0.7rem; margin: 0; } ::: :::
::: {#js-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“js” aria-hidden=“true”} ::: {#js-editor} ::: ::: :::
::: {#output .output-container}
Output #
::: :::
::: {.section .console-container .hidden aria-hidden=“true”}
Console Output #
![] clear console
::: {#console .console} ::: :::
::: {#html-output .output .editor-tabbed} %html-content% ::: :::
If you have to display reserved characters such as <
, >
, &
, and
"
within the <pre>
tag, the characters must be escaped using their
respective
HTML
entity.
:::
Attributes #
::: section-content This element only includes the global attributes.
cols
[Non-standard]{.visually-hidden} [Deprecated]{.visually-hidden}Contains the preferred count of characters that a line should have. It was a non-standard synonym of
width
. To achieve such an effect, use CSSwidth
instead.width
[Deprecated]{.visually-hidden} [Non-standard]{.visually-hidden}Contains the preferred count of characters that a line should have. Though technically still implemented, this attribute has no visual effect; to achieve such an effect, use CSS
width
instead.wrap
[Non-standard]{.visually-hidden} [Deprecated]{.visually-hidden}Is a hint indicating how the overflow must happen. In modern browser this hint is ignored and no visual effect results in its present; to achieve such an effect, use CSS
white-space
instead. :::
Accessibility concerns #
::: section-content 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
<figure>
and
<figcaption>
elements, supplemented by the
ARIA
role
and
aria-label
attributes on the pre
element allow the preformatted
ASCII art to
be announced as an image with alternative text, and the figcaption
serving as the image's caption.
:::
Example #
::: section-content ::: code-example [html]{.language-name}
<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>
:::
- MDN Understanding WCAG, Guideline 1.1 explanations
- H86: Providing text alternatives for ASCII art, emoticons, and leetspeak | W3C Techniques for WCAG 2.0{target="_blank"} :::
Examples #
Basic example #
::: section-content
HTML #
::: code-example [html]{.language-name}
<p>Using CSS to change the font color is easy.</p>
<pre>
body {
color: red;
}
</pre>
:::
Result #
::: {#sect1 .code-example} ::: iframe ::: ::: :::
Escaping reserved characters #
::: section-content
HTML #
::: code-example [html]{.language-name}
<pre>
let i = 5;
if (i < 10 && i > 0)
return "Single Digit Number"
</pre>
:::
Result #
::: {#sect2 .code-example} ::: iframe ::: ::: :::
Technical summary #
::: section-content
Content categories | Flow content, palpable content. |
---|---|
Permitted content | Phrasing content. |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | Any element that accepts flow content. |
Implicit ARIA role | generic |
Permitted ARIA roles | Any |
DOM interface | HTMLPreElement |
Specifications #
::: _table #
Specification #
HTML Standard
[#
the-pre-element]{.small}
:::
Browser compatibility #
::: _table #
Desktop Mobile
Chrome Edge Firefox Internet Opera Safari WebView Chrome Firefox for Opera Safari on Samsung
Explorer Android Android Android Android IOS Internet
pre
1 12 1 Yes 15 ≤4 4.4 18 4 14 ≤3.2 1.0
cols
No No 1–29 No No No No No 4–29 No No No
width
1 12 1 Yes 15 3 4.4 18 4 14 2 1.0
Specifying Specifying Since Specifying Specifying Specifying Specifying Specifying Since Specifying Specifying Specifying
the `width` the `width` Firefox 29, the `width` the `width` the `width` the `width` the `width` Firefox 29, the `width` the `width` the `width`
attribute attribute specifying attribute attribute attribute attribute attribute specifying attribute attribute attribute
has no has no the `width` has no has no has no has no has no the `width` has no has no has no
layout layout attribute layout layout layout layout layout attribute layout layout layout
effect. effect. has no effect. effect. effect. effect. effect. has no effect. effect. effect.
layout layout
effect. effect.
wrap
16 79 1 No 15 6 4.4 18 4 14 6 1.0
#
:::
See also #
::: section-content
- CSS:
white-space
,word-break
- HTML Entity
- Related element:
<code>
:::
::: _attribution
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5
or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre{._attribution-link}
:::