There are times when a really long string of text can overflow the container of a layout.
For example:
Here’s a big snippet with all the CSS players involved:
.dont-break-out {
/* These are technically the same, but use both */
overflow-wrap: break-word;
word-wrap: break-word;
-ms-word-break: break-all;
/* This is the dangerous one in WebKit, as it breaks things wherever */
word-break: break-all;
/* Instead use this non-standard one: */
word-break: break-word;
/* Adds a hyphen where the word breaks, if supported (No Blink) */
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
}
That would fix the issue for us:
Here’s the scoop:
overflow-wrap: break-word;
makes sure the long string will wrap and not bust out of the container. You might as well useword-wrap
as well because as the spec says, they are literally just alternate names for each other. Some browsers support one and not the other. Firefox (tested v43) only supportsword-wrap
. Blink (tested Chrome v45) will take either one.- With
overflow-wrap
in use all by itself, words will break kinda anywhere they need to. If there is an “acceptable break” character (like a literal dash, for instance), it will break there, otherwise it just does what it needs to do. - You might as well use
hyphens
as well, because then it will try to tastefully add a hyphen where it breaks if the browser supports it (Blink doesn’t at time of writing, Firefox does). word-break: break-all;
is to tell the browser that it’s OK to break the word wherever it needs to. Even though it kinda does that anyway so I’m not sure in what cases it’s 100% necessary.
If you want be more manual with hyphens, you can suggest them in your markup. See more on the MDN page.
Browser support
word-break
:
For 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 |
---|---|---|---|---|
44 | 15 | 5.5 | 12 | 9 |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
115 | 115 | 115 | 9.0-9.2 |
hypens
:
For 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 |
---|---|---|---|---|
88 | 6* | 10* | 12* | 5.1* |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
115 | 115 | 115 | 4.2-4.3* |
overflow-wrap
:
For 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 |
---|---|---|---|
115 | 115 | 4.4 | 7.0-7.1 |
text-overflow
:
For 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 |
---|---|---|---|---|
4 | 7 | 6 | 12 | 3.1 |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
115 | 115 | 2.1 | 3.2 |
Preventing Overflow with Ellipsis
Another approach to consider is truncating the text altogether and adding ellipses where the string of text hits the container:
.ellipses {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
This nice thing about using text-overflow
is that it is supported universally.
Examples
See the Pen Hyphenate Long Words by CSS-Tricks (@css-tricks) on CodePen.
See the Pen Ellipses by CSS-Tricks (@css-tricks) on CodePen.
See the Pen Figuring Out Line Wrapping by Chris Coyier (@chriscoyier) on CodePen.
More Resources
- Michael Scharnagl: Dealing with long words in CSS
- Kenneth Auchenberg: Word wrapping/hyphenation using CSS
- MDN: word-wrap, word-break, hyphens
- Spec: CSS Text Level 3
For the SCSS-inclined
These tend to be the kind of things you sprinkle into code where needed, so they make for nice @mixins:
@mixin word-wrap() {
overflow-wrap: break-word;
word-wrap: break-word;
-ms-word-break: break-all;
word-break: break-all;
word-break: break-word;
-ms-hyphens: auto;
-moz-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
}
@mixin ellipsis() {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Apparently,
overflow-wrap
is the newword-wrap
as its been removed from the CSS3 spec. I just read about this only a couple of days ago, along with some other interesting proposed typographic properties:http://www.impressivewebs.com/new-css3-text-wrap/
Worth noting: although word-wrap has been removed from spec there is no current browser support for the replacement property!
But before you go rushing into changing all that word-wrap goodness you just added – from the spec: “For legacy reasons, UAs may also accept ‘word-wrap’ as an alternate name for the ‘overflow-wrap’ property.”
the surrounding element must have a specified width or your long url will still break out of the box.
You solved a problem I’ve been having. Awesome!
Thanks agentsuperdave, this tip was extremely useful. For a table, that means you should use “table-layout: fixed” and specify widths for your tds.
Quentin, I love you man, you saved my ass! ;)
Btw. I didn’t declare width, just “table {table-layout: fixed; width: 100%}” and “table a {word-wrap: break-word}”.
I didn’t declare “td” width, just…
Yeah, it works too. In my case I needed a different width for each column.
Great thanks for that, I had been looking for a solution for word-break in FF for like an hour. Cheers
Your robust solution fixed my issue, my URL did not want to break while inside a table styled to be responsive (display:block). Cheers!
fix example from
to
If these options dont’ give you enough control, see this question on SO, especially two of my answers (;-), http://stackoverflow.com/a/6298738/736006 and http://stackoverflow.com/a/6508168/736006.
Thanks man!
I tried using this :
// Given on the top post
-ms-word-break: break-all;
word-break: break-all;
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
but was not working.
Until i added
float:left and a width.
Now it is working perfectly fine.
This is working fine in all browsers.
I tried all these solutions and it works perfectly in Safari, Firefox and IE but it does not work in the newest Chrome.
The one I ended up using now is this from the article:
The article says it is supposed to work in Chrome. Does anyone know what the issue might be?
If you are using Compass, there is a mixin available that includes all of this code:
Thanks for the tip @Damon. Sadly I am not using Compass and do not have the option to use it at the company I am working.
But does the code in the mixin include code that works for Chrome aswell?
Thanks this was really useful and helped me fix an issue with links not wrapping in the prefooter area of my Drupal site. You may be interested in checking out the result here in the third prefooter area: http://www.mtusesthis.com/
This is wrapping every word, so even if I have say “teams” at the end of a line and it does not quite fit, the “s” is pushed to the next line. I only want words wrapped that are longer than the width of my div. Am I doing something wrong here? Is there something else I can do to achieve this? Thanks.
I think you should think about including a js library to get such a refinement – e.g.: https://code.google.com/p/hyphenator/
it has a setting to get that behaviour: minwordlength : 10
Awesome! It works like a charm ^.^, thanks for tip!
And the too long email-adresses??
Really, for more control, check out the Stack Overflow posts I referred to above. Works very well, if js is an option.
Dave is right, and actually, js should be an option, since we’re talking about – essentially – aesthetics
Thanks Chris! I was scratching my head on this one!
Worked like a charm in FF, Chrome and Safari for me, didn’t check IE
I’m working with DocBook, it outputs to a PDF. My table’s cell’s content is not word wrapping with words that are long and have no spaces. I believe our css is a docbook_custom.xsl Where would I set these word wrap adn break statement?
Very useful, indeed. Thank you.
The fact that
break-all
breaks words in weird places was a no-no for me, so I removed it and instead addedword-wrap: break-word
.My mixin now looks like this:
It seems to be working OK in Chrome, Safari, Firefox, IE9-10, unless I’m missing something.
Your solution does seem to work better than the article’s solution. The problem I had was that the super long words were broken to the next line as expected but a smaller word that could fit on the next one would be also broken instead of simply be “pushed” to the next line.
see the “bad” one (Chris’ solution), notice the “d” is cut: http://imgur.com/avo2kxS,i0aqqwX#1
see the good one (Sugarenia’s solution): http://imgur.com/avo2kxS,i0aqqwX
Thanks! Was looking for it.
great! pointed me in the right direction when encountering a layout issue due to long words / strings / text. thanks.
PHP and HTML5 to the rescue:
$url = str_replace(‘/’,’/<WBR>’,htmlentities($url))
of course, don’t do this to the url in the HREF, just the one that is displayed.
Thanks a lot for the tip. Worked using a long word within %width table
Thank you very much. I have a table that spans 100% screen width, the first two columns contain timestamps and nicknames (the latter being of variable width) and the third contains text and should fill the available space. Long URLs however kept adding a horizontal scroll bar because they extended the table beyond 100% screen width. Your CSS made the text column wrap text exactly when it should, not more (break between any two characters) or less (live with horizontal scrolling). Thank you.
If all these suggestions fail, make sure your long text is NOT affected by { white-space: nowrap } (facepalm)
Ellipsis works for single-line truncating. However, there’s not yet a good way to truncate multi-line elements. For example, suppose you text that spans 3 (or more) lines, but you want to allow up to 2 lines of text before displaying the ellipsis. There’s no good way to do this.
Here’s a bunch of ways: https://css-tricks.com/line-clampin/
Possibly try this JavaScript solution:
It’ll crop to a set number of lines, add an ellipsis, and responsively at that. It also accessibly expands the copy onclick but that feature could be edited out.
We’ve used it with great success on Tesco Food Love Stories:
https://www.tesco.com/food-love-stories/
I had seen that already, but as you wrote in that article: “There are a couple of ways to get it done, none of them spectacular.” I was going to look at the Clamp.js way, but the example in your article seems to not be working, and that deterred me from investigating further. I just wish there was a standard way for doing this.
It’s worth noting that Chrome behaves differently to IE 11 and Edge when using on inline elements within a table.
Chrome works as expected however IE 11 and Edge require the mixin class to be applied to the ‘td’ itself
Hyphenation is a good technique. While Ellipsis has cross-browser issues.
I always end up coming to this site about 90% of the time for WORKING css answers. It pisses me off to no end how many other sites give advice and their solutions don’t work. StackOverflow is the biggest culprit. Very good stuff on wrapping long text. We will be using it at our site.
@Dave Murray: so TRUE :D
Nice Mixins, awesome!
Only thing that works for me in all browsers is
word-break: break-word;
Doh, sorry, typo. Make that
word-break: break-all;
When using flex, it will sometimes not work.
You will need to use
On the block, in addition of the
break-word
rules.Per this CodePen:
http://codepen.io/anon/pen/YWZWJV
I can’t get a url only to wrap in a table cell in IE. As noted here:
IE expects the css to be applied to the td and/or the table, which wraps all of the text therein, not just the url. Does anybody know a way around this?
Perfect solution. The break-word vs break-all was killing me, in a dynamically sized div with display:table-cell;
Works brilliantly. Just used it like this on a new web site with a long email address.
This is awesome! Thanks for your work Chris!
Chris, thanks for the tips! At the time of writing, Chrome still doesn’t properly support Hyphen so the wrap/break solution is still necessary. Firefox is happy to break long urls at the / char using just Hyphen.
Thank you very much! It was so helpful, I even left a comment ;-)
Good morning comrades. It’s very possible that I missed it, but is there a way to set this up so that it kicks in only when the screen is a certain size? For instance when a title or a heading displays fine on desktop and tablet, but runs off the screen to the right on a smartphone? When that happens it would be great to have something that reduces the point size of the element in question.
Just what I needed! Thank you so much!
Just fought with this today: there’s an issue in IE11 & Edge – if you have
display: flex
on the container that holds the text with a long URL / word, it won’t wrap. This pen shows a reduced demo: https://codepen.io/jdsteinbach/pen/JBZrLR?editors=1100Hey Chris! According to the Chicago Manual of Style, “To avoid confusion, an address that contains a hyphen should never be broken at the hyphen; nor should a hyphen be added to break an e-mail address or URL.”
If following this advice,
hyphen: auto
should only apply to long words since it would add hyphens to force a break. But even if you stop targeting URLs withhyphen: auto
, they may still break if they contain hyphens. And that can still cause confusion because it isn’t clear whether the hyphen is part of the URL or was added to hyphenate the paragraph.According to the latest version of Text Module Level 3, this is possible with
hyphen
property’s ‘none’ value which is defined as meaning, “Words are not hyphenated, even if characters inside the word explicitly define hyphenation opportunities”CSS Text Level 3 does not define the exact rules for hyphenation or what a hyphenation opportunity is, but Unicode,org does, “spaces and hyphens are used to determine breaks” Unfortunately,
hyphen: none
will still break words at hyphens (an explicitly defined hyphenation opportunity) even though the spec says it shouldn’t.I’m trying to fit long file names with:
overflow: hidden;
text-overflow: ellipsis;
width: 70%;
and it works good if the file names are consisted of letters, spaces or “_”, but if there is “-” word breaks in many lines and ellipsis is not implemented.
Is there a way to overcome that?
Thanks in advance :)
In Internet Explorer 11 and in Microsoft Edge 42.17134.1.0 , I have text 9999\XY5514Z to display in a div tag. Due to backslash “\” within the mentioned text, the text gets split into 9999 and XY5514Z. The same code works as expected in chrome and Firefox. I have word-break: normal; in css and expect to break complete “9999\XY5514Z” to next line and not all the text after “\”, if not enough space in the line where the word is rendered. In Internet Explorer 11 and in Microsoft Edge 42.17134.1.0, the result is displayed as 9999 and then remaining text on next line \XY5514Z. In Chrome and Firefox, the text is displayed as it is 9999\XY5514Z which is correct. I don’t want the word to break on “\” backslash in IE and Edge.
What about verylong emails like: [email protected]?
Thanks! You helped me a lot
See also: the
<wbr>
tag.MDN gives a thumb down to “word-break: break-word;”, but that fixed my problem where nothing else suggested here helped, until I added that to my paragraph styling.
Thank you, this has been very helpful. I struggled to have the URLs stay within the frame of my HTML emails. I added this CSS and worked like a charm. Thanks a lot!
thanks a lot :) , i was searching how to fix this for hours ! , in worpdress rtl with the english links breaks the page width , it seems the theme developer added only ” word-wrap: break-word; ” and it worked fine in firefox but not in chrome , so after adding overflow-wrap: break-word; it looks fine now.
Wow! Thanks a lot!! This solved my url overflow issue!