Skip to content
Tutorials Center
My learning

Topics

HTML Reference ยท Text Content

The <pre> element

Reference for the HTML <pre> element: preformatted text that keeps spaces and line breaks, code blocks with <code>, ASCII art, escaping and technical details.

The <pre> element holds preformatted text: browsers keep every space and line break exactly as you typed them and use a monospace font by default. It is the standard container for code listings, command-line output, text diagrams and poetry whose layout matters.

HTML
<pre>
Name    Score
Ana        92
Ben        87
</pre>

Try it

A code block and a text diagram

overflow-x: auto adds a scrollbar for long lines instead of letting them break the layout.

Attributes

This element has no attributes of its own. It accepts the global attributes. The old width attribute is obsolete; use CSS.

Usage notes

  • Code goes in <pre><code>. <pre> preserves the formatting; <code> says the text is computer code. For program output, use <samp> inside <pre>.
  • Escape markup characters. Tags inside <pre> are still parsed, so write &lt; for < and &amp; for & when you show HTML source.
  • First newline is dropped. A single line break right after the <pre> start tag is ignored by the parser, so you can start the content on the next line without adding a blank line.
  • Only phrasing content. Inline elements like <b>, <a> or <span> are allowed (handy for syntax highlighting); block elements like <div> or <p> are not.
  • Long lines. Preformatted text doesn't wrap by default. Use overflow-x: auto, or white-space: pre-wrap if wrapping is acceptable.
  • Accessibility. Screen readers read text art character by character. Give it a text alternative, for example role="img" with an aria-label, or a nearby description.

Technical summary

Content categories Flow content, palpable content
Permitted content Phrasing content
Tag omission None; both the start and end tags are required
Permitted parents Any element that accepts flow content
Implicit ARIA role generic
DOM interface HTMLPreElement
Default display block (with white-space: pre and a monospace font)

Browser support

Supported in every browser.