The word-break
property in CSS can be used to change when line breaks ought to occur. Normally, line breaks in text can only occur in certain spaces, like when there is a space or a hyphen.
In the example below we can make the word-break
between letters instead:
.element {
word-break: break-all;
}
If we then set the width of the text to one em
, the word will break by each letter:
See the Pen Setting text vertically with word-break by CSS-Tricks (@css-tricks) on CodePen.
This value is often used in places with user generated content so that long strings don’t risk breaking the layout. One very common example is a long copy and pasted URL. If that URL has no hyphens, it can extend beyond the parent box and look bad or worse, cause layout problems.
See the Pen Fixing links with word-break by CSS-Tricks (@css-tricks) on CodePen.
Values
normal
: use the default rules for word breaking.break-all
: any word/letter can break onto the next line.keep-all
: for Chinese, Japanese and Korean text words are not broken. Otherwise this is the same asnormal
.
This property is also often used in conjunction with the hyphens property so that when breaks occur a hypen is inserted, as per the standard in books.
The full usage, with needed vendor prefixes, is:
-ms-word-break: break-all;
word-break: break-all;
/* Non standard for WebKit */
word-break: break-word;
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
Using these properties on the universal selector can be useful if you have a site with a lot of user-generated content. Although fair warning, it can look weird on titles and pre-formatted text (
).
Related
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 |
---|---|---|---|
123 | 124 | 4.4 | 7.0-7.1 |
Safari and iOS support the break-all
value but not keep-all
It seems that “webkit-hyphens: auto; -moz-hyphens: auto; hyphens: auto;”all has no effect. And is there hyphens property in the css specification?
I have the same problem too Jiayang…
you have not typed
-webkit-hyphens: auto;
instead you typed
webkit-hyphens:auto;
that’s where your error is. if you make an error in css all the following styles are disregarded
chrome does not supported it.
http://caniuse.com/css-hyphens
Apparently IE10 supports prefixed hyphens property, so a more complete one would be:
IE8 introduced -ms-word-break as a synonym for word-break. Don’t use the -ms- prefix.
^ From: https://developer.mozilla.org/en-US/docs/Web/CSS/word-break
Apparently the support in Firefox should be nope, instead of yep.
Firefoc 41.0.2 on OSX 10.11 El Capitan does not show a linebreak in your example. Safari and Chrome do.
same here, does not work in firefox on mac!
Does not work on Firefox on Windows either.
shut up
note supported hyphens ie10
The example above for hyphenation is incorrect.
For Firefox to support automatic hyphenation the word-break behavior must be set to normal.
word-break: normal
Hyphenation is not supported in Chrome. You can break words in Chrome, without breaking Firefox by using the Chrome-specific notation
word-break: break-word
.Check out this codepen in both Firefox and Chrome
Is there a way to have a word wrap only if it overflows the line? Instead of just having every word at the end of the line break, regardless of length?
This code works for me wonderfully.
white-space: pre-wrap; /* css-3 /
white-space: -moz-pre-wrap; / Mozilla, since 1999 /
white-space: -pre-wrap; / Opera 4-6 /
white-space: -o-pre-wrap; / Opera 7 /
word-wrap: break-word; / Internet Explorer 5.5+ */
In Firefox word-break is not working for inline elements
CSS:
.break {
word-break: break-all;
display:block;
}
The “word-break : break-all” parameter is also useful for writing in Runic script, which allows line breaks within words.
Does not work with periods :(
thanks! this solve the middle word cut off problem that I had ;)
I’m still getting a fair amount of traffic to an old answer on Stack Overflow https://stackoverflow.com/questions/8186743/what-is-the-best-way-to-break-html-text-on-slashes/21780096#21780096 regarding breaking URLs on slashes …
… any thoughts to add in 2021?
tl;dr in 2014 I suggested inserting a
<wbr>
or
zero-width space (which can break when copied and pasted into browser)I’d like to react this:
Our website has those 3 languages (with English and French). We had complains from our Japanese and Chinese friends regarding using
keep-all
. Korean feedback is that it is better withkeep-all
.So my advise would be to use it for korean only. I implement it using the :lang pseudo-class (it requires a correct
lang
attribute of thehtml
tag).