10

I have the folowing html:

<div class="box">
    long text here
</div>

and css:

.box {
    width: 400px;
    height: 100px;
    overflow: auto;
    border: 1px gold solid; 
}

I want only a vertical scroll. But when a word is too long, a horizontal scroll is displayed. How do I make the long words wrap ?

If needed, I can use a trick with jQuery or PHP, but I would like to solve it using CSS, because it's CSS job.

You can fiddle here: http://jsfiddle.net/879bc/1/

Burhan Ali
  • 2,258
  • 1
  • 28
  • 38
Benjamin Crouzier
  • 40,265
  • 44
  • 171
  • 236

3 Answers3

28
word-wrap: break-word

https://developer.mozilla.org/en/CSS/word-wrap

DA.
  • 39,848
  • 49
  • 150
  • 213
  • @Timeless working for me on OSX Chrome 36.0.1985.125 – DA. Jul 26 '14 at 21:42
  • @Timeless yea, `table > td` shouldn't actually match anything in your HTML (as TD is never a direct child of Table) – DA. Jul 26 '14 at 22:32
  • @DA. I mean I applied to a td element inside a table. – Timeless Jul 26 '14 at 23:02
  • 1
    @Timeless ah! Yes, looks like you need to set Table to use `table-layout: fixed`. See here: http://stackoverflow.com/questions/5889508/using-word-wrap-break-word-within-a-table – DA. Jul 27 '14 at 03:01
  • this works with fixed width but if you use bootstrap/responsive div(such as col -md-9 etc),is not working.. – erginduran May 27 '16 at 21:16
2

For custom word breaking, there is an html special character that is not so often used- &shy; (soft hyphen) - that will split words on 2 lines and insert a dash after the first part of the word if the word approaches the edge of its container. Trouble is, you'll have to place them everywhere you want them. As you say, though, you can also set up a js or php function, and insert them into the appropriate places.

uɥƃnɐʌuop
  • 14,022
  • 5
  • 58
  • 61
0

The solution I have used in the past is an ellipsis library like http://dotdotdot.frebsite.nl/ for jquery, you can specify the number of chars and have it dot dot dot after that so it all fits on one line.

rabs
  • 1,807
  • 3
  • 18
  • 29