liveBook · Manning

Pre element

As explained in Web Design Playground, Second Edition

The HTML pre element represents preformatted text whose whitespace and line breaks are preserved.

The HTML pre element displays preformatted text: the browser preserves spaces, indentation, and line breaks instead of collapsing them. It also uses a monospace font, making it especially handy for code and similar text. section 16.1

Use pre when the text’s layout carries useful information:

  • Place code inside pre when indentation and line breaks should remain visible. section 16.1
  • Remember that pre has no semantic purpose in HTML5; pair it with semantic elements such as code when appropriate. section 16.1

PLAY: Preserve a code sample

<pre><code> function helloWorld() { //Greet the reader alert('Hello World!'); } </code></pre>

How Web Design Playground, Second Edition covers it

How deep it goes
Defines whitespace preservation and monospace display, but stops short of deeper rendering behavior.
Examples
Uses a JavaScript helloWorld function with indentation, a comment, and an alert call.
What you can do after
Apply it to preserve formatting in browser-displayed code and other text whose whitespace matters.
How it connects
Connects pre with semantic HTML elements, especially code, for presenting programming code.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.