107
<td>gdfggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg</td>

How do I get text like this to wrap in CSS?

keruilin
  • 16,782
  • 34
  • 108
  • 175

5 Answers5

137

If you type "AAAAAAAAAAAAAAAAAAAAAARRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGG" this will produce:

AARRRRRRRRRRRRRRRRRRRR
RRGGGGGGGGGGGGGGGGGGGG
G

I have taken my example from a couple different websites on google. I have tested this on ff 5.0, IE 8.0, and Chrome 10. It works on all of them.

.wrapword {
    white-space: -moz-pre-wrap !important;  /* Mozilla, since 1999 */
    white-space: -pre-wrap;      /* Opera 4-6 */
    white-space: -o-pre-wrap;    /* Opera 7 */
    white-space: pre-wrap;       /* css-3 */
    word-wrap: break-word;       /* Internet Explorer 5.5+ */
    white-space: -webkit-pre-wrap; /* Newer versions of Chrome/Safari*/
    word-break: break-all;
    white-space: normal;
}

<table style="table-layout:fixed; width:400px">
    <tr>
        <td class="wrapword"></td>
    </tr>
</table>
Ben Kalsky
  • 148
  • 2
  • 16
Stirling
  • 4,308
  • 3
  • 23
  • 16
  • 2
    I added "word-wrap: break-word;" but didnt work neither in Firefox nor in IE(I tested only in these two)... n when I added all the option you suggested.. it worked... Thanks!!! – UID Dec 11 '13 at 21:15
  • 2
    I had to add "white-space: -webkit-pre-wrap;" to get new versions of Chrome (and probably Safari) to work. – George Oct 12 '14 at 01:02
  • thanks, it worked! bootstrap was doing a thing on its own, and nothing helped. Finally the right solution for me. – 3xCh1_23 Aug 18 '16 at 15:26
  • `word-break: break-all;` works – onmyway133 Jun 22 '20 at 22:34
107

Try doing this. Works for IE8, FF3.6, Chrome

<body>
  <table>
    <tr>
      <td>
        <div style="word-wrap: break-word; width: 100px">gdfggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg</div>
      </td>
    </tr>
  </table>
</body>
Ben Kalsky
  • 148
  • 2
  • 16
Gaurav Saxena
  • 4,257
  • 3
  • 19
  • 17
1

This will work everywhere.

<body>
  <table style="table-layout:fixed;">
  <tr>
    <td><div style="word-wrap: break-word; width: 100px" > gdfggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg</div></td>
  </tr>
  </table>
 </body>
Benj
  • 736
  • 7
  • 21
1

With text-wrap, browser support is relatively weak (as you might expect from from a draft spec).

You are better off taking steps to ensure the data doesn't have long strings of non-white-space.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 13
    "To avoid some display error, please do not input long words." Is that really your answer? – SandRock Jan 05 '14 at 21:38
  • 2
    @SandRock — No, it isn't. That is a mischaracterisation of *half* of this answer. The data in the question, while being a "long string of non-white-space" was not a "word". "taking steps" also doesn't mean "don't input", we don't know where the data is coming from, it could mean testing user input for sanity and displaying an error message for inappropriate content. – Quentin Jan 05 '14 at 21:40
0

The better option if you cannot control user input, it is to establish the css property, overflow:hidden, so if the string is superior to the width, it will not deform the design.

Edited:

I like the answer: "word-wrap: break-word", and for those browsers that do not support it, for example, IE6 or IE7, I would use my solution.

netadictos
  • 7,602
  • 2
  • 42
  • 69
  • 2
    If you're going to use this option be sure to set a width for the element containing the text or overflow:hidden will simply hide the overflow, not wrap it. – jay Oct 16 '10 at 17:03