I have some tables on an HTML page where some cells need to be limited in width. Any text that's too long needs to be cut off with ellipsis (...) to show that there is more text.
A great job for text-overflow: ellipsis
, I would say!
My example looks like this:
<html>
<head>
<title>Test table ellipsis</title>
<style>
table th,
table td {
border: 1px solid red;
}
table .col-A {
width: 25%;
}
table .col-B {
width: 75%;
}
table .col-B div {
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}
</style>
</head>
<body>
<table>
<thead>
<th class="col-A">Column A</th>
<th class="col-B">Column B</th>
</thead>
<tbody>
<tr>
<td class="col-A">Value A1</td>
<td class="col-B">Value B1</td>
</tr>
<tr>
<td class="col-A">Value A2</td>
<td class="col-B">
<div class="col-B">Value B2 that is going to be ellipsized, because it's way too long!</div>
</td>
</tr>
</tbody>
</table>
</body>
</html>
At first sight this looks like it works. But the more you look at it, the more it falls apart:
- I specified column widths 25% and 75%. This is obviously not the case. Changing the long text directly influences the column widths
- The text is "ellipsized", but there's a lot of room left. Why doesn't it use that free space (marked orange below in the screenshot)?
I'm really looking for a solution that works nicely with a table (since the data it's showing is really tabular and I have a ton of existing CSS that assumes tables are used). But at this point I'm also open to solutions that use some combination of div and flexbox.