The HTML <pre> element displays preformatted text. Spaces, tabs and line breaks in the source are preserved instead of being collapsed like ordinary HTML text.
<pre>
Welcome to plus2net.
PHP Server-side scripting
HTML Web page markup
JavaScript Client-side scripting
</pre>
The browser displays the text using a monospace font by default and preserves its whitespace formatting.
<pre> does not make HTML special characters safe to type literally. To display markup such as <div> as text, escape the angle brackets as < and >.In normal HTML text, sequences of spaces, tabs and line breaks are usually collapsed into a single displayed space.
<p>
PHP Server-side scripting
HTML Web page markup
</p>
The indentation visible in the source is not normally preserved in the rendered paragraph. Use <pre> when the whitespace itself is meaningful.
<pre>
Some preformatted text
</pre>
The content is rendered using CSS behavior equivalent to preserved whitespace. The browser's default stylesheet normally gives <pre> a monospace font.
Here is a source example with aligned columns:
<pre>
Welcome to plus2net, you can learn following scripts here.
PHP Server-side scripting language
ASP Server-side scripting language
HTML Tags used in a web browser
JavaScript Client-side scripting language
</pre>
Rendered output:
Welcome to plus2net, you can learn following scripts here. PHP Server-side scripting language ASP Server-side scripting language HTML Tags used in a web browser JavaScript Client-side scripting language
The spaces and line breaks are retained, making <pre> useful when alignment is part of the content.
If you want to display HTML markup as text, escape the characters that would otherwise be interpreted as markup.
Source:
<pre><code>
<h2>Hello</h2>
</code></pre>
Displayed code:
<h2>Hello</h2>
<pre> element preserves whitespace; character escaping is a separate HTML requirement. See displaying HTML tags as text.Use <code> for computer code and place it inside <pre> when the code contains meaningful indentation or multiple lines.
<pre><code>
SELECT *
FROM users
WHERE active = 1;
</code></pre>
For a short inline fragment such as SELECT *, <code> alone is usually enough. For multi-line code, <pre><code>...</code></pre> is the usual semantic combination.
Use CSS for background, border, padding, width and scrolling.
<style>
pre.styled-pre {
background-color: #f5f5f5;
border-left: 3px solid #ccc;
padding: 10px;
overflow-x: auto;
}
</style>
<pre class="styled-pre">
function helloWorld() {
console.log("Hello, world!");
}
</pre>
For a tutorial site, horizontal scrolling is often preferable for source code because forced wrapping can make long code lines harder to copy and read.
By default, preformatted text preserves line structure and can overflow horizontally. One option is to allow scrolling:
<style>
pre {
overflow-x: auto;
}
</style>
If the content should wrap instead, use white-space: pre-wrap:
<style>
pre.wrap {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
</style>
<pre class="wrap">
This is a long line that can wrap while still preserving other whitespace.
</pre>
pre-wrap may improve small-screen readability.HTML has a small special rule for <pre>: a line break immediately after the opening tag is removed from the rendered text.
<pre>
First visible line
Second visible line
</pre>
The first visible line does not begin with an extra blank line merely because the source starts the content on the next line.
Do not use spaces inside <pre> as a substitute for a semantic table, list or layout structure. Preformatted text preserves appearance, but it does not describe relationships between pieces of data.
For ASCII art or text diagrams, provide an accessible description when the visual arrangement conveys information.
<pre role="img" aria-label="A simple smiley face made with ASCII characters">
:-)
</pre>
For structured tabular data, use an HTML table rather than arranging columns with spaces.
The old width attribute on <pre> is obsolete. Use CSS instead.
<style>
pre.example {
max-width: 80ch;
}
</style>
The ch CSS unit is based approximately on the width of the 0 character in the current font. It can be useful for text-oriented line lengths, but it is not an exact "number of characters per line" guarantee.
Do not use <pre> merely to create visual spacing. Use CSS for page layout.
<pre> preserves whitespace but does not disable HTML parsing. Escape markup characters when displaying source code.
The element already preserves source line breaks, so extra <br> elements are usually unnecessary.
Use tables, lists and headings when the content has those meanings. Spaces alone do not create semantic relationships.
Wrapping may make source code harder to follow. Horizontal scrolling is often better for code, while wrapping can work well for long prose or logs.
Control width and overflow with CSS.
It displays preformatted text while preserving spaces, tabs and line breaks from the source.
No. HTML is still parsed inside pre. Escape angle brackets when you want markup to appear as text.
Pre preserves whitespace and line structure. Code identifies computer code semantically. Multi-line code is commonly placed inside pre and code together.
Use CSS such as overflow-x:auto for horizontal scrolling, or white-space:pre-wrap when wrapping is appropriate.
No. The old width attribute is obsolete. Use CSS for width and layout.
Not when the content is genuinely tabular. Use an HTML table so the relationships between rows and columns are represented semantically.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.