How do I wrap text in a pre tag?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To wrap text inside an HTML <pre> tag, apply white-space: pre-wrap in CSS. This preserves the whitespace and line breaks that make <pre> useful while allowing the browser to wrap long lines at the container boundary instead of forcing horizontal scrolling.
That is the core answer. The rest of this article covers why <pre> does not wrap by default, how to handle extremely long unbroken strings, when wrapping is the wrong choice, and how to build a production-quality code block component that works across screen sizes.
Why <pre> Does Not Wrap by Default
The browser's default stylesheet applies white-space: pre to <pre> elements. This mode:
- Preserves all spaces and tabs exactly as written
- Preserves explicit line breaks (newlines in the source)
- Disables automatic line wrapping entirely
This behavior is intentional. <pre> stands for "preformatted text," and the whole point is that the content should render exactly as authored. Code listings, ASCII art, and fixed-width tables all depend on this.
The problem appears when a single line is wider than the container. Without wrapping, the content overflows and creates a horizontal scrollbar, or worse, extends beyond the visible area with no scrollbar at all.
The white-space Property Values
Understanding the full set of white-space values clarifies why pre-wrap is the right choice:
| Value | Preserves Spaces | Preserves Newlines | Auto-Wraps |
normal | No (collapses) | No (collapses) | Yes |
nowrap | No (collapses) | No (collapses) | No |
pre | Yes | Yes | No |
pre-wrap | Yes | Yes | Yes |
pre-line | No (collapses) | Yes | Yes |
break-spaces | Yes (even trailing) | Yes | Yes |
pre-wrap is the only value that preserves both spaces and newlines while also enabling automatic line wrapping. It keeps the formatting behavior of <pre> and adds the wrapping behavior of normal text.
Basic Implementation
The multiple spaces between "all," "spaces," and "and" are preserved. The explicit newline before "explicit line breaks" is also preserved. But the long first line wraps at the container width instead of overflowing.
Handling Long Unbroken Strings
pre-wrap wraps at word boundaries (spaces, hyphens). If the content contains a very long token with no break opportunities (a URL, a base64 string, a long variable name), the browser may still overflow the container.
Add overflow-wrap to allow breaking within long words:
For even more aggressive breaking:
The difference between break-word and anywhere is subtle: anywhere allows the browser to consider mid-word breaks when calculating minimum content width, which affects layout in flex and grid containers. For most <pre> use cases, break-word is sufficient.
Legacy Compatibility
The word-wrap property is the old name for overflow-wrap. Both work in all modern browsers, but overflow-wrap is the standard name:
Production Code Block Styling
A real-world code block needs more than just wrapping. Here is a complete, production-ready style:
The tab-size property controls how tab characters render. The default is 8 spaces, which is too wide for most code. Setting it to 4 (or 2) keeps indentation reasonable.
When Not to Wrap: Use Horizontal Scrolling Instead
Wrapping is not always the right choice. Some content is meaningfully laid out in columns, and wrapping destroys readability:
- ASCII art and diagrams
- Fixed-width data tables
- Code where indentation alignment carries meaning (YAML, Python)
- Diff output
For these cases, use overflow-x: auto instead:
This preserves the exact layout and adds a horizontal scrollbar only when content overflows.
Responsive Strategy: Wrap on Mobile, Scroll on Desktop
You can use different strategies at different viewport widths:
On desktop, users get a scrollbar to see the full line. On mobile, lines wrap to fit the screen. This is a practical compromise for documentation sites and blogs that need to work on all devices.
Using <pre> With <code> Elements
In semantic HTML, code blocks use <pre> wrapped around <code>:
When nesting these elements, apply the wrapping styles to <pre> and reset any conflicting styles on <code>:
The inherit value ensures <code> does not override the wrapping behavior set on <pre>.
Comparison of Overflow Strategies
| Strategy | CSS | Preserves Layout | Works on Mobile | Best For |
| Wrapping | white-space: pre-wrap | Partially (line breaks change) | Yes | Prose, logs, stack traces |
| Horizontal scroll | overflow-x: auto | Fully | Usable but awkward | Code, tables, diagrams |
| Responsive hybrid | Media query switch | Desktop: full; Mobile: wrap | Yes | Documentation, blogs |
| Forced break | overflow-wrap: anywhere | No (breaks mid-word) | Yes | URLs, long tokens |
Common Pitfalls
Using white-space: normal instead of pre-wrap is a frequent mistake. While normal enables wrapping, it also collapses multiple spaces into one and ignores explicit newlines, destroying the preformatted nature of the content.
Applying pre-wrap without overflow-wrap fails when content includes unbreakable strings like URLs or base64 data. The browser wraps at spaces but cannot break the long token, so it still overflows.
Forgetting to set tab-size leaves tabs rendering at the browser default of 8 spaces, which makes indented code look excessively wide.
Styling only the desktop view and ignoring mobile leads to <pre> blocks that overflow the viewport on phones, breaking the entire page layout. Always test <pre> elements at narrow widths.
Using word-break: break-all instead of overflow-wrap: break-word breaks words at arbitrary points even when there are natural break opportunities. This produces ugly results for normal text. Prefer overflow-wrap, which only breaks words as a last resort.
Setting overflow: hidden on <pre> silently clips content without any visual indication, making users unaware that text is missing. Use overflow-x: auto (scrollbar) or pre-wrap (wrapping), never hidden.
Summary
- Apply
white-space: pre-wrapto<pre>elements to enable wrapping while preserving whitespace and line breaks. - Add
overflow-wrap: break-wordto handle long unbroken strings that would otherwise overflow. - Use
overflow-x: autoinstead of wrapping when exact column alignment matters (code, tables, diagrams). - Consider a responsive approach: scroll on desktop, wrap on mobile.
- Never use
white-space: normalon<pre>because it collapses the whitespace that<pre>is designed to preserve. - Set
tab-size: 4to prevent tabs from rendering at the default width of 8 spaces.

