π Introduction
The overflow-wrap property in CSS controls whether the browser should break long words when they exceed the width of their container. It is essential for handling user-generated content, long URLs, filenames, and responsive UI elements. Without it, long words may overflow and break your layout. π¬ Using overflow-wrap keeps your UI clean and readable. β¨
π― Why Use overflow-wrap?
- To prevent long words from breaking your layout π§±
- To force-breaking long URLs or strings π
- To maintain responsive design on small screens π±
- To control wrapping more safely than word-break: break-all βοΈ
β¨ Syntax
Basic Syntax
selector {
overflow-wrap: normal | break-word | anywhere;
}Note
π§© Available Values
1. normal (default)
Words will only break at normal break points (spaces, hyphens). Long words may overflow if thereβs no natural breaking point.
2. break-word (most common) β
Forces long words to wrap when needed β prevents overflow but breaks only when necessary. This is the safest and most widely used option.
break-word Example
.text {
overflow-wrap: break-word;
}3. anywhere 𧨠(Modern)
Allows breaking at any point, even if it produces awkward breaks. More aggressive than break-word, similar to word-break: break-all but safer.
anywhere Example
.force-break {
overflow-wrap: anywhere;
}π₯ Practical Examples
1. Prevent Layout Break with long URLs
URL Example
.url-box {
width: 200px;
border: 1px solid #ccc;
padding: 8px;
overflow-wrap: break-word;
}2. Chat Messages or User Comments
Chat Example
.chat-message {
max-width: 250px;
overflow-wrap: break-word;
background: #f4f4f4;
padding: 10px;
border-radius: 8px;
}3. Aggressive Wrapping for Responsive UI
Aggressive Break
.tag {
overflow-wrap: anywhere;
padding: 4px 8px;
background: #eaeaea;
}π Comparison Table
| Value | Break Long Words? | Ideal Use |
|---|---|---|
| normal | No (unless natural break) | Clean paragraphs, articles |
| break-word | Yes (when necessary) | General UI preventing overflow |
| anywhere | Yes (at any point) | Very long strings, technical text |
πΌοΈ Visual Example
Further details can be found at: https://dummyimage.com/900x250/eeeeee/000000&text=overflow-wrap+demo
π Conclusion
The overflow-wrap property is essential for maintaining clean, readable, and responsive layouts. Use break-word for safe, common scenarios, and anywhere for extreme overflow prevention. Pair it with word-break and white-space for full text control. π