Block-level
HTML 2.0
The <pre> element represents preformatted text where whitespace, line breaks, and formatting are preserved exactly as written in the HTML. It’s commonly used for code blocks, ASCII art, and text that requires specific formatting.
Interactive code playground requires JavaScript. Here's the code:
<pre>
function greet(name) {
console.log("Hello, " + name);
}
</pre>
Text with preserved spacing
All whitespace inside <pre> is preserved, including spaces, tabs, and newlines.
The <pre> element supports all global attributes like id, class, style, and lang.
< pre id = " code-sample " class = " language-javascript " >
Interactive code playground requires JavaScript. Here's the code:
<pre>
function calculateTotal(price, quantity) {
const subtotal = price * quantity;
const tax = subtotal * 0.08;
return subtotal + tax;
}
</pre>
Combine <pre> with <code> for semantic code blocks:
Interactive code playground requires JavaScript. Here's the code:
<pre><code>const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(n => n * 2);
console.log(doubled); // [2, 4, 6, 8, 10]</code></pre>
Interactive code playground requires JavaScript. Here's the code:
<h3>HTML</h3>
<pre><code><div class="container">
<h1>Hello World</h1>
</div></code></pre>
<h3>CSS</h3>
<pre><code>.container {
max-width: 1200px;
margin: 0 auto;
}</code></pre>
Interactive code playground requires JavaScript. Here's the code:
<pre>
/\_/\
( o.o )
> ^ <
/| |\
(_| |_)
</pre>
Interactive code playground requires JavaScript. Here's the code:
<pre>
Invoice #12345
--------------
Item Qty Price
Widget A 5 $10.00
Widget B 3 $15.00
--------------
Total $95.00
</pre>
Interactive code playground requires JavaScript. Here's the code:
<pre>
$ npm install react
+ react@18.2.0
+ react-dom@18.2.0
added 4 packages in 2.156s
</pre>
Interactive code playground requires JavaScript. Here's the code:
<style>
pre {
background-color: #1e293b;
color: #e2e8f0;
padding: 1rem;
border-radius: 6px;
overflow-x: auto;
font-family: 'Courier New', monospace;
font-size: 0.9rem;
line-height: 1.5;
}
</style>
<pre>function hello() {
return "Hello, World!";
}</pre>
Interactive code playground requires JavaScript. Here's the code:
<style>
.line-numbers {
background: #f8f9fa;
border-left: 3px solid #3b82f6;
padding: 1rem;
counter-reset: line;
}
.line-numbers code {
counter-increment: line;
}
.line-numbers code::before {
content: counter(line);
display: inline-block;
width: 2em;
margin-right: 1em;
color: #9ca3af;
text-align: right;
}
</style>
<pre class="line-numbers"><code>function add(a, b) {
return a + b;
}
console.log(add(2, 3));</code></pre>
Interactive code playground requires JavaScript. Here's the code:
<style>
.scrollable-code {
max-height: 150px;
overflow-y: auto;
background: #f3f4f6;
padding: 1rem;
border: 1px solid #d1d5db;
border-radius: 4px;
}
</style>
<pre class="scrollable-code"><code>// This is a long code block
function processData(data) {
const cleaned = data.filter(item => item.valid);
const transformed = cleaned.map(item => ({
...item,
processed: true
}));
return transformed.sort((a, b) => a.id - b.id);
}
// More code...
const result = processData(rawData);
console.log(result);</code></pre>
Interactive code playground requires JavaScript. Here's the code:
<style>
.highlight .keyword { color: #d73a49; font-weight: bold; }
.highlight .string { color: #032f62; }
.highlight .function { color: #6f42c1; }
.highlight .comment { color: #6a737d; font-style: italic; }
</style>
<pre class="highlight"><code><span class="keyword">function</span> <span class="function">greet</span>(name) {
<span class="comment">// Display greeting</span>
<span class="keyword">return</span> <span class="string">"Hello, "</span> + name;
}</code></pre>
When displaying HTML code, escape special characters:
Interactive code playground requires JavaScript. Here's the code:
<pre><code><div class="example">
<p>This is HTML code</p>
</div></code></pre>
Interactive code playground requires JavaScript. Here's the code:
<figure>
<figcaption>example.js</figcaption>
<pre><code>const greeting = "Hello, World!";
console.log(greeting);</code></pre>
</figure>
Interactive code playground requires JavaScript. Here's the code:
<h3>Before</h3>
<pre><code>var x = 10;
var y = 20;</code></pre>
<h3>After</h3>
<pre><code>const x = 10;
const y = 20;</code></pre>
Interactive code playground requires JavaScript. Here's the code:
<style>
.terminal {
background: #1a1a1a;
color: #00ff00;
padding: 1rem;
border-radius: 4px;
font-family: 'Courier New', monospace;
}
.terminal::before {
content: '$ ';
color: #fff;
}
</style>
<pre class="terminal">npm run build
Building for production...
✓ Build complete in 3.2s</pre>
Combining <pre> and <code> provides better semantic meaning:
Help assistive technology by specifying the programming language:
< pre >< code class = " language-javascript " >
< pre >< code class = " language-python " >
Ensure code blocks can scroll horizontally:
Interactive code playground requires JavaScript. Here's the code:
<style>
pre {
overflow-x: auto;
max-width: 100%;
}
</style>
<pre><code>const veryLongVariableName = "This is a very long line of code that will need horizontal scrolling on narrow screens";</code></pre>
Keep code readable:
< pre style = " font-size: 0.9rem; " >
< pre style = " font-size: 0.5rem; " >
< p > This is regular text that wraps normally. </ p >
< pre > This is regular text that should wrap. </ pre >
Indentation in HTML source affects display:
This text is not indented
Interactive code playground requires JavaScript. Here's the code:
<!-- Each line has different spacing -->
<pre>No spaces
Two spaces
Four spaces
Six spaces</pre>
Browser Version Notes Chrome 1+ Full support Firefox 1+ Full support Safari 1+ Full support Edge 12+ Full support IE 3+ Full support
The <pre> element has been supported since early HTML versions. Modern browsers support overflow-x: auto for horizontal scrolling.