使用具有固定尺寸边距檐槽的flexbox的网格列

时间:2016-01-05 10:58:17

标签: html css css3 flexbox grid-layout

是否可以将固定边距(例如5px)应用于flexbox布局中的单元格,其中行空间均匀分布在其列中?

想要实现:

this is the desired layout

我当前的CSS只会在一行中对齐3列:

JSFiddle

.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>

1 个答案:

答案 0 :(得分:6)

您可以将calc()用于flex-basis长度(在您的情况下 - 它是简写属性flex),10px因为margin-left: 5pxmargin-right: 5px }:

JSFiddle

.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>