Flexbox,如何在 400 像素处保持至少 2 列?

Flexbox, how to keep a minimum of 2 cols at 400px?

https://codepen.io/leon-yum/pen/XYpgzj?editors=1100

我试图确保在移动设备(400 像素及以下)上我的 flexbox 网格坚持 2 列/框。但是无论我做什么,它要么对齐到 1 列,要么每行分别保持在 3 列或 4 列。

预期宽度为 400 像素:

结果:

HTML

<div class="flex-grid-thirds">
  <div class="col">This little piggy went to market.</div>
  <div class="col">This little piggy stayed home.</div>
  <div class="col">This little piggy had roast beef.</div>
</div>

<div class="flex-grid-four">
  <div class="col">This little piggy went to market.</div>
  <div class="col">This little piggy stayed home.</div>
  <div class="col">This little piggy had roast beef.</div>
  <div class="col">This little piggy had roast beef.</div>
</div>

CSS

.flex-grid-thirds,
.flex-grid-four {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.flex-grid-thirds .col {
  width: 32%;
}

.flex-grid-four .col {
  width: 24%;
}

@media (max-width: 400px) {
  .flex-grid-thirds,
  .flex-grid-four {
    .col {
      width: 40%;
    }
  }
}

* {
  box-sizing: border-box;
}
body {
  padding: 20px;
}
.flex-grid { 
  margin: 0 0 20px 0;
}
.col {
  background: salmon;
  padding: 20px;
}

这是可行的解决方案:https://codepen.io/anon/pen/vrgZVa?editors=1100。 您所需要的只是将所有项目放在一个包装器元素中(现在您有 2 个元素),然后将 flex-wrap: wrap 属性 赋给该包装器。还有一件事要做:给你的项目宽度(最好是百分比),如果你给它们 width: 50%,就会有 2 列,如果 width: 33% - 3 列等等。我没有触及边距和这么小的东西,只是展示你的要求:

.flex-grid {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.flex-grid-thirds .col {
  width: 32%;
}

.flex-grid-four .col {
  width: 24%;
}

@media (max-width: 400px) {
  .flex-grid {
    .col {
      width: 50%;
    }
  }
}

* {
  box-sizing: border-box;
}

body {
  padding: 20px;
}
.flex-grid { 
  margin: 0 0 20px 0;
}
.col {
  background: salmon;
  padding: 20px;
}
<div class="flex-grid">
  <div class="col">This little piggy went to market.</div>
  <div class="col">This little piggy stayed home.</div>
  <div class="col">This little piggy had roast beef.</div>
  <div class="col">This little piggy went to market.</div>
  <div class="col">This little piggy stayed home.</div>
  <div class="col">This little piggy had roast beef.</div>
  <div class="col">This little piggy had roast beef.</div>
</div>