Widely availableUse it when the text layout itself carries meaning and must not collapse into normal paragraph spacing.

Overview

The <pre> element preserves whitespace and line breaks exactly as written. It is often used for code, logs, generated output, and text where formatting is meaningful.

Browser support

Feature Desktop Mobile
Chrome
Edge
Firefox
Safari
Chrome Android
Safari iOS
1
12
1
≤4
18
≤3.2
DOM API

The HTMLPreElement interface exposes specific properties and methods (beyond those of the HTMLElement interface it also has available to it by inheritance) for manipulating a block of preformatted text (pre).

1
12
1
3
18
1
1+Supported (version) Not supported Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)

Syntax

HTML
<pre><code>function hello() {
  console.log("Hello, World!");
}
hello();</code></pre>

Live demo

Format onlytext

Pre element in emptywhite or change row. display.co-dblock to usage.

PreviewFullscreen

rownumber withco-d

CSS counter- in rownumber displaypattern.

PreviewFullscreen

aski-a-t

emptywhite retainaski-a-t display. with pre

PreviewFullscreen

Use cases

  • Code blocks

    Preserve indentation and line breaks for examples, snippets, and terminal-style output.

  • Formatted text output

    Display logs, ASCII layouts, or machine-generated text where spacing must stay intact.

Cautions

  • Long lines can overflow narrow screens, so provide wrapping or horizontal scrolling intentionally.
  • pre preserves all whitespace, which can accidentally include unwanted indentation from source formatting.

Accessibility

  • If code or output is important, pair pre with a descriptive label or heading so users know what the block contains before entering it.

Powered by web-features