<div class="container">
<div class="box-wrap">
<input type="checkbox">
<div class="box">
I've got an `auto` height so my height is generally unknown to CSS.
</div>
</div>
<div class="box-wrap">
<input type="checkbox">
<div class="box">
I've got also got an unknown height and I've got even more content just to demonstrate how this works for unknown sizes.
</div>
</div>
</div>
body {
height: 100vh;
margin: 0;
display: grid;
place-items: center;
}
.container {
display: flex;
align-items: flex-start;
gap: 2rem;
}
.box-wrap {
text-align: center;
}
.box {
width: 200px;
overflow: clip;
background: salmon;
height: calc-size(auto, size);
transition: 0.2s ease-in-out;
[type="checkbox"]:checked + & {
height: 0;
}
}
.box-2 {
height: 80px;
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.