106
<td>gdfggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg</td>

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

keruilin
  • 15,808
  • 32
  • 106
  • 174

5 Answers5

134

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
  • 1
  • 16
Stirling
  • 4,188
  • 3
  • 22
  • 16
106

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
  • 1
  • 16
Gaurav Saxena
  • 4,237
  • 3
  • 18
  • 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
  • 638
  • 7
  • 20
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
  • 857,932
  • 118
  • 1,152
  • 1,264
  • 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,514
  • 1
  • 40
  • 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