I have a table filled with user generated text. The text spans for as much as the TD allows, however if the input is a string made of a long string with no breaking chars (white space, dash etc) it messes up the table.
e.g. ggggggggggggggggggggggggggggggggggggggggggggggggggggg
How can I make it so that those strings wrap as well?
Thank you.
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 out of date, sorry)
If you find this to hard to apply, you can use PHP's wordwrap function:
$text = "The quick brown fox jumped over the lazy dog.";
$newtext = wordwrap($text, 20, "<br />\n");
try this jquery solution it will break the text after number of letters defined:
http://plugins.jquery.com/project/Word-Break
You can use it like this.
$('yourtable td').wordwrap({
width: 50,
cut:true,
brk: '<br/>\n'
})
Thanks
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