是否可以将固定边距(例如5px)应用于flexbox布局中的单元格,其中行空间均匀分布在其列中?
想要实现:
我当前的CSS只会在一行中对齐3列:
.row {
display: flex;
flex-flow: row wrap;
box-shadow: 0 0 0 1px green;
}
.col {
flex: 0 0 25%;
text-align: center;
box-shadow: 0 0 0 1px blue;
background-color: steelblue;
margin: 5px;
}
<div class="row">
<div class="col">
<div class="panel">1</div>
</div>
<div class="col">
<div class="panel">2</div>
</div>
<div class="col">
<div class="panel">3</div>
</div>
<div class="col">
<div class="panel">4</div>
</div>
<div class="col"><div class="panel">5</div></div>
</div>
答案 0 :(得分:6)
您可以将calc()用于flex-basis
长度(在您的情况下 - 它是简写属性flex
),10px
因为margin-left: 5px
和margin-right: 5px
}:
.row {
display: flex;
flex-flow: row wrap;
box-shadow: 0 0 0 1px green;
}
.col {
flex: 0 0 calc(25% - 10px);
text-align: center;
box-shadow: 0 0 0 1px blue;
background-color: steelblue;
margin: 5px;
}
<div class="row">
<div class="col">
<div class="panel">1</div>
</div>
<div class="col">
<div class="panel">2</div>
</div>
<div class="col">
<div class="panel">3</div>
</div>
<div class="col">
<div class="panel">4</div>
</div>
<div class="col">
<div class="panel">5</div>
</div>
</div>