<pre> HTML preformatted text element

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

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 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 character references. See escaping ambiguous characters for more information.

<pre> elements commonly contain <code>, <samp>, and <kbd> elements, to represent computer code, computer output, and user input, respectively.

By default, <pre> is a block-level element, i.e., its default display value is block.

Try it

<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;
}

Attributes

This element only includes the global attributes.

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

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

html
<figure>
  <pre role="img" aria-label="ASCII COW">
      ___________________________
  &lt; I'm an expert in my field. &gt;
      ---------------------------
          \   ^__^
           \  (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

html
<p>Using CSS to change the font color is easy.</p>
<pre><code>
body {
  color: red;
}
</code></pre>

Result

Escaping ambiguous characters

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 &amp; 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:

  1. First, write the content out, as you would like it to appear in the HTML document.
  2. Replace any ampersands (&) with &amp;. Do this step first, so that new & characters generated in the next step don't get escaped.
  3. Replace any < characters with &lt;.

This should result in the content being displayed as you intended. The replacement of other HTML syntax characters is optional (like > to &gt;, " to &quot;, and ' to &apos;), but will do no harm.

HTML

html
<pre><code>
let i = 5;

if (i &lt; 10 &amp;&amp; i &gt; 0)
  return &quot;Single Digit Number&quot;
</code></pre>

Result

Technical summary

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

Specification
HTML
# the-pre-element

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
pre
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote Full support
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote Full support
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote Full support
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 4 (Release date: 2009-06-08)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote Full support
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 3.2 (Release date: 2010-04-03)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 3.2 (Release date: 2010-04-03)
footnote Full support
width
Deprecated Non-standard
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote
footnote Specifying the width attribute has no layout effect.
Edge – Full support
Edge 12 (Release date: 2015-07-29)
footnote
footnote Specifying the width attribute has no layout effect.
Firefox – Full support
Firefox 1 (Release date: 2004-11-09)
footnote
footnote Since Firefox 29, specifying the width attribute has no layout effect.
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote
footnote Specifying the width attribute has no layout effect.
Safari – Full support
Safari 3 (Release date: 2007-10-26)
footnote
footnote Specifying the width attribute has no layout effect.
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote
footnote Specifying the width attribute has no layout effect.
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote
footnote Since Firefox for Android 29, specifying the width attribute has no layout effect.
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote
footnote Specifying the width attribute has no layout effect.
Safari on iOS – Full support
Safari on iOS 2 (Release date: 2008-07-11)
footnote
footnote Specifying the width attribute has no layout effect.
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote
footnote Specifying the width attribute has no layout effect.
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote
footnote Specifying the width attribute has no layout effect.
WebView on iOS – Full support
WebView on iOS 2 (Release date: 2008-07-11)
footnote
footnote Specifying the width attribute has no layout effect.

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Non-standard. Check cross-browser support before using.
Deprecated. Not for use in new websites.
See implementation notes.

See also