I have an HTML table 360px wide, which works great. The challenge is that sometimes a url appears http://this/is/a/really-really-really-really-really/long/url
in the text. This causes the table to expand horizontally and a scroll bar appears on the bottom.
I don't think overflow:hidden
will work because half of the text would be hidden.
What is the best way to force breaking the line on slashes (/) and dashes (-) in CSS (hopefully)?
It should work with IE7+, Chrome, Firefox and Safari.
Working in Rails 3 and jQuery.
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. If we then set the width of the text to one em , the word will break by each letter: HTML.
The <wbr> element If you know where you want a long string to break, then it is also possible to insert the HTML <wbr> element. This can be useful in cases such as displaying a long URL on a page.
The <br> HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant.
While the css word-wrap: break-word;
does work, its implementation is different across browsers.
If you have control of the content and want exact breakpoints, you can insert
<wbr>
word break (supported in all major browsers except IE8 CanIUse.com); ​
zero-width space (U+200B) - ugly in IE<=6­
soft hyphen - though of course this adds a hyphen when breaking which is not always what is desired.I have a large corporate user base who still have to use IE8, so when I hit this problem I used the C# someString.Replace("/", "/​")
in the server-side code.
Gotcha: If you insert a zero-width space in an email address, when a user copies and pastes into their email client, the space gets copied too and the email will fail with no way for a user to see why (the space is zero width ...)
You can use word-wrap : break-word;
like so:
<div>http://www.aaa.com/bbb/ccc/ddd/eee/fff/ggg</div> div { width : 50px; border : 1px solid #000; word-wrap : break-word; }
I tested this in: I.E. 6/7/8, Firefox 7, Opera 11, Safari 5, Chrome 15
Here is a jsfiddle: http://jsfiddle.net/p4SxG/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With