I have a table with a fixed width and I want the size of all columns to be automatically adjusted to their content. However, if there is little content for the columns, I want only the last column to fill the remaining space.
So basically, I want the table to behave like a normal table with no fixed width. In cases where the table does not fill the maximum width of the parent, the last column should expand.
Furthermore, the table should have several rows, where the cells of the same column are exactly below each other.
Here are some examples of how the table is supposed to look:
-----------------------------------------------------------------------
| Lorem ipsum | Lorem ipsum | Lorem ipsum |
-----------------------------------------------------------------------
-----------------------------------------------------------------------
| Lorem ipsum dolor sit amet | Lorem | Lorem ipsum dolor sit |
-----------------------------------------------------------------------
-----------------------------------------------------------------------
| Lorem ipsum dolor sit | Lorem ipsum dolor sit | Lorem ipsum dolor sit |
-----------------------------------------------------------------------
-----------------------------------------------------------------------
| Lorem ipsum dolor sit | Lorem ipsum dolor sit | Lorem ipsum dolor sit |
| amet, consetetur | amet, consetetur | amet, consetetur |
-----------------------------------------------------------------------
The best solution I have found so far (other question on stackoverflow) uses width: 100%;
on the table and the last cell and white-space: nowrap;
on all cells. Problem with the solution is that the table becomes wider than it should because of the white-space: nowrap;
when there is a lot of text, as you can see in the example.
div {
width: 500px;
background-color: #cccccc;
padding: 10px;
}
table {
border-collapse: collapse;
width: 100%;
}
td {
border: 1px solid black;
padding: 5px;
white-space: nowrap;
}
td:last-child {
width: 100%;
}
<div>
<p>This looks good:</p>
<table>
<tr><td>Lorem ipsum</td><td>Lorem ipsum</td><td>Lorem ipsum</td></tr>
</table>
<p>This doesn't:</p>
<table>
<tr>
<td>Lorem ipsum dolor sit amet, consetetur</td>
<td>Lorem ipsum dolor sit amet, consetetur</td>
<td>Lorem ipsum dolor sit amet, consetetur</td>
</tr>
</table>
</div>
2
Answers
If you could use JS:
Usually I wrap the cell content into a span and have freedom to do any formatting/positioning on the content/cells:
Try flex. The key here is setting the
flex-grow
property of the last cell in a row to a high number (it defaults to 0).