CSS
Master CSS overflow-wrap: How to Prevent Text and Long URLs from Breaking Your Layout
Have you ever seen a long URL, an email address, or a massive technical term completely spill out of its container and ruin a beautiful website layout? This common issue happens because browsers, by default, try to avoid breaking single words.
To fix this layout nightmare, we use the CSS overflow-wrap property. In this guide, you will learn how to easily handle long strings of text and keep your designs looking clean and professional.
🎯 What is overflow-wrap?
The overflow-wrap property (formerly known as word-wrap) tells the browser whether it should break lines inside an otherwise unbreakable string of text when it exceeds the width of its containing box.
Simply put: It decides what to do when a single word is too long to fit on one line.
🛠️ overflow-wrap Values and Practical Use Cases
This property has two primary values that completely change how text behaves on your page:
1. normal (Default)
The browser will only break lines at normal break points, such as spaces or hyphens. If a single word (like a long link) is wider than the container, it will simply overflow and stick out.
Best Used For: Standard body paragraphs, articles, and text blocks where you have full control over the content lengths.
p {
overflow-wrap: normal;
}
2. break-word (The Layout Saver)
If an unbreakable word or string of characters is too long to fit, the browser will force a break anywhere within the word to wrap it onto the next line.
Best Used For: User comments, long URLs, chat applications, and dynamic data inputs. This is a lifesaver for making text responsive on mobile screens.
p {
overflow-wrap: break-word;
}
📊 Quick Comparison Table
| Value | Behavior | Ideal Use Case |
|---|---|---|
| normal | Words only break at spaces; long text will overflow. | Standard web copy and predictable text. |
| break-word | Forces long words to split and wrap to the next line. | Dynamic content, URLs, and mobile responsive text. |
| initial | Resets the property to its default CSS value (normal). | Resetting styles on specific elements. |
| inherit | Adopts the overflow-wrap value of the parent element. | Keeping text behavior consistent across sections. |
-
💡 Note: overflow-wrap vs. word-wrap
- If you have been in web development for a while, you might remember word-wrap. Don't worry! overflow-wrap is simply the modern, official standard name for word-wrap. Browsers still support word-wrap as an alias, but using overflow-wrap is recommended for future-proof code.
