Do flexible wrapping items two at a time

What I have now, when the size of the window changes, the boxes correspond accordingly to the width. However, I want at least two boxes to move to the next line instead of 3 on top and 1.

Is this possible with flexbox, and if not, how would this be done?

script: 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;
}
<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 codeHide result
+4
source share
1 answer

You can use a media query that changes flex-basis50% to a wrapper by forcing two elements in a row.

Add this to your CSS:

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

Revised Demo

+3
source

All Articles