<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.

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 CSS width 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.

Example

HTML

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

Result

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 a combination of an id and the ARIA role and aria-labelledby attributes allow the preformatted text to be announced as an image, with the figcaption serving as the image's alternate description.

Example

<figure role="img" aria-labelledby="cow-caption">
  <pre>
  ___________________________
< 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>

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
pre
Yes
12
1
Yes
Yes
Yes
Yes
Yes
4
Yes
Yes
Yes
cols
No
No
1-29
No
No
No
No
No
4-29
No
No
No
width
Yes
Specifying the width attribute has no layout effect.
12
Specifying the width attribute has no layout effect.
1
Since Firefox 29, specifying the width attribute has no layout effect.
Yes
Specifying the width attribute has no layout effect.
Yes
Specifying the width attribute has no layout effect.
Yes
Specifying the width attribute has no layout effect.
Yes
Specifying the width attribute has no layout effect.
Yes
Specifying the width attribute has no layout effect.
4
Since Firefox 29, specifying the width attribute has no layout effect.
Yes
Specifying the width attribute has no layout effect.
Yes
Specifying the width attribute has no layout effect.
Yes
Specifying the width attribute has no layout effect.
wrap
?
?
1
?
?
?
?
?
4
?
?
?

See also

© 2005–2021 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