White Space, Line Breaking, Hyphenation and text-wrap: balance and pretty
How browsers handle spaces and line breaks: white-space and its longhands, breaking long words with overflow-wrap and word-break, hyphenation, text-wrap balance and pretty for better line endings, ellipsis truncation and line clamping.
kvi5hnuCode Sprout team member8 min readOn this page
HTML collapses white space: several spaces, tabs and newlines in the source become a single space, and lines wrap wherever the text reaches the edge of its box. CSS controls both: which white space is kept, and where and how lines may break.
white-space and its longhands
white-space is now a shorthand for two properties (the spec adds a third, white-space-trim, not yet implemented): white-space-collapse, whether spaces and newlines are kept, and text-wrap-mode, whether lines wrap.
white-space | Spaces | Newlines | Wraps | Use |
|---|---|---|---|---|
normal | collapsed | collapsed | yes | ordinary text |
nowrap | collapsed | collapsed | no | labels, buttons that must stay on one line |
pre | kept | kept | no | code, ASCII art (the default for <pre>) |
pre-wrap | kept | kept | yes | user-written text, like comments or chat messages |
pre-line | collapsed | kept | yes | addresses and poems typed with line breaks |
break-spaces | kept, and can wrap | kept | yes | editors, where trailing spaces must take room |
Tested in Chrome: white-space: pre-line computes to white-space-collapse: preserve-breaks with text-wrap-mode: wrap, and nowrap to collapse with nowrap. Using the longhands lets you change one aspect without the other:
.chat-message { white-space-collapse: preserve; } /* keep the user's spaces and newlines; wrapping unchanged */
.tag { text-wrap-mode: nowrap; } /* never wrap; spacing unchanged */
Breaking long words
By default, lines only break at soft wrap opportunities: spaces, hyphens, and between characters in scripts like Chinese. A long URL, an email address or a German compound noun with no break opportunity overflows its box. Three properties allow breaks inside words:
| Property | Effect |
|---|---|
overflow-wrap: break-word | break a word only if it would otherwise overflow |
overflow-wrap: anywhere | the same, and the possible breaks count toward the min-content size |
word-break: break-all | break between any two letters, even when not necessary |
word-break: keep-all | don't break between CJK characters; only at spaces, for Korean and some Chinese and Japanese styles |
word-break: auto-phrase | break Japanese at natural phrase boundaries: Chrome 119 |
line-break: strict / loose | how strictly CJK line-breaking rules apply, for example whether a line may start with small kana or certain punctuation; anywhere allows a break between any two characters, even around punctuation |
The difference between break-word and anywhere shows in intrinsic sizing, such as flex items, grid auto tracks and width: min-content. Tested in Chrome with the default 16px font: a min-content box with a 20-letter word was 135px wide with break-word, because the word still counts as unbreakable for sizing, and 8px with anywhere, one letter wide. In a fixed 100px box, both broke the word to fit.
So overflow-wrap: anywhere is the better default for user content in flexible layouts:
.comment, .card-body { overflow-wrap: anywhere; }
word-break: break-all breaks words even when they'd fit on the next line, which makes ordinary text harder to read; keep it for things like hashes or code.
word-break: break-word is an old alias with the same effect as overflow-wrap: anywhere combined with word-break: normal; prefer overflow-wrap.

In the fixed boxes, break-word and anywhere look identical; the numbers underneath show the difference, the width the same text asks for as a min-content box, which is what flex items and auto grid tracks use.
Hyphenation
article { hyphens: auto; }
<html lang="en">
hyphens | Effect |
|---|---|
manual (default) | break only at soft hyphens, ­, written in the HTML |
auto | the browser may hyphenate using its dictionary for the element's language |
none | never hyphenate, even at ­ |
hyphens: auto needs a lang attribute, and the browser must have a dictionary for that language; support varies by language and platform. Two related properties:
hyphenate-characterchanges the character shown at the break, for example"‐"or"=".hyphenate-limit-chars: 6 3 2sets the minimum word length, and the minimum characters before and after the break: Chrome 109, Firefox 137, Safari 27.3. It prevents ugly breaks like "o-ver".
Hyphenation helps most in narrow and justified columns (article 89), and hurts in headings and UI labels; turn it off there.
text-wrap: better line endings
text-wrap is a shorthand for text-wrap-mode and text-wrap-style, which chooses how the browser picks line breaks:

| Value | Effect | Use for |
|---|---|---|
wrap (default) | fill each line as much as possible | body text |
balance | make all lines about the same length | headings, captions, short blocks |
pretty | slower, better choices that avoid a single short word on the last line | paragraphs |
stable | don't rewrap earlier lines as content after them is edited | editable text |
Tested in Chrome: in a 300px box, a 24px heading wrapped as 233px, 273px and 64px lines by default, and 191px, 171px and 209px with text-wrap: balance. Balancing is limited to short blocks (a few lines; Chrome balances up to six), because it's expensive.
h1, h2, h3, blockquote, figcaption { text-wrap: balance; }
p { text-wrap: pretty; }
Both are progressive enhancements: browsers without support wrap normally. balance and stable work everywhere; pretty works in Chrome and Safari 26, not Firefox.
Truncation: ellipsis and line clamping
To cut text off with "…" on one line, three declarations are needed together:
.filename {
white-space: nowrap; /* stay on one line */
overflow: hidden; /* clip what doesn't fit */
text-overflow: ellipsis; /* show … at the clip point */
}
For several lines, the prefixed -webkit-line-clamp works in every browser, with its legacy companions:
.excerpt {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
Tested in Chrome: with -webkit-line-clamp: 2, a 100px-wide paragraph of ten words was cut to two 18px lines, 36px tall. The unprefixed line-clamp property is specified but not yet supported in Chrome (CSS.supports('line-clamp', '2') is false).
Truncated text hides content: make sure the full text is available, for example in a title, on hover, or by linking to the full item. Screen readers read the whole text anyway.
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; }
.box { inline-size: var(--w, 13rem); border: 2px dashed #d8cfc0; padding: 0.4rem; margin-top: 0.5rem; }
h3 { margin: 0 0 0.4rem; font-size: 1.05rem; color: #8a3b12; }
.balanced { text-wrap: balance; }
.balanced::before { content: "balance: "; color: #555; font-weight: 400; }
h3:not(.balanced)::before { content: "wrap: "; color: #555; font-weight: 400; }
.url { overflow-wrap: anywhere; background: #f4efe6; padding: 0.2rem; }
.clip { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; background: #f4efe6; padding: 0.2rem; }
Change the width: the balanced heading keeps its lines even, the long URL breaks wherever needed instead of overflowing, and the file name is clipped with an ellipsis.

The two hyphenation panels show why hyphens: auto matters most for justified text in narrow columns: without it, the browser can only stretch the spaces.
What's current
white-space-collapseandtext-wrap-modelonghands: all browsers.text-wrap: balanceandstable: all browsers;pretty: Chrome and Safari.hyphenate-limit-chars: all browsers since Safari 27.3.word-break: auto-phrase: Chrome only.- Unprefixed
line-clamp: not yet; use-webkit-line-clamp.
Common questions
Why does a long URL overflow its container?
It has no break opportunities. Add overflow-wrap: anywhere.
What's the difference between break-word and anywhere?
Both break only when needed, but anywhere also lets intrinsic sizes shrink, so flex and grid items can get narrower.
Why doesn't hyphens: auto work?
The element or page has no lang attribute, or the browser has no dictionary for that language.
How wide is a tab character in <pre> or white-space: pre?
Eight spaces by default. tab-size changes it, as a number of spaces or a length: tested in Chrome with a monospace font, a tab followed by x was 9 characters wide by default, 5 with tab-size: 4 and 3 with tab-size: 2ch. It matters for code samples that are indented with tabs.
Why doesn't text-overflow: ellipsis show?
It needs overflow set to something other than visible, such as hidden, on the same element, and the text must actually overflow: usually white-space: nowrap plus a constrained width.
Summary
white-spaceis a shorthand forwhite-space-collapseandtext-wrap-mode;pre-wrapkeeps user formatting while wrapping.overflow-wrap: anywherebreaks long words only when needed and lets intrinsic sizes shrink;word-breakchanges breaking rules more aggressively.hyphens: autoneedslang;hyphenate-limit-charsprevents short fragments.text-wrap: balancefor headings andprettyfor paragraphs improve line endings.- Single-line ellipsis needs
nowrap,overflow: hiddenandtext-overflow: ellipsis; multi-line uses-webkit-line-clamp.
Practice questions
User comments should keep their line breaks and spaces, but wrap at the box edge, and long links shouldn't overflow. Write the CSS.
Answer
.comment { white-space: pre-wrap; overflow-wrap: anywhere; }
A flex item contains a long URL and won't shrink, even with overflow-wrap: break-word. What change fixes it?
Answer
Use overflow-wrap: anywhere. Tested in Chrome: in a 200px flex row with a 50px sibling, the item stayed 256px wide with break-word and shrank to 150px with anywhere. With break-word, the URL's full length still counts toward the item's min-content size, which flex items use as their minimum. Alternatively, give the item min-inline-size: 0.
Write CSS that shows at most two lines of a card's description, with an ellipsis.
Answer
.card p {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
Which text-wrap value suits headings, and which suits paragraphs?
Answer
balance for headings, so their lines are of similar length; pretty for paragraphs, to avoid a single word on the last line.
Previous: Text Decoration, Underline Offsets, Emphasis Marks and Text Shadows

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.