我在固定的父 div 中有 5 个子 div,其高度因设备而异。我正在寻找任何 hack 来隐藏整个 div,如果它不适合固定的父 div。
我试过“溢出:隐藏;”但它只裁剪溢出的部分而不是整个 div
.fixed-div {
background: greenyellow;
height: calc(100vh - 10px);
width: 100px;
position: fixed;
overflow: hidden;
}
.child-div {
width: 60px;
height: 60px;
background: red;
margin: 20px auto;
}
<div class="container">
<div class="fixed-div">
<div class="child-div">
</div>
<div class="child-div">
</div>
<div class="child-div">
</div>
<div class="child-div">
</div>
<div class="child-div">
</div>
<div class="child-div">
</div>
<div class="child-div">
</div>
</div>
</div>
预期:如果高度只能容纳2个子div,则不应显示或完全裁剪其他5个子div
最佳答案
这可以通过 CSS 网格来完成,方法是定义最大宽度和动态行数,以及定义 overflow: hidden 以隐藏没有空间的元素。看看这个:
.fixed-div {
display: grid;
grid-template-rows: repeat( auto-fit, minmax(100px, 1fr));
grid-template-columns: 100px;
grid-auto-flow: column;
background: greenyellow;
max-height: calc(100vh - 10px);
max-width: 100px;
overflow: hidden;
}
.child-div {
width: 60px;
height: 60px;
background: red;
margin: 20px auto;
}
<div class="fixed-div">
<div class="child-div">
</div>
<div class="child-div">
</div>
<div class="child-div">
</div>
<div class="child-div">
</div>
<div class="child-div">
</div>
<div class="child-div">
</div>
<div class="child-div">
</div>
</div>
关于html - 父div溢出时隐藏整个子div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57496605/