Question
I use <pre> tags for code blocks in HTML and for debugging output while writing scripts. By default, long lines stay on a single line and can overflow horizontally. How can I make the text wrap inside a <pre> element while still preserving its whitespace behavior?
Short Answer
By the end of this page, you will understand why <pre> elements do not wrap text by default, how CSS controls that behavior, and how to make long lines wrap while keeping the formatting that makes <pre> useful for code and debug output.
Concept
The <pre> element in HTML is designed to preserve whitespace exactly as written. That means:
- Spaces are kept
- Line breaks are kept
- Text usually does not wrap automatically
This is useful for:
- Code samples
- Logs
- Debug output
- ASCII art
- Any text where spacing matters
The reason long lines do not wrap is the default browser styling for <pre>, which is usually similar to:
pre {
white-space: pre;
}
The white-space property controls how spaces, tabs, newlines, and wrapping behave.
If you want a <pre> element to keep whitespace but also allow wrapping, you can change its CSS to use values like:
white-space: pre-wrap;
This is the key idea:
pre= preserve whitespace, no wrappingpre-wrap= preserve whitespace, allow wrapping
This matters in real programming because you often want output to stay readable on small screens or narrow containers. For example, a log viewer, debug panel, or documentation page may contain long lines that would otherwise force horizontal scrolling.
Mental Model
Think of a <pre> tag like writing on graph paper.
- With normal text, the browser rearranges words to fit the page neatly.
- With
<pre>, the browser tries to keep every space and line exactly where you put it. - Using
white-space: pre-wrapis like telling the browser: "Keep my spacing, but if the line gets too long, fold it onto the next line so it still fits."
So you are not turning <pre> into normal text. You are keeping its formatting while allowing long lines to bend instead of overflowing.
Syntax and Examples
The most common solution is to apply CSS to the <pre> element.
<pre class="wrapped-output">This is a very long line of debug output that would normally continue forever on one line, but now it can wrap inside the container.</pre>
.wrapped-output {
white-space: pre-wrap;
}
What this does
- Preserves spaces and line breaks
- Allows long lines to wrap
Example with a fixed-width container
<pre class="wrapped-output">
Name: Example User
Message: This is a very long debug message that should wrap instead of causing horizontal scrolling in the browser.
</pre>
.wrapped-output {
width: 300px;
padding: 12px;
border: 1px solid #ccc;
background: #f7f7f7;
white-space: pre-wrap;
}
If a line contains a very long word or unbroken string, you may also need:
.wrapped-output {
white-space: pre-wrap;
overflow-wrap: break-word;
}
This helps when the content has things like:
- long URLs
- stack traces
- generated tokens
- long file paths
Full example
<!DOCTYPE html>
<html>
<head>
<style>
pre {
width: 320px;
padding: 12px;
border: 1px solid #999;
background-color: #f4f4f4;
white-space: pre-wrap;
overflow-wrap: break-word;
}
</style>
</head>
<body>
<pre>
User: Alex
Status: Success
Debug: This is a very long line of output that would normally overflow, but with pre-wrap it stays readable inside the box.
</pre>
</body>
</html>
In this example, the indentation and line breaks remain, but long lines can wrap.
Step by Step Execution
Consider this HTML and CSS:
<pre class="log">Error: Something went wrong while processing a very long request that contains lots of information and needs to stay readable.</pre>
.log {
width: 250px;
white-space: pre-wrap;
overflow-wrap: break-word;
border: 1px solid #000;
}
Step by step
- The browser reads the
<pre>element. - A
<pre>normally preserves spaces and line breaks. - The CSS
white-space: pre-wrapchanges wrapping behavior. - The browser still preserves formatting, but now long lines may move onto the next line.
- The container width is
250px, so the text cannot continue forever on one line. - If a word or string is too long to fit,
overflow-wrap: break-wordallows it to break and stay inside the box. - The result is readable formatted text without horizontal overflow.
Without pre-wrap
The same content would try to stay on one long line, often causing:
- horizontal scrolling
- layout overflow
- hard-to-read output
With pre-wrap
You get:
- preserved formatting
- wrapped lines
- better readability in smaller containers
Real World Use Cases
Here are common situations where wrapped <pre> text is useful:
- Code documentation: Show code samples in narrow content areas without forcing horizontal scrolling.
- Debug panels: Display logs or API responses in admin tools.
- Error reporting: Show stack traces or server output in a readable box.
- Mobile layouts: Keep formatted text readable on small screens.
- Terminal-style UI: Preserve spacing while preventing the interface from breaking.
- JSON or plain-text responses: Display structured text in a way users can read more easily.
Example: an error viewer in a web app might use:
<pre class="error-output"></pre>
.error-output {
white-space: pre-wrap;
overflow-wrap: break-word;
max-width: 100%;
}
This keeps logs visible even when messages are long.
Real Codebase Usage
In real projects, developers often combine <pre> wrapping with layout and safety rules.
Common patterns
-
Log and debug containers
- Use
white-space: pre-wrapfor readable logs - Add
max-heightandoverflow: autofor scrollable panels
- Use
-
Responsive documentation pages
- Keep code blocks readable on tablets and phones
- Sometimes allow horizontal scroll for real code samples, but wrap debug text
-
API response viewers
- Show formatted server output in a
<pre>block - Use wrapping for long messages or values
- Show formatted server output in a
Example pattern
.debug-box {
white-space: pre-wrap;
overflow-wrap: break-word;
max-width: 100%;
padding: 1rem;
background: #111;
color: #eee;
border-radius: 6px;
}
Practical decision developers make
Not every <pre> should wrap.
- For source code, horizontal scrolling is sometimes preferred because line structure matters exactly.
- For logs, error text, and debug output, wrapping is often better because readability matters more.
A common approach is to use separate classes:
pre.code-sample {
white-space: pre;
overflow-x: auto;
}
pre.debug-output {
white-space: pre-wrap;
overflow-wrap: break-word;
}
This lets each type of content behave appropriately.
Common Mistakes
1. Using <pre> and expecting normal wrapping
Broken expectation:
<pre>This long line will not automatically wrap the way a normal paragraph does.</pre>
Why it happens:
<pre>preserves formatting- Default behavior usually prevents wrapping
Fix:
pre {
white-space: pre-wrap;
}
2. Using word-wrap or overflow-wrap without changing white-space
Broken example:
pre {
overflow-wrap: break-word;
}
Why this may not be enough:
- If
white-spaceis stillpre, the line may still behave like a non-wrapping line
Better:
pre {
white-space: pre-wrap;
overflow-wrap: break-word;
}
3. Applying the style to the wrong element
Broken example:
<div class="wrapped">
<pre>Long output here...</pre>
</div>
.wrapped {
white-space: pre-wrap;
}
Why this can fail:
- The
<pre>itself still has its own whitespace behavior
Fix:
pre {
white-space: pre-wrap;
}
or
.wrapped pre {
white-space: pre-wrap;
}
4. Wrapping code when horizontal scrolling would be better
For actual code samples, wrapping can make lines harder to read because indentation and structure may become visually confusing.
Better choice for code samples:
pre.code {
white-space: pre;
overflow-x: auto;
}
5. Forgetting about very long unbroken strings
Example problem:
<pre class="output">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</pre>
If there are no spaces, pre-wrap alone may not be enough.
Fix:
.output {
white-space: pre-wrap;
overflow-wrap: break-word;
}
Comparisons
| Approach | Preserves spaces and line breaks | Wraps long lines | Good for |
|---|---|---|---|
white-space: normal | No | Yes | Regular paragraphs |
white-space: pre | Yes | No | Exact code formatting, ASCII art |
white-space: pre-wrap | Yes | Yes | Debug output, logs, formatted text |
white-space: pre-line | Line breaks only | Yes | Text where line breaks matter more than spaces |
pre vs pre-wrap
prekeeps formatting exactly and avoids wrapping.pre-wrapkeeps formatting but wraps when needed.
Wrap vs horizontal scroll
| Choice | Best when | Trade-off |
|---|---|---|
| Wrap text | Readability is more important than exact line shape | Wrapped lines may be harder to copy visually |
| Horizontal scroll | Exact line layout matters | Users may need to scroll sideways |
Cheat Sheet
/* Make <pre> wrap while preserving whitespace */
pre {
white-space: pre-wrap;
}
/* Better for long unbroken strings */
pre {
white-space: pre-wrap;
overflow-wrap: break-word;
}
Quick rules
<pre>normally behaves likewhite-space: pre- Use
white-space: pre-wrapto preserve formatting and allow wrapping - Use
overflow-wrap: break-wordfor very long words or strings - Use horizontal scrolling instead of wrapping for many code samples
Common values of white-space
normal→ normal text behaviorpre→ preserve spaces and line breaks, no wrappingpre-wrap→ preserve spaces and line breaks, allow wrappingpre-line→ collapse spaces, preserve line breaks, allow wrapping
Good default for debug output
pre.debug {
white-space: pre-wrap;
overflow-wrap: break-word;
max-width: 100%;
}
FAQ
How do I make text wrap in a <pre> tag?
Use CSS:
pre {
white-space: pre-wrap;
}
Why does <pre> not wrap text by default?
Because <pre> is meant to preserve whitespace and formatting exactly, which usually includes keeping long lines unwrapped.
Can I preserve spaces and still wrap lines in <pre>?
Yes. That is exactly what white-space: pre-wrap does.
What if long strings still overflow inside <pre>?
Add:
overflow-wrap: break-word;
This helps break long unbroken content such as URLs or tokens.
Should code blocks in <pre> wrap?
Sometimes, but not always. For real code samples, horizontal scrolling is often better. For logs and debug output, wrapping is usually easier to read.
What is the difference between pre and pre-wrap?
pre preserves formatting and prevents wrapping. pre-wrap preserves formatting and allows wrapping.
Can I apply wrapping to only some <pre> elements?
Yes. Use a class:
pre.wrap {
white-space: pre-wrap;
}
Mini Project
Description
Build a small debug output panel that displays formatted text inside a <pre> element. The goal is to preserve spacing and line breaks while making long lines wrap cleanly inside a fixed-width box. This mirrors a common real-world pattern in admin panels, logging tools, and documentation pages.
Goal
Create a <pre>-based output box that preserves formatting and wraps long lines without overflowing its container.
Requirements
- Create an HTML page with a
<pre>element for debug output. - Style the
<pre>so that spaces and line breaks are preserved. - Make long lines wrap inside a fixed-width container.
- Ensure very long unbroken strings do not overflow.
- Add at least two lines of sample debug text.
Keep learning
Related questions
Angular ngClass Conditional Class Binding Explained
Learn how to use Angular ngClass for conditional classes, fix common binding mistakes, and understand why this template error happens.
CSS :not() Selector for Elements Without a Class or Attribute
Learn how to use the CSS :not() selector to target elements that do not have a specific class or attribute, with examples and common pitfalls.
CSS Font Scaling Relative to Container Size: %, em, rem, vw, and Responsive Text
Learn how CSS font scaling really works and how to make text responsive using %, em, rem, vw, clamp(), and media queries.