An easy way to do this would be to use CSS columns instead of flex .
Just use column-width equal to the width of the container. Apply break-inside: avoid to the child div s. And there you are.
It resolves all requests:
[..] have a fixed container of width and height, which consists of arbitrary height elements that must be stacked vertically. How can I hide elements that do not fit?
You may notice that the red div (last) is completely hidden.
Fragment example:
* { box-sizing: border-box; margin: 0; padding: 0; } .container { border: 1px solid #999; height: 200px; width: 300px; overflow: hidden; column-width: 300px; } .box { padding: 8px; text-align: center; color: #fff; width: 250px; height: 80px; break-inside: avoid } .box:nth-child(1) { background: #3b3; } .box:nth-child(2) { background: #33b; width: 200px; height: 75px; } .box:nth-child(3) { background: #b33; }
<div class="container"> <div class="box">show</div> <div class="box">show</div> <div class="box">hide</div> </div>
Note. . At the moment, Firefox is still a problem area with CSS columns. break-inside , although documented in MDN , is erroneous in Firefox. The error is still open: https://bugzilla.mozilla.org/show_bug.cgi?id=549114 .
source share