I use the following css to make the width of each table column to fit the width of the widest cell in that column (of course, I can't use table-layout:fixed
in such case):
.fit {
white-space: nowrap;
width: 1%;
}
Now, I have a case where a cell content may be changed dynamically, and the changed content may be longer the original.
In such a case, is it possible to keep the original width of the column that holds the changed cell? The part of the content that "overflows" should be hidden.
E.g., in the following example (here's also a fiddle), when the button is clicked and the content of the last cell is changed to a longer text, I'd like to keep the original width of the last column:
$('#button').click(ChagneLastWord);
function ChagneLastWord() {
var lastCell = $("#myTable tr").first().find("td").last();
lastCell.text("loooooooooooooooooooooooooooooooooooooooooooooooong word");
}
table {
width: 100%;
}
td {
text-align: center;
vertical-align: middle;
white-space: nowrap;
}
.fit {
white-space: nowrap;
width: 1%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="button">ChangeLastWord</button>
<div id="tableWrapper">
<table id="myTable" class="fit">
<tr>
<td>some</td>
<td>nice</td>
<td>word</td>
</tr>
<tr>
<td>some</td>
<td>nice</td>
<td>word</td>
</tr>
</table>
</div>