🧠 CSS Tutorial: overflow-wrap

πŸ“Œ 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

overflow-wrap was previously known as word-wrap. Both work the same, but overflow-wrap is the modern standard.

🧩 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

ValueBreak Long Words?Ideal Use
normalNo (unless natural break)Clean paragraphs, articles
break-wordYes (when necessary)General UI preventing overflow
anywhereYes (at any point)Very long strings, technical text

πŸ–ΌοΈ Visual Example

>>β€œGood UI design isn’t just about looks β€” it’s also about preventing disasters like overflow.” πŸ’‘

πŸŽ‰ 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. πŸš€