I'm creating a table to inspect tabular data (like result of SQL query) and trying to find the optimal layout.
I don't know the number columns or its content, I know only that some columns could be narrow and some wide. And that table should take no more than 100% of its container and columns should not wrap, it's ok to hide overflow. And it should utilise the available space in a reasonable way.
How it could be done? I tried table-layout: fixed
(without it table won't respect width: 100%
and takes wider space) but the layout din't utilise all the available space (see screenshot below). Are there better ways? Maybe involving some JS calculations (I'm using Svelte)?
One possible improvement would be - if the column is narrow, let it be narrow. But in the example below the narrow column "Aaa" still take the extra space it don't need.
Example:
table {
width: 100%;
table-layout: fixed;
}
table td {
white-space: nowrap;
}
table td {
overflow: hidden;
text-overflow: ellipsis;
}
<html>
<body>
<table>
<tr>
<td>Aaa</td>
<td>Bbb bbb bbb bbb bb bbbbbb bb bbbbbb bb bbbbbb bbbbbbbb bb bbbbbb bb bbbbbb bb bbbbbb bb</td>
<td>Ccc cc c ccccc c cccc</td>
</tr>
</table>
</body>
</html>
P.S.
Please note the white-space: nowrap;
- the solution for the similar question Table overflowing outside of div doesn't work with the white-space: nowrap;
option.