According to this famous questions and good accepted answer: Why don't flex items shrink past content size? We can prevent the flex items to overflow their parent by setting a min-width of 0 on needed parents and overflow: hidden or auto.
I could verify this in other contexts, but I can't make it work within a table.
Here is a Codepen where I would like the .pagination
element to be constrained in the table width and not extending it:
https://codepen.io/antoniandre/pen/MWPjGGg?editors=1100
<div class="wrapper">
<table>
<thead>
<tr>
<th>first name</th>
<th>last name</th>
</tr>
</thead>
<tbody>
<tr>
<td>aa</td>
<td>aa</td>
</tr>
<tr>
<td>bb</td>
<td>bb</td>
</tr>
<tr>
<td>cc</td>
<td>cc</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2">
<div class="pagination">
<button>1</button>
...
<button>100</button>
</div>
</td>
</tr>
</tfoot>
</table>
</div>
.wrapper {
margin: auto;
max-width: 600px;
background-color: #fef;
}
table {
width: 100%;
}
tfoot, tfoot tr, tfoot td {
overflow: auto;
min-width: 0;
}
.pagination {
background-color: #efe;
padding: 8px;
display: flex;
overflow: auto;
min-width: 0;
}
There are lots of similar questions that I have seen Fitting child into parent, but not in a table, and this seems to be the issue.
I don't mind adding some wrappers in the markup or not using flex. Grid or inline-block can also be an option if it works without fixed max-width.
Here is a screenshot of how it looks like in its original context, not overflowing in that case.