我正在尝试将元素堆叠在一起。在我的情况下,我有一些具有不同高度的元素,这会导致不必要的空白。
我正在寻找6和7框,它们下面的所有对应元素都将向上移动。
.blockParent {
text-align: center;
position: relative;
width: 90%;
left: 5%;
}
.block {
position: relative;
display: inline-block;
width: 500px;
background-color: red;
height: 200px;
margin: 5px;
}
.smaller {
height: 100px;
vertical-align: top;
}
<div class="blockParent">
<div class="block">
</div>
<div class="block">
</div>
<div class="block smaller">
</div>
<div class="block smaller">
</div>
<div class="block">
</div>
<div class="block">
</div>
<div class="block ">
</div>
<div class="block">
</div>
<div class="block">
</div>
</div>
我认为flexbox可能是一个解决方案,但是我没有使用flexbox的经验。 有任何想法吗?预先感谢!
答案 0 :(得分:1)
Flexbox并非旨在解决此问题。您可以改用CSS多列。请注意,显示顺序从水平更改为垂直。
.blockParent {
column-count: 3;
}
.block {
display: inline-block;
vertical-align: top;
background-color: silver;
width: 100%;
height: 200px;
margin: 5px;
}
.smaller {
height: 100px;
}
<div class="blockParent">
<div class="block"></div>
<div class="block"></div>
<div class="block smaller"></div>
<div class="block smaller"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block "></div>
<div class="block"></div>
<div class="block"></div>
</div>
答案 1 :(得分:0)
.blockParent {
text-align: center;
position: relative;
width: 90%;
/* left: 5%; */
margin: 0 auto;
}
.block {
position: relative;
display: inline-flex;
width: 500px;
background-color: red;
height: 200px;
margin: 5px;
}
<div class="blockParent">
<div class="block">
</div>
<div class="block">
</div>
<div class="block smaller">
</div>
<div class="block smaller">
</div>
<div class="block">
</div>
<div class="block">
</div>
<div class="block ">
</div>
<div class="block">
</div>
<div class="block">
</div>
</div>
https://s.codepen.io/fasiya656/debug/NOrNQN/XxkVwaPeaooM