{"id":16145,"date":"2012-01-30T20:31:45","date_gmt":"2012-01-31T03:31:45","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=16145"},"modified":"2018-07-25T20:54:37","modified_gmt":"2018-07-26T03:54:37","slug":"prevent-long-urls-from-breaking-out-of-container","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/snippets\/css\/prevent-long-urls-from-breaking-out-of-container\/","title":{"rendered":"Handling Long Words and URLs (Forcing Breaks, Hyphenation, Ellipsis, etc)"},"content":{"rendered":"
There are times when a really long string of text can overflow the container of a layout.<\/p>\n
For example:<\/p>\n Here’s a big snippet with all the CSS players involved:<\/p>\n That would fix the issue for us:<\/p>\n Here’s the scoop:<\/p>\n If you want be more manual with hyphens, you can suggest them in your markup. See more on the MDN page<\/a>.<\/p>\n This browser support data is from Caniuse<\/a>, which has more detail. A number indicates that browser supports the feature at that version and up.<\/p><\/div>.dont-break-out {\r\n\r\n \/* These are technically the same, but use both *\/\r\n overflow-wrap: break-word;\r\n word-wrap: break-word;\r\n\r\n -ms-word-break: break-all;\r\n \/* This is the dangerous one in WebKit, as it breaks things wherever *\/\r\n word-break: break-all;\r\n \/* Instead use this non-standard one: *\/\r\n word-break: break-word;\r\n\r\n \/* Adds a hyphen where the word breaks, if supported (No Blink) *\/\r\n -ms-hyphens: auto;\r\n -moz-hyphens: auto;\r\n -webkit-hyphens: auto;\r\n hyphens: auto;\r\n\r\n}<\/code><\/pre>\n
\n
overflow-wrap: break-word;<\/code> makes sure the long string will wrap and not bust out of the container. You might as well use
word-wrap<\/code> as well because as the spec says<\/a>, they are literally just alternate names for each other. Some browsers support one and not the other. Firefox (tested v43) only supports
word-wrap<\/code>. Blink (tested Chrome v45) will take either one.<\/li>\n
overflow-wrap<\/code> 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.<\/li>\n
hyphens<\/code> 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).<\/li>\n
word-break: break-all;<\/code> 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.<\/li>\n<\/ul>\n
Browser support<\/h3>\n
For
word-break<\/code>:<\/h4>\n
Desktop<\/h4>