CSS Reference ยท Box Model and Sizing
The overflow-wrap property
Reference for the CSS overflow-wrap property: when a long word or URL may be broken so it does not overflow its box. Values, anywhere vs break-word, examples.
The overflow-wrap property lets the browser break a line in the middle of a word when that word is too long to fit in its box. Without it, a long URL or an unbroken string such as a file hash pushes out of the container and causes horizontal overflow.
.comment { overflow-wrap: break-word; }
The older name word-wrap is a legacy alias for the same property.
Try it
The first box lets the address stick out through its right border. The second breaks the address at the box edge, only because it could not fit on a line of its own.
Syntax
overflow-wrap: normal | break-word | anywhere
- Initial value:
normal - Applies to: text (all elements)
- Inherited: yes
- Animatable: no (discrete)
- Computed value: as specified
Values
| Value | Description |
|---|---|
normal |
Lines break only at normal break opportunities (spaces, hyphens). A long word overflows. |
break-word |
An otherwise unbreakable word may be broken at an arbitrary point, but only if it would overflow the line on its own. |
anywhere |
Like break-word, but the break opportunities also count when the browser calculates the minimum content size of the box. |
Usage notes
break-wordvsanywhere. The visible result in a normally sized box is the same. The difference appears in shrink-to-fit contexts (flex items, grid tracks withauto, table cells): withanywherethe box's minimum width can shrink down to a single character, whilebreak-wordstill reports the full word width, so the box may stay wide.- Flex and grid items. If a long word still overflows a flex item, the cause is usually
min-width: auto. Addmin-width: 0to the item, or useoverflow-wrap: anywhere. - Compared with
word-break.word-break: break-allbreaks anywhere even when the word would fit on the next line.overflow-wraponly breaks when it must, which looks more natural. - Hyphenation is separate.
overflow-wrapnever inserts a hyphen. Usehyphensfor dictionary-based hyphenation. - Good defaults. Setting
overflow-wrap: break-wordon user-generated content (comments, chat, table cells) is a cheap way to protect a layout.
Browser support
break-word is supported everywhere; anywhere is also supported in current versions of all major browsers.