一種方法是使用多行和flex-grow: 1
不要在:last-child
html, body {
\t height: 100%;
\t background: #252525;
margin: 0;
}
section {
}
.row {
\t display: flex;
}
.column {
\t flex: 1 0 0;
\t margin-top: 25px;
\t height: 200px;
\t background: #2848e6;
}
.column:not(:last-child) {
\t margin-right: 25px;
}
<section>
\t <div class="row">
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t </div>
\t <div class="row">
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t </div>
</section>
使用權margin
或者你可以使用margin-right: 15px
除了:nth-child(4)
和flex-basis
的calc(25% - 11.25px)
(45px保證金/ 4盒= 11.25)作爲寬度。
html, body {
\t height: 100%;
\t background: #252525;
margin: 0;
}
section {
}
.row {
\t display: flex;
\t flex-wrap: wrap;
}
.column {
\t flex: 0 1 calc(25% - 11.25px);
\t margin-top: 25px;
\t height: 200px;
\t background: #2848e6;
}
.column:not(:nth-child(4n)) {
\t margin-right: 15px;
}
<section>
\t <div class="row">
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t </div>
</section>
您可以使用grid-column-gap
和fr
單位display: grid;
做到這一點很容易。
html, body {
\t height: 100%;
\t background: #252525;
margin: 0;
}
section {
}
.row {
\t display: grid;
\t grid-template-columns: 1fr 1fr 1fr 1fr;
\t grid-column-gap: 25px;
\t grid-row-gap: 25px;
}
.column {
\t height: 200px;
\t background: #2848e6;
}
<section>
\t <div class="row">
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t \t <div class="column"></div>
\t </div>
</section>
是您打開使用'grid'? https://codepen.io/mcoker/pen/WOWQGP?editors=1100 –
我寧願使用flexbox。支持似乎好得多。 – jesders88
如果您對列使用固定邊距,我認爲獲得列填充行的唯一方法是使用'flex-grow:1',這不適用於包裝或使用的行calc()就像你一樣。你打算多行嗎? –