使 flex 项目一次包裹两个

Sno*_*lax 5 html javascript css flexbox

我现在拥有的是当窗口重新调整大小时,框会相应地调整以适应宽度。但是,我想要的是至少有两个框移动到下一行,而不是顶部的 3 个和 1 个。

这是否可以通过 flexbox 实现,如果不能,我将如何实现?

小提琴:https : //jsfiddle.net/jzhang172/cqdwLyxu/

.line{
  display:flex;
  width:100%;
  margin-bottom:30px;
  flex-wrap:wrap;
  align-items:center;
  justify-content:center;
  
}
body,html{
  margin:0;
  padding:0;
  position:relative;
}
*{
  box-sizing:border-box;
}
.box{
  width:200px;
  min-width:200px;
  background:blue;
  margin:0 10px 0 10px;
  height:200px;
}
Run Code Online (Sandbox Code Playgroud)
<div class="line">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>
<div class="line">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

Mic*_*l_B 4

您可以使用媒体查询将换行更改flex-basis为 50%,强制每行两个项目。

将其添加到您的 CSS 中:

@media ( max-width: 800px ) {
    .box {
           flex-basis: calc(50% - 20px);
           margin: 10px; 
         }
}
Run Code Online (Sandbox Code Playgroud)

修改后的演示