PHP or HTML / CSS solution to force line breaks in a table

I have a table populated with user generated text. The text covers as much as TD allows, however, if the input is a line from a long line without breaks (space, dash, etc.), it will ruin the table.

eg. ggggggggggggggggggggggggggggggggggggggggggggggggggggggg

How can I make these lines also wrap around?

Thanks.

+4
source share
2 answers

You can use CSS:

table { table-layout: fixed; width: 100%; } table td, table th { word-wrap: break-word; /* Internet Explorer 5.5+, Chrome, Firefox 3+ */ overflow-wrap: break-word; /* CSS3 standard: Chrome & Opera 2013+ */ } /* Use this if you also want to preserve multiple spaces in text */ table td, table th { white-space: -moz-pre-wrap; /* Firefox 1.0-2.0 */ white-space: -pre-wrap; /* Opera 4-6 */ white-space: -o-pre-wrap; /* Opera 7 */ white-space: pre-wrap; /* CSS3 */ } 

Here is an example: http://www.jsfiddle.net/QPP8A/ (now deprecated, sorry)

If you find this difficult to apply, you can use the wordwrap PHP function :

 $text = "The quick brown fox jumped over the lazy dog."; $newtext = wordwrap($text, 20, "<br />\n"); 
+9
source

try this jquery solution, it will break the text after a certain number of letters:

http://plugins.jquery.com/project/Word-Break

You can use it like that.

 $('yourtable td').wordwrap({ width: 50, cut:true, brk: '<br/>\n' }) 

thanks

+2
source

All Articles