Flexbox 使用 justify-content 的值 space-between 使这变得非常容易:
.flexrow {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
这样可以确保在尺寸正确时物品会粘在容器的外边缘。
通过将弹性项目的宽度设置为小于总宽度的 100% 来创建装订线。这里我们需要 2 列,因此您可以将项目的宽度设置为 50% 减去足以弥补装订线宽度:
.flexcol {
width: calc(50% - 5px); // 50% minus 5px for each column, with 2 columns that's a 10px gutter space between the two columns.
margin-bottom: 10px; //space between the rows, remove if you dont want that.
}
现在要设置第一行和第三行,只需相应地更改宽度即可:
.col-1,
.col-6 {
width: calc(40% - 5px); // here's the 40% ones
}
.col-2,
.col-5 {
width: calc(60% - 5px); // and the 60% ones
}
对于 992px 的 100% 变化,只需添加一个媒体查询并更新必要的值:
@media (max-width: 992px) {
.flexcol {
width: 100%; // 100% width, no margin or anything to deal with because of the space-between layout approach
}
.col-5 {
margin-bottom: 10px; // add this so the 5th and 6th items have space between them when 100% wide
}
}
.flexrow {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
background: #efefef;
}
.flexcol {
width: calc(50% - 5px);
margin-bottom: 10px;
/* just for visibility */
border: 1px solid red;
padding: 20px;
background: #fff;
box-sizing: border-box;
}
.col-1,
.col-6 {
width: calc(40% - 5px);
}
.col-2,
.col-5 {
width: calc(60% - 5px);
}
.col-5,
.col-6 {
margin-bottom: 0;
}
@media (max-width: 992px) {
.flexcol {
width: 100%;
}
.col-5 {
margin-bottom: 10px;
}
}
<div class="flexrow">
<div class="flexcol col-1">THING 1</div>
<div class="flexcol col-2">THING 2</div>
<div class="flexcol col-3">THING 3</div>
<div class="flexcol col-4">THING 4</div>
<div class="flexcol col-5">THING 5</div>
<div class="flexcol col-6">THING 6</div>
</div>