CSS overflow-wrap Property
The CSS overflow-wrap property controls whether long words or unbreakable text can break onto additional lines to prevent overflow.
It is particularly useful for long URLs, filenames, identifiers, and strings without spaces. The property introduces additional line-breaking opportunities when text cannot otherwise fit within its container.
overflow-wrap Syntax
The overflow-wrap property accepts one keyword that determines how the browser handles text that cannot fit within the available line width.
Basic Syntax
selector {
overflow-wrap: anywhere;
}
Keyword Values
.text {
overflow-wrap: normal;
}
.text {
overflow-wrap: anywhere;
}
.text {
overflow-wrap: break-word;
}
CSS-Wide Keywords
.text {
overflow-wrap: initial;
}
.text {
overflow-wrap: inherit;
}
.text {
overflow-wrap: unset;
}
.text {
overflow-wrap: revert;
}
.text {
overflow-wrap: revert-layer;
}
The initial value is normal. Unlike many CSS properties, overflow-wrap is inherited, so its value can affect text within descendant elements.
Property Values
| Value | Description |
|---|---|
normal |
Breaks text only at normally permitted line-breaking opportunities. |
anywhere |
Allows otherwise unbreakable text to break at arbitrary points when necessary to prevent overflow. These opportunities are considered when calculating min-content size. |
break-word |
Allows emergency breaks within otherwise unbreakable text, but does not consider those breaks when calculating min-content size. |
initial |
Resets the property to normal. |
inherit |
Uses the computed value of the parent element. |
unset |
Uses the inherited value because the property is inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | normal |
| Inherited | Yes |
| Applies to | Text elements |
| Computed value | Specified keyword |
| Animation type | Discrete |
| Percentage values | Not accepted |
| Length values | Not accepted |
| Legacy alias | word-wrap |
Valid Values
.text {
overflow-wrap: normal;
}
.text {
overflow-wrap: anywhere;
}
.text {
overflow-wrap: break-word;
}
Invalid Values
.text {
overflow-wrap: auto;
}
.text {
overflow-wrap: break-all;
}
.text {
overflow-wrap: 10px;
}
The break-all keyword belongs to the word-break property, not overflow-wrap.
How It Works
Browsers normally wrap text at spaces and other permitted line-breaking opportunities. Long strings without suitable breaks can extend beyond their containers.
The overflow-wrap property determines whether additional breaks may be introduced to prevent that overflow.
Without Emergency Wrapping
.text {
width: 220px;
overflow-wrap: normal;
}
A long unbroken string may extend beyond the container because no suitable line-breaking opportunity exists.
With Emergency Wrapping
.text {
width: 220px;
overflow-wrap: anywhere;
}
The browser can break an otherwise unbreakable string when it cannot fit on the line.
Normal Words
Ordinary text with spaces generally wraps normally regardless of the overflow-wrap value. Additional breaks are introduced only when needed.
Important Consideration
The property affects line-breaking opportunities, not the actual characters in the text. It does not insert spaces or automatically add hyphens.
normal Value
The normal value allows text to wrap only at normally permitted line-breaking opportunities.
Basic Example
.text {
overflow-wrap: normal;
}
Long Unbroken Text
<p class="text">ThisIsAnExtremelyLongUnbrokenStringOfTextThatMayOverflow</p>
.text {
width: 200px;
overflow-wrap: normal;
}
The string may extend beyond the paragraph's available width.
When normal Is Useful
Use normal when standard text wrapping is sufficient and additional emergency breaks are unnecessary.
anywhere Value
The anywhere value permits additional breaks within otherwise unbreakable strings when necessary to prevent overflow.
Basic Example
.text {
overflow-wrap: anywhere;
}
Long URLs
.url-text {
overflow-wrap: anywhere;
}
Long URLs can wrap within narrow containers without requiring horizontal scrolling.
Long Identifiers
.identifier {
overflow-wrap: anywhere;
}
This is useful for long filenames, generated identifiers, and other strings without natural break points.
Intrinsic Sizing
Break opportunities introduced by anywhere are considered when calculating an element's min-content size. This can allow flex and grid items to become narrower than they otherwise would.
Important Consideration
The browser does not insert a hyphen at an emergency break introduced by anywhere.
break-word Value
The break-word value allows otherwise unbreakable text to break when necessary, similarly to anywhere.
Basic Example
.text {
overflow-wrap: break-word;
}
Long Filenames
.filename {
overflow-wrap: break-word;
}
A long filename can break across lines instead of extending outside a constrained container.
Min-Content Sizing
Unlike anywhere, the additional breaks introduced by break-word are not considered when calculating min-content size.
Important Consideration
The break-word value of overflow-wrap is different from the legacy word-break: break-word declaration. Use the correct property for the intended behavior.
anywhere vs. break-word
The anywhere and break-word values often produce similar visual wrapping, but they differ in intrinsic sizing calculations.
| Behavior | anywhere | break-word |
|---|---|---|
| Allows emergency line breaks | Yes | Yes |
| Prevents overflow from long unbroken text | Yes, when wrapping is permitted | Yes, when wrapping is permitted |
| Considers emergency breaks for min-content sizing | Yes | No |
| Automatically inserts hyphens | No | No |
Using anywhere
.grid-item {
overflow-wrap: anywhere;
}
This can help a grid item shrink because the additional break opportunities affect its min-content contribution.
Using break-word
.grid-item {
overflow-wrap: break-word;
}
The text can still wrap, but its min-content contribution does not account for those emergency breaks.
Which Value Should You Use?
For responsive layouts containing long unbroken strings, anywhere is often the more useful choice. Use break-word when you want emergency wrapping without changing min-content sizing calculations.
overflow-wrap vs. word-break
Both properties influence line breaking, but they serve different purposes.
overflow-wrap
.text {
overflow-wrap: anywhere;
}
Introduces emergency breaks when otherwise unbreakable text cannot fit within the available line width.
word-break
.text {
word-break: break-all;
}
Allows breaks between characters under broader conditions, potentially breaking words even when normal word boundaries could have been used.
Comparison
| Property | Purpose |
|---|---|
overflow-wrap |
Adds emergency breaks to prevent otherwise unbreakable text from overflowing. |
word-break |
Controls line-breaking rules within words and character sequences. |
hyphens |
Controls whether words may be hyphenated at permitted points. |
Recommended Approach
Use overflow-wrap: anywhere when the goal is to prevent long strings from overflowing without unnecessarily breaking ordinary words.
Interaction with white-space
The white-space property controls whether text is allowed to wrap normally. Its value can affect whether overflow-wrap has a visible result.
Normal Wrapping
.text {
white-space: normal;
overflow-wrap: anywhere;
}
Text wraps normally, and additional emergency breaks are permitted when needed.
No Wrapping
.text {
white-space: nowrap;
overflow-wrap: anywhere;
}
The nowrap value suppresses automatic line wrapping, so overflow-wrap does not provide the expected emergency breaks.
Preserving Whitespace
.text {
white-space: pre-wrap;
overflow-wrap: anywhere;
}
Whitespace and line breaks are preserved while additional wrapping opportunities remain available.
Important Consideration
If overflow-wrap appears ineffective, check whether white-space or another text formatting rule prevents wrapping.
Responsive Layouts
Long unbroken strings can cause flex items, grid columns, and other responsive components to exceed their intended widths.
Responsive Cards
.card-content {
overflow-wrap: anywhere;
}
Grid Layouts
.grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 20px;
}
.grid-item {
overflow-wrap: anywhere;
}
The minmax(0, 1fr) columns can shrink below their content's intrinsic minimum size, while overflow-wrap allows long text to break within the available space.
Flexbox Layouts
.flex-container {
display: flex;
gap: 20px;
}
.flex-item {
flex: 1;
min-width: 0;
overflow-wrap: anywhere;
}
The min-width: 0 declaration allows flex items to shrink, while overflow-wrap: anywhere helps prevent long strings from extending beyond their boundaries.
Long Links
.content a {
overflow-wrap: anywhere;
}
This is useful for URLs displayed as link text in articles, documentation, and narrow content areas.
overflow-wrap Example
This example compares the three values using the same long, unbroken string inside fixed-width containers.
HTML Example
<div class="wrap-demo">
<div class="wrap-item">
<h3>normal</h3>
<div class="wrap-box wrap-normal">
ThisIsAnExtremelyLongUnbrokenStringOfText
</div>
</div>
<div class="wrap-item">
<h3>anywhere</h3>
<div class="wrap-box wrap-anywhere">
ThisIsAnExtremelyLongUnbrokenStringOfText
</div>
</div>
<div class="wrap-item">
<h3>break-word</h3>
<div class="wrap-box wrap-break-word">
ThisIsAnExtremelyLongUnbrokenStringOfText
</div>
</div>
</div>
CSS Example
.wrap-demo {
display: flex;
flex-wrap: wrap;
gap: 24px;
padding: 20px;
background-color: #f1f5f9;
}
.wrap-item {
width: 200px;
}
.wrap-item h3 {
margin: 0 0 12px;
font-size: 16px;
}
.wrap-box {
box-sizing: border-box;
width: 200px;
min-height: 110px;
padding: 12px;
border: 2px solid #64748b;
background-color: #fff;
}
.wrap-normal {
overflow-wrap: normal;
}
.wrap-anywhere {
overflow-wrap: anywhere;
}
.wrap-break-word {
overflow-wrap: break-word;
}
normal
anywhere
break-word
The normal example allows the long string to extend beyond its box. The anywhere and break-word examples introduce additional breaks to keep the text within the available width.
Long URL Example
This example demonstrates a common use of overflow-wrap: anywhere in a narrow content area.
<div class="url-demo">
<p>Example URL:</p>
<p class="url-text">https://www.example.com/documentation/really-long-example-page-name-without-spaces</p>
</div>
.url-demo {
box-sizing: border-box;
width: 260px;
padding: 15px;
border: 2px solid #64748b;
background-color: #f8fafc;
}
.url-text {
overflow-wrap: anywhere;
}
Example URL:
https://www.example.com/documentation/really-long-example-page-name-without-spaces
The URL wraps within the container rather than extending beyond its border.
Intrinsic Sizing Example
The difference between anywhere and break-word becomes more apparent when the container uses intrinsic sizing.
.intrinsic-anywhere {
width: min-content;
overflow-wrap: anywhere;
}
.intrinsic-break-word {
width: min-content;
overflow-wrap: break-word;
}
The anywhere value allows emergency break opportunities to contribute to min-content sizing. The break-word value does not, so its min-content width may remain much larger.
Accessibility Considerations
Text wrapping can improve readability and prevent content from requiring unnecessary horizontal scrolling.
Support Text Enlargement
Long words and URLs should remain readable when users increase browser zoom or text size.
Avoid Unnecessary Horizontal Scrolling
Using overflow-wrap: anywhere can help prevent a single long string from forcing an entire page to scroll horizontally.
Preserve Text Content
Emergency line breaks do not change the underlying characters, allowing users to select and copy the original text.
Avoid Excessive Word Breaking
Breaking ordinary words unnecessarily can make text harder to read. Prefer overflow-wrap over aggressive character-by-character breaking when only emergency wrapping is needed.
Important Consideration
Test text wrapping with narrow viewports, increased zoom, and different languages to ensure that content remains readable and usable.
Common Mistakes
Using break-all with overflow-wrap
.text {
overflow-wrap: break-all;
}
The break-all keyword belongs to word-break.
Confusing overflow-wrap with overflow
.text {
overflow: hidden;
}
The overflow property clips or scrolls overflowing content. It does not introduce additional text-wrapping opportunities.
Using nowrap with overflow-wrap
.text {
white-space: nowrap;
overflow-wrap: anywhere;
}
The nowrap value prevents automatic wrapping, so the emergency wrapping behavior will not work as expected.
Assuming anywhere and break-word Are Identical
Both can introduce emergency breaks, but only anywhere considers those breaks when calculating min-content size.
Using Invalid Length Values
.text {
overflow-wrap: 10px;
}
The property accepts keywords, not lengths.
Expecting Automatic Hyphenation
The property does not insert hyphens at emergency breaks. Use hyphens when language-aware hyphenation is required.
Ignoring Flex and Grid Minimum Sizes
Some layouts require additional sizing rules, such as min-width: 0 or minmax(0, 1fr), to allow items to shrink as intended.
Important Considerations
Keep the following points in mind when using overflow-wrap:
- The property controls emergency line breaking within otherwise unbreakable text.
- Its initial value is
normal. - The property is inherited.
- Its supported values are
normal,anywhere, andbreak-word. normaluses standard line-breaking opportunities.anywherepermits emergency breaks and affects min-content sizing.break-wordpermits emergency breaks without affecting min-content sizing.- Emergency breaks do not automatically insert hyphens.
- The property does not modify the underlying text.
word-wrapis a legacy alias foroverflow-wrap.word-breakcontrols different line-breaking behavior.white-space: nowrapprevents automatic wrapping.- Long URLs and identifiers are common use cases.
- The property is useful in responsive flex and grid layouts.
- Additional sizing constraints may be necessary in flex and grid containers.
- Using
anywherecan help prevent unwanted horizontal page overflow. - The property does not clip or hide overflowing content.
- It does not accept length or percentage values.
- Text wrapping can vary with language and browser line-breaking rules.
- Test layouts at narrow widths and increased zoom levels.
Browser Support
The CSS overflow-wrap property is Widely available across modern browsers.
The normal and break-word values have broad browser support. The anywhere value is supported in modern browsers, but older browsers may require a fallback.
Fallback Example
.text {
overflow-wrap: break-word;
overflow-wrap: anywhere;
}
Browsers that do not recognize anywhere can continue using break-word.
Legacy Alias
.text {
word-wrap: break-word;
overflow-wrap: break-word;
}
The older word-wrap property is an alias for overflow-wrap and remains supported for compatibility.
For current compatibility information, visit Can I Use? and search for overflow-wrap.
Specifications
The overflow-wrap property is defined in CSS Text Module Level 3.
CSS Text Module Level 3: overflow-wrap
The specification defines the supported values, emergency line-breaking behavior, and differences between anywhere and break-word.
For additional documentation and examples, consult MDN Web Docs.
