DigitalOcean provides cloud products for every stage of your journey. Get started with $200 in free credit!
The overflow-wrap
property in CSS allows you to specify that the browser can break a line of text inside the targeted element onto multiple lines in an otherwise unbreakable place. This helps to avoid an unusually long string of text causing layout problems due to overflow.
.example {
overflow-wrap: break-word;
}
Syntax
overflow-wrap =
normal |
break-word |
anywhere
- Initial value:
normal
- Applies to: non-replaced inline elements
- Inherited: yes
- Computed value: as specified
- Animation type: discrete
Values
normal
: the default. The browser will break lines according to normal line breaking rules. Words or unbroken strings will not break, even if they overflow the container.break-word
: words or strings of characters that are too large to fit inside their container will break in an arbitrary place to force a line break. A hyphen character will not be inserted, even if thehyphens
property is used.inherit
: the targeted element must inherit the value of theoverflow-wrap
property defined on its immediate parent.
The demo below has a toggle button that allows you to switch between normal
and break-word
.
To demonstrate the problem that overflow-wrap
attempts to solve, the demo uses an unusually long word inside a relatively small container. When you toggle break-word
on, the word is broken to accommodate the small amount of space available, as if the word were multiple words.
A string of non-breaking space characters (
) would be treated the same way and would also break at an appropriate spot.
overflow-wrap
is useful when applied to elements that contain unmoderated user-generated content (like comments sections). This can prevent long URLs and other unbroken strings of text (e.g. vandalism) from breaking a web page’s layout.
word-break
property
Similarities to the overflow-wrap
and word-break
behave very similarly and can be used to solve similar problems. A basic summary of the difference, as explained in the CSS specification is:
overflow-wrap
is generally used to avoid problems with long strings causing broken layouts due to text flowing outside a container.word-break
specifies soft wrap opportunities between letters commonly associated with languages like Chinese, Japanese, and Korean (CJK).
After describing examples of how word-break
can be used in CJK content, the spec says: “To enable additional break opportunities only in the case of overflow, see overflow-wrap
“.
From this, we can surmise that word-break
is best used with non-English content that requires specific word-breaking rules, and that might be interspersed with English content, while overflow-wrap
should be used to avoid broken layouts due to long strings, regardless of the language used.
word-wrap
property
The historical overflow-wrap
is the standard name for its predecessor, the word-wrap
property. word-wrap
was originally a proprietary Internet Explorer-only feature that was eventually supported in all browsers despite not being a standard.
word-wrap
accepts the same values as overflow-wrap
and behaves the same way. According to the spec, browsers “must treat word-wrap
as an alternate name for the overflow-wrap
property, as if it were a shorthand of overflow-wrap
“. Also, all user agents are required to support word-wrap
indefinitely, for legacy reasons.
Both overflow-wrap
and word-wrap
will pass CSS validation as long as the validator is set to test against CSS3 or higher (currently the default).
Browser support
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
23 | 49 | 11 | 18 | 6.1 |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
127 | 127 | 4.4 | 7.0-7.1 |
More information
- CSS Text Module Level 3 Specification (W3C)
- Handling Long Words and URLs (CSS-Tricks)
- Word-Wrap: A CSS3 Property That Works in Every Browser (Louis Lazaris)
word-break
vs.overflow-wrap
(Stack Overflow)- How to Prevent Line Breaks Using CSS (DigitalOcean)
This example demonstrates more of
hyphens
thanoverflow-wrap
, as one can remove thewrap
properties and the result will remain the same. I tested on Safari and Firefox, and they both honourhyphens
, as opposed to what is stated in the article.Hyphens is a different issue altogether. The properties discussed here behave a certain way when hyphenation is not present.
Hyphenation could have been discussed here, but that would cover too much ground. This is part of the “Almanac”, which discusses one feature at a time. You can look up hyphens under its own article (which is linked in the “Related” section).
Very useful article. Helped me out.
The text says a hyphen will not be inserted; however there is a superficial hyphen visible when toggling word-wrap; could some explanatory text be added regarding this?
Not sure what you mean…? In what way is it there a hyphen visible?
Indeed what you and Yongwei Wu are saying is true. This example shows
hyphes: auto
in action! You can tell by the hyphen itself. A fact that Louis is failing to observe. That is because, the word Pneumonoultramicroscopicsilicovolcanoconiosis CAN be hyphenated by the hyphenation algorithm.But others shouldn’t – i.e. long strings of numbers, some special or repeated characters or mix of them, and for them hyphens won’t work (unless you are Safari, then expect unexpected..)
I is also true, that
overflow-wrap: break-word
would break the word in this example, as it will break everything but not necessarily in the same or correct way. And there will be no hyphen.When used together, you can expect even weirder mixed (although sometimes correct) behaviour.
Just to clarify something on this previous discussion:
The original demo uses
hyphens: auto
which I forgot about. Chrome still doesn’t supporthyphens: auto
. So Firefox adds the hyphen, Chrome does not. I think that’s why the original commenter was saying there is a hyphen visible. I was testing this in Chrome and did not realize Firefox had added that feature. So, yes, you can see a hyphen appear in some browsers.https://drive.google.com/open?id=1xy6gQGagGon77vfjL79GYCkzkM_v4Rba
So not sure if it’s my machine or what but… even tried >>all: unset;<< and it just refuses to wrap… Found tons of fiddles that just wraps by default… not sure what to do… any suggestions?
Hey Jacob! You might want to dig a little deeper with the browser’s DevTools. A super quick copy of your example seems to work out of the box, so maybe there’s something else at play in the rest of the CSS.
Still does not work in Chrome Version 79.0.3945.88 (Official Build) (64-bit)
I tested it and it works fine. If you can provide a link here to a demo where it’s not working, I’ll have a look.
Thank you! Really saved the day!
Unfortunately, I hit a use case with a grid where some cells just had a single overflowing word in them. “overflow-wrap: break-word;” did not work here as expected and did not break inside this single “word” (at least on a recent Chrome).
A quick research revealed “overflow-wrap: anywhere;” which then gave me the desired outcome of breaking this single oversized word. I would have though that “overflow-wrap: break-word;” would already achieve that … so what I’m overlooking that “anywhere” does differently?