👀 Live Preview
Function example inside pre and code:
function exampleFunction() {
return "Hello, World!";
}
The <pre> tag preserves spaces and line breaks for code, poetry, and fixed-layout text. This guide covers syntax, pairing with code, common use cases, and best practices for beginners.
Keep exact spacing.
Multiline examples.
Semantic pairing.
ASCII art, tables.
Classes on pre.
Readable code display.
<pre> Tag?The <pre> tag, short for preformatted text, is an HTML element designed to preserve both spaces and line breaks. It is particularly useful when you want to display code blocks or any text where formatting and spacing are crucial.
Browsers render <pre> with white-space: pre by default, so indentation and line breaks in your source appear exactly on the page.
It replaced the obsolete <plaintext> and <listing> tags as the standard way to show unmodified text layout in modern HTML.
Wrap your code or text within opening <pre> and closing </pre> tags:
<pre>
Your Code or Text Here
</pre>pre for block content that needs preserved whitespace.code inside pre when displaying programmatic source code.< and & when showing raw HTML in a pre block.| Topic | Code Snippet | Notes |
|---|---|---|
| Basic pre | <pre>Text</pre> | Preserves spaces |
| Code block | <pre><code>...</code></pre> | Best practice |
| Styling | class="code-block" | Global attribute |
| Display | Block-level | Monospace font default |
| vs p | pre keeps spaces | p collapses whitespace |
| Browser support | Universal | All browsers |
<pre> vs <code> vs <p>| Element | Display | When to use |
|---|---|---|
<pre> | Block; preserves whitespace | Code blocks, ASCII art |
<code> | Inline semantic code | Short snippets in sentences |
<p> | Block; collapses spaces | Normal paragraphs |
The <pre> tag has no tag-specific attributes. Combine it with code and global attributes such as class for styling:
<pre>
<code class="highlight">
// Your code here
function exampleFunction() {
return "Hello, World!";
}
</code>
</pre>class CSSApply stylesheet rules to the pre block.
class="code-block"id TargetUnique identifier for links or scripts.
id="sample-code"tabindex FocusMake long code blocks keyboard-focusable when needed.
tabindex="0"Display code blocks and text that must keep every space and line break.
Function example inside pre and code:
function exampleFunction() {
return "Hello, World!";
}The <pre> tag displays code blocks and fixed-layout text where whitespace and line breaks must be preserved.
The primary purpose of pre is to display code blocks without losing formatting. It is commonly used when you want to present code examples in a clear and readable manner.
<pre>
<code>
// Your multiline code here
function exampleFunction() {
return "Hello, World!";
}
</code>
</pre>The pre tag preserves both spaces and line breaks, making it ideal for displaying content that relies on specific formatting, such as ASCII art or tables.
<pre>
This text
will be
displayed exactly
as written.
</pre>< so content is not parsed.Spaces and line breaks are included inside pre.
Default white-space: pre keeps layout intact.
Classes add backgrounds, fonts, and scroll for long code.
Code and fixed-layout text display exactly as authors intended.
The <pre> tag is supported in all major browsers, including every version of Internet Explorer.
Every browser preserves whitespace inside <pre> elements.
Bottom line: Use <pre> confidently in any browser for preformatted text.
Mastering the use of the HTML <pre> tag is essential for presenting code blocks and formatted text accurately. By understanding its syntax, leveraging the code element, and adhering to best practices, you can enhance the readability and visual appeal of your content.
pre when formatting and whitespace mattercode for code block semanticsoverflow-x: auto for long lines in CSSpre for normal paragraphspre< when showing HTML sourceplaintext instead of pre<pre>Bookmark these before you display code on your pages.
Spaces stay intact.
BehaviorMultiline examples.
Use caseSemantic pairing.
PatternOwn line + margin.
Layoutclass on pre.
DesignAll browsers.
Compatibility<pre><code>...</code></pre> is the standard pattern.p collapses extra spaces; pre keeps them exactly.class and id apply.Practice <pre> with code blocks and whitespace examples in the Try It editor.
6 people found this page helpful