Smarter word break in CSS?
If I just put word-break: break-all
on an element, I often end up with this:
Hello people, I am typing a mes
sage that's too long to fit!
Obviously this would be much better as:
Hello people, I am typing a
message that's too long to fit!
But at the same time if someone writes:
BLAAAAAAAAARRRRRRRRRRRRGGGGGGGGHHHHHHHH!!!!!!
Then I'd want it to be:
BLAAAAAAAAARRRRRRRRRRR
RGGGGGGGGHHHHHHHH!!!!!!
I can't seem to find a way to actually do this.
Note that the width of the element is not fixed and may change.
Solution 1:
Try word-break: break-word;
it should behave as you expect.
Solution 2:
For a lot of our projects we usually add this where necessary:
.text-that-needs-wrapping {
overflow-wrap: break-word;
word-wrap: break-word;
-ms-word-break: break-all;
word-break: break-word;
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
}
It handles most odd situations with different browsers.