Web/HTML/Element/pre

From Get docs


The HTML <pre> 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 ("monospace") font. Whitespace inside this element is displayed as written.


The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

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

Specification Status Comment
HTML Living StandardThe definition of '<pre>' in that specification. Living Standard No significant change from HTML5
HTML5The definition of '<pre>' in that specification. Recommendation No significant change from HTML 4.01 Specification
HTML 4.01 SpecificationThe definition of '<pre>' in that specification. Recommendation Deprecated the cols attribute

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
pre Chrome

Full support Yes

Edge

Full support 12

Firefox

Full support 1

IE

Full support Yes

Opera

Full support Yes

Safari

Full support Yes

WebView Android

Full support Yes

Chrome Android

Full support Yes

Firefox Android

Full support 4

Opera Android

Full support Yes

Safari iOS

Full support Yes

Samsung Internet Android

Full support Yes

cols

Deprecated'Non-standard'

Chrome

No support No

Edge

No support No

Firefox

No support 1 — 29

IE

No support No

Opera

No support No

Safari

No support No

WebView Android

No support No

Chrome Android

No support No

Firefox Android

No support 4 — 29

Opera Android

No support No

Safari iOS

No support No

Samsung Internet Android

No support No

width

Deprecated'Non-standard'

Chrome Full support Yes

Notes'

Full support Yes

Notes'

Notes' Specifying the width attribute has no layout effect.

Edge Full support 12

Notes'

Full support 12

Notes'

Notes' Specifying the width attribute has no layout effect.

Firefox Full support 1

Notes'

Full support 1

Notes'

Notes' Since Firefox 29, specifying the width attribute has no layout effect.

IE Full support Yes

Notes'

Full support Yes

Notes'

Notes' Specifying the width attribute has no layout effect.

Opera Full support Yes

Notes'

Full support Yes

Notes'

Notes' Specifying the width attribute has no layout effect.

Safari Full support Yes

Notes'

Full support Yes

Notes'

Notes' Specifying the width attribute has no layout effect.

WebView Android Full support Yes

Notes'

Full support Yes

Notes'

Notes' Specifying the width attribute has no layout effect.

Chrome Android Full support Yes

Notes'

Full support Yes

Notes'

Notes' Specifying the width attribute has no layout effect.

Firefox Android Full support 4

Notes'

Full support 4

Notes'

Notes' Since Firefox 29, specifying the width attribute has no layout effect.

Opera Android Full support Yes

Notes'

Full support Yes

Notes'

Notes' Specifying the width attribute has no layout effect.

Safari iOS Full support Yes

Notes'

Full support Yes

Notes'

Notes' Specifying the width attribute has no layout effect.

Samsung Internet Android Full support Yes

Notes'

Full support Yes

Notes'

Notes' Specifying the width attribute has no layout effect.

wrap

Deprecated'Non-standard'

Chrome

?

Edge

?

Firefox

Full support 1

IE

?

Opera

?

Safari

?

WebView Android

?

Chrome Android

?

Firefox Android

Full support 4

Opera Android

?

Safari iOS

?

Samsung Internet Android

?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
Non-standard. Expect poor cross-browser support.'
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.'
Deprecated. Not for use in new websites.
See implementation notes.'
See implementation notes.


See also