<pre>: The Preformatted Text element
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.
If you have to display reserved characters such as <
, >
, &
, "
within the <pre>
tag, the characters must be escaped using their respective HTML entity.
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 | No corresponding role |
Permitted ARIA roles | Any |
DOM interface | HTMLPreElement |
Attributes
This element only includes the global attributes.
cols
-
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
-
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
-
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
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
<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>
Examples
Basic example
HTML
<p>Using CSS to change the font color is easy.</p>
<pre>
body {
color: red;
}
</pre>
Result
Escaping reserved characters
HTML
<pre>
let i = 5;
if( i < 10 && i > 0 )
return " Single Digit Number "
</pre>
Result
Specifications
Specification |
---|
HTML Standard # the-pre-element |
Browser compatibility
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
pre | ChromeFull supportYes | EdgeFull support12 | FirefoxFull support1 | Internet ExplorerFull supportYes | OperaFull supportYes | SafariFull supportYes | WebView AndroidFull supportYes | Chrome AndroidFull supportYes | Firefox for AndroidFull support4 | Opera AndroidFull supportYes | Safari on iOSFull supportYes | Samsung InternetFull supportYes |
cols | ChromeNo supportNo | EdgeNo supportNo | FirefoxNo support1 – 29 | Internet ExplorerNo supportNo | OperaNo supportNo | SafariNo supportNo | WebView AndroidNo supportNo | Chrome AndroidNo supportNo | Firefox for AndroidNo support4 – 29 | Opera AndroidNo supportNo | Safari on iOSNo supportNo | Samsung InternetNo supportNo |
width | ChromeFull supportYes | EdgeFull support12 | FirefoxFull support1 | Internet ExplorerFull supportYes | OperaFull supportYes | SafariFull supportYes | WebView AndroidFull supportYes | Chrome AndroidFull supportYes | Firefox for AndroidFull support4 | Opera AndroidFull supportYes | Safari on iOSFull supportYes | Samsung InternetFull supportYes |
wrap | ChromeCompatibility unknown; please update this.? | EdgeCompatibility unknown; please update this.? | FirefoxFull support1 | Internet ExplorerCompatibility unknown; please update this.? | OperaCompatibility unknown; please update this.? | SafariCompatibility unknown; please update this.? | WebView AndroidCompatibility unknown; please update this.? | Chrome AndroidCompatibility unknown; please update this.? | Firefox for AndroidFull support4 | Opera AndroidCompatibility unknown; please update this.? | Safari on iOSCompatibility unknown; please update this.? | Samsung InternetCompatibility unknown; please update this.? |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- Non-standard. Check cross-browser support before using.
- Deprecated. Not for use in new websites.
- See implementation notes.
See also
- CSS:
white-space
,word-break
- HTML Entity
- Related element:
<code>