Reputation: 1268
Here is my code sample
<td width="40%">Field One</td>
<td width="30%">THISISAVERYLONGTEXTSOMEMORECHARACTERSTOMAKEITALITTLELONGERTHISISJUSTFORTESTING</td>
<td width="30%">Field Three</td>
Unable to bring the long text to next line I have also used these following methods in the <td>
tag. but same result.
These are :
style="width:200px;"
style="width:100px; overflow:hidden"
style="word-wrap:break-word;
I don't want to scroll the text. Please suggest some option.
Upvotes: 1
Views: 324
Reputation: 66398
CSS might not be cross browser enough or cause some side effects. For those who want alternative, here is pure JavaScript way to force spaces into the long string, thus wrapping it as well.
var cellID = "cell1";
var whatToPush = " ";
var pushAfter = 10;
window.onload = function() {
var oCell = document.getElementById(cellID);
var oTable = oCell.parentNode;
while (oTable && oTable.nodeName.toLowerCase() !== "table")
oTable = oTable.parentNode;
if (!oTable) {
alert("Fatal error: " + cellID + " is not a table cell");
return;
}
var availableWidth = document.body.offsetWidth;
var tableWidth = oTable.offsetWidth;
var counter = 0;
while (tableWidth > availableWidth) {
if (counter > 100) {
alert("Warning: word wrap forcing took too many iterations");
break;
}
var rawHTML = oCell.innerHTML;
var index = rawHTML.length - (pushAfter * (counter + 1));
if (counter > 0)
index -= whatToPush.length;
rawHTML = rawHTML.substring(0, index) + whatToPush + rawHTML.substring(index, rawHTML.length);
oCell.innerHTML = rawHTML;
tableWidth = oTable.offsetWidth;
counter++;
}
};
The constants in the beginning are pretty much intuitive, it works on a single cell given its ID and pushing some fixed string (e.g. blank space but can also be different string) after fixed amount of characters, counting backwards. (end of contents will split first)
Upvotes: 0
Reputation: 1605
hope it will help you
td {
padding: 0;
word-break: break-all;
text-align: center;
}
table{
width:100%
}
demo http://jsfiddle.net/nVZHp/3/
Upvotes: 1
Reputation: 21
You have a non-breaking text that can not be carried forward to the next line. If you want to move the text to the next line, insert a space in the line.
Upvotes: 0
Reputation: 632
Are you able to use a soft-hyphen at intervals during your long text string? For Example
<td width="40%">Field One</td>
<td width="30%">THIS­IS­A­VERY­LONG­TEXT­SOME­MORE­CHARACTERS­TO­MAKE­IT­A­LITTLE­LONGER­THIS­IS­JUST­FOR­TESTING</td>
<td width="30%">Field Three</td>
If required, the bowser will automatically split the string wherever you have the soft-hyphen and move the rest on to the next line.
Upvotes: 0
Reputation: 10168
The problem is word-wrap: break-word
doesn't work with elements that have display: table-cell
. That includes <td>
. You can't resolve it by adding word-wrap
directly to the td
but you can wrap its content with additional div
. The problem will still occur until you set a fixed width for the div
. I believe this is the only way to achieve this:
<td width="30%">
<div>
THISISAVERYLONGTEXTSOMEMORECHARACTERSTOMAKEITALITTLELONGERTHISISJUSTFORTESTING
</div>
</td>
Then the CSS:
td > div {
word-wrap: break-word;
width: 150px; /* or whatever else */
}
You can't achieve this when relying only on percentage widths. When using percent, the div
will still spread to fit its content in one line. The td
, then tr
and table
will follow.
Here's the DEMO: http://jsfiddle.net/nVZHp/2/
Upvotes: 0