Visualise every white-space value with live interactive demos, then build production-ready CSS that combines white-space, overflow-wrap, word-break, and text-overflow.
Edit the sample text once and watch all 6 demo cards update instantly — see exactly how each value treats spaces, tabs, and newlines.
Combine white-space with overflow-wrap, word-break, text-overflow, and overflow to build complete, copy-paste-ready CSS rules.
Learn the difference between all 6 values, when to use each one, and how white-space interacts with word-break and overflow-wrap.
Edit the sample text below — all six cards update live to show how each value handles your text.
normalCollapses whitespace and wraps text at container width (browser default).
Best for: Body text, paragraphs, most UI text
white-space: normal;nowrapCollapses whitespace but prevents line wrapping — text stays on one line.
Best for: Badges, tags, button labels, table cells
white-space: nowrap;prePreserves all whitespace and newlines; does not wrap.
Best for: Code blocks, preformatted output, ASCII art
white-space: pre;pre-wrapPreserves whitespace and newlines, and wraps when needed.
Best for: User-generated content, chat messages, markdown
white-space: pre-wrap;pre-lineCollapses spaces/tabs but preserves newlines; wraps at container width.
Best for: Poems, lyrics, any content where line breaks matter
white-space: pre-line;break-spacesLike pre-wrap but trailing spaces at line ends also wrap and take space.
Best for: Specialised editors, exact whitespace rendering required
white-space: break-spaces;Combine white-space with companion properties to build a complete CSS rule.
.element {
white-space: normal;
}The white-space CSS property is one of the most important yet frequently misunderstood properties in the CSS specification. It governs two entirely distinct but deeply interrelated aspects of how text is rendered inside an HTML element: whitespace collapsing and line-breaking behaviour. Understanding these two axes is the key to using the property correctly.
Whitespace collapsing refers to what the browser does when it encounters a sequence of whitespace characters — spaces, tabs, and non-breaking spaces — in the HTML source or in JavaScript-rendered text. By default (the normal value), browsers collapse any sequence of whitespace into a single space character. This is why writing multiple spaces in HTML produces only one visible space on screen. The pre, pre-wrap, and break-spaces values all disable this collapsing and preserve whitespace exactly as it appears in the source.
Line-breaking behaviour determines whether the browser is permitted to wrap text onto a new line when it runs out of horizontal space in its container, and whether explicit newline characters in the source create visual line breaks. With normal and nowrap, the browser ignores newline characters in the source (treating them as spaces). With pre, pre-wrap, pre-line, and break-spaces, explicit newlines in the source are respected and rendered as visual line breaks.
The property has been part of the CSS specification since CSS2 and is fully supported across all modern browsers. It applies to any HTML element — not just <pre> — which means you can attach whitespace-preserving behaviour to a <div>, a <span>, a table cell, or any other element. This flexibility makes it especially useful when rendering user-generated content, chat messages, code snippets, terminal output, or any situation where preserving the author's original spacing and line structure matters.
The browser default is white-space: normal. Most UI elements and body text use this default, which is why consecutive HTML spaces appear as a single space and text reflows to fill its container. When you need different behaviour — such as a scrollable code block, a single-line truncated label, or a chat bubble that preserves line breaks — you must explicitly set one of the other values.
A quick-reference comparison of all six values across the four key dimensions.
| Value | Collapses whitespace | Preserves newlines | Allows wrapping | Best use case |
|---|---|---|---|---|
normal | Yes | No | Yes | Default body text |
nowrap | Yes | No | No | Badges, tags, single-line labels |
pre | No | Yes | No | Code blocks, terminal output |
pre-wrap | No | Yes | Yes | User content, chat, markdown |
pre-line | Yes | Yes | Yes | Poems, lyrics, line-break content |
break-spaces | No | Yes | Yes | Exact whitespace editors |
These three CSS properties are frequently confused because they all influence how text behaves when it runs out of horizontal space. However, they operate at fundamentally different levels and control different things. Understanding the distinction allows you to combine them precisely to get exactly the behaviour you need.
white-space is the highest-level property. It controls whether wrapping is allowed at all, and whether whitespace characters and newlines are preserved. If you set white-space: nowrap, the browser will never wrap text, regardless of what word-break or overflow-wrap say. Think of it as a gate: it decides whether wrapping is on or off.
word-break controls where inside a word the browser is permitted to break. With word-break: normal, the browser only breaks at conventional soft-wrap opportunities — spaces, hyphens, and similar characters. With word-break: break-all, the browser may break between any two characters, even mid-syllable in the middle of an English word. With word-break: keep-all, Chinese, Japanese, and Korean (CJK) words are not broken, which mirrors how CJK text would be typeset in a print context. This property only matters when white-space allows wrapping.
overflow-wrap (historically called word-wrap) is a more conservative safety valve. With overflow-wrap: break-word, the browser will only force a mid-word break as a last resort — specifically, only if the word is so long that it would overflow the container even with all other wrap opportunities taken. overflow-wrap: anywhere is similar but also affects the minimum content size calculation, which can matter in flex and grid layouts. Most developers use overflow-wrap: break-word on containers that hold user-generated content (emails, comment bodies, form submissions) where long uninterrupted strings like URLs could overflow.
The practical combination for user content is: white-space: pre-wrap; overflow-wrap: break-word; — this preserves intentional line breaks and extra spaces, wraps long lines, and forces breaks on very long unbroken strings. For a single-line truncation label: white-space: nowrap; overflow: hidden; text-overflow: ellipsis;. For a code block: white-space: pre; overflow-x: auto; (or pre-wrap if you want soft-wrapping).
The side-by-side demo cards let you instantly see how different values render your actual text, so there are no surprises in production.
Skip the MDN lookup. Select the values you need and the tool produces a complete, ready-to-paste CSS rule in seconds.
white-space rarely lives alone. The generator covers overflow-wrap, word-break, text-overflow, and overflow so you get the full picture.
Everything runs in your browser. No text you type is ever sent to a server. Your code and content stays yours.
The responsive layout adapts to mobile, tablet, and desktop screens so you can use it wherever you work.
Each demo card includes a plain-English description, a best-use note, and the exact CSS produced, so you learn the property as you use it.
Type or paste any text into the shared textarea at the top of the demo section. Use multiple spaces, tabs, and newlines to make the differences between values visible. All six demo cards update in real time.
In the CSS Generator, select your white-space value and optionally add overflow-wrap, word-break, text-overflow, and overflow. The tool assembles a complete .element { … } CSS rule as you change the dropdowns.
Click "Copy CSS" to copy the generated rule to your clipboard, then paste it directly into your stylesheet. No reformatting needed — the output follows standard CSS conventions.
Use pre-wrap for user-generated content. Whenever you display content that a person typed — a comment, a bio, a chat message, or a product description — reach for white-space: pre-wrap combined with overflow-wrap: break-word. This combination respects the line breaks your users intentionally inserted, preserves their formatting, wraps long lines so they never overflow the container, and handles URLs and long unspaced words gracefully. If you use white-space: normal instead, all of those line breaks disappear and the text collapses into a wall of prose, which often breaks the meaning or intent of what was written.
Use nowrap for badges, tags, and navigation links. Any UI element that should stay on a single line regardless of container width should be set to white-space: nowrap. This prevents embarrassing mid-word breaks on narrow screens. Common use cases include status badges (e.g., "In progress"), category tags, breadcrumb items, navigation links, and inline button labels. Pair it with overflow: hidden; text-overflow: ellipsis if the container has a max-width and you want graceful truncation instead of overflow.
Use pre for code blocks. The <pre> HTML element sets white-space: pre by default, which is exactly what you want for raw code samples where indentation and line length are semantically significant. If you want code that also soft-wraps for readability (for example in a mobile context), switch to white-space: pre-wrap. Most syntax-highlighting libraries handle this for you, but it is useful to know when implementing custom code renders.
Use overflow: hidden + text-overflow: ellipsis for single-line truncation. The classic CSS truncation pattern — white-space: nowrap; overflow: hidden; text-overflow: ellipsis — requires all three properties to work correctly. The nowrap keeps text on one line, overflow: hidden clips the overflow, and text-overflow: ellipsis replaces the clipped portion with "…". If any of the three is missing, the ellipsis will not appear. Note that this pattern requires the element to have a defined width (or be inside a flex/grid container that constrains its width).
Consider pre-line when newlines matter but extra spaces do not. If you have content where authors use manual line breaks for structure (poems, verse, bullet-style prose) but you do not want to preserve extra spaces or tabs, white-space: pre-line is a clean choice. It collapses multiple consecutive spaces as normal but treats \n characters as hard line breaks. This gives you the most important benefit of pre-wrap — preserved newlines — without the potential visual noise of preserved indentation or extra spaces.
Beautify and format CSS code with proper indentation and spacing.
Expand minified CSS into readable, formatted code instantly.
Generate CSS list-style properties with a live visual preview.
Build HTML elements with attributes and inline styles visually.