π Introduction
Text handling in CSS can be confusing because several properties seem to control wrapping and breaking β but each works differently. In this tutorial, weβll clearly compare:text-wrap, white-space, word-break, andoverflow-wrap. By the end, youβll know exactly which one to use and when. β¨
π§ What Each Property Does (Quick Summary)
- text-wrap β Controls *wrapping behavior* (modern CSS).
- white-space β Controls *spaces, line breaks, and wrapping* behavior.
- word-break β Controls *breaking inside words*.
- overflow-wrap β Controls *breaking long words to prevent overflow*.
1οΈβ£ text-wrap
text-wrap defines how text should wrap naturally. It is part of the modern CSS Text Level 4 specification.
text-wrap Syntax
text-wrap: wrap | nowrap | balance | pretty;- wrap β default normal wrapping
- nowrap β prevent wrapping
- balance β balance lines (good for headings)
- pretty β avoids ugly breaks
2οΈβ£ white-space
white-space controls how spaces, tabs, and line breaks behave. This is the most fundamental text-formatting property.
white-space Syntax
white-space: normal | nowrap | pre | pre-wrap | pre-line | break-spaces;- normal β collapses spaces, wraps text
- nowrap β collapses spaces, no wrap
- pre β preserves spaces + line breaks, no wrap
- pre-wrap β preserves spaces + line breaks, wraps
- break-spaces β like pre-wrap but keeps trailing spaces
3οΈβ£ word-break
word-break decides how words break when they reach the container edge.
word-break Syntax
word-break: normal | break-all | keep-all | break-word;- normal β only break at natural boundaries
- break-all β breaks anywhere (aggressive)
- keep-all β avoids breaking CJK text
- break-word β non-standard; similar to overflow-wrap
4οΈβ£ overflow-wrap
overflow-wrap prevents overflow when very long words exceed the container width. Previously known as word-wrap.
overflow-wrap Syntax
overflow-wrap: normal | break-word | anywhere;- normal β no forced breaks
- break-word β break only when needed
- anywhere β break at any point if necessary
π Comparison Table
| Property | Controls | Wrap? | Break Long Words? | Preserves Spaces? | Best Use |
|---|---|---|---|---|---|
| text-wrap | How text wraps visually | Yes | No | No | Headings, paragraphs, modern layouts |
| white-space | Spaces + newlines | Depends | No | Sometimes | Preformatted, chat UI, poems |
| word-break | Break rules inside words | Yes | Yes (break-all) | No | CJK text, long technical strings |
| overflow-wrap | Prevent overflow | Yes | Yes (softly) | No | Safe breaking for responsive UI |
π₯ Practical Examples
1. Prevent Overflow in Long Text
Overflow Fix
.safe-box {
width: 200px;
overflow-wrap: break-word;
}2. Balanced Hero Heading
Balanced Heading
h1 {
text-wrap: balance;
}3. Prevent Wrapping for UI Buttons
nowrap Button
button {
white-space: nowrap;
}4. Forcing Breaks When Needed
Aggressive Break
.force-break {
word-break: break-all;
}πΌοΈ Visual Demo
Further details can be found at: https://dummyimage.com/1000x260/eeeeee/000000&text=text-wrap+vs+white-space+vs+word-break+vs+overflow-wrap
π§ When to Use What (Final Guide)
- βοΈ Use text-wrap β For modern wrapping control (headings, UX text).
- βοΈ Use white-space β For preserving spaces or preventing wrapping.
- βοΈ Use overflow-wrap β To prevent UI from breaking due to long words.
- βοΈ Use word-break β For aggressive breaking or CJK text support.
π Conclusion
Understanding these four properties helps you build layouts that handle long text gracefully across all screens. Whether you're building blogs, dashboards, chat apps, or responsive components, these text tools give you full control over wrapping and breaking behavior. π