π Introduction
The word-break property in CSS controls how words should break when they reach the end of a line. It is especially useful for handling long words, URLs, user-generated text, or multilingual content that doesn't naturally break. Properly using word-break keeps your layouts clean and prevents text overflow issues. β¨
π― Why Use word-break?
- To prevent long words from breaking UI layouts π§±
- To handle long URLs or code snippets π
- To improve readability in narrow containers π±
- To support languages without spaces (Chinese, Japanese, Korean) π
β¨ Syntax
Basic Syntax
selector {
word-break: normal | break-all | keep-all | break-word;
}Note
break-word is a legacy value (alias for overflow-wrap: break-word) and is not part of the official spec, but still widely supported.π§© Available Values
1. normal (default)
Uses default line-breaking rules. Long words may overflow if they cannot break naturally.
2. break-all π¨
Breaks words at arbitrary points if necessary. Good for handling extremely long words or strings without spaces.
break-all Example
.break-all {
word-break: break-all;
}3. keep-all π
Prevents word breaks in languages using normal spacing (like Chinese, Japanese, Korean). Does not apply to languages using Latin script (English).
keep-all Example
.keep-all {
word-break: keep-all;
}4. break-word (legacy but useful) π
Breaks words only when absolutely necessary. Works as a fallback for older browsers. Equivalent to overflow-wrap: break-word.
break-word Example
.break-word {
word-break: break-word; /* legacy */
}π₯ Practical Examples
1. Prevent Layout Breaking Due to Long URLs
URL Example
.url {
width: 200px;
border: 1px solid #ccc;
word-break: break-all;
}2. Maintain Clean Text in Articles (Use Normal)
Normal Article Text
article p {
word-break: normal;
}3. Handling Multilingual CJK Content
keep-all Example
.cjk {
word-break: keep-all;
}4. Prevent Overflow in Narrow UI Boxes
Narrow Box
.narrow {
width: 120px;
word-break: break-word;
}π Comparison Table
| Value | Break Long Words? | Good For |
|---|---|---|
| normal | No | Standard paragraphs |
| break-all | Yes (anywhere) | Long URLs, technical content |
| keep-all | No for CJK languages | Chinese/Japanese/Korean text |
| break-word | Yes (when needed) | General overflow handling |
πΌοΈ Visual Example
Further details can be found at: https://dummyimage.com/900x260/eeeeee/000000&text=word-break+demo
π Conclusion
The word-break property is essential when dealing with long words, responsive layouts, and multilingual content. Use break-all for forcing breaks, keep-all for CJK text, and normal for typical content. Pair it with overflow-wrap and white-space for complete control over text layout! π