Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break flex items into columns for mobile?

Tags:

html

css

flexbox

I'm using flex and I have 4 columns. How would I break it to have 2 side by side for mobile?

My pen: http://codepen.io/omarel/pen/PpPXao

.flexparent {
  display: -webkit-flex;
  display: flex;
  width: 100%;
  max-width: 700px;
  margin: 0 auto;
  justify-content: center;
}

.flexparent .flexchild {
  width: 30%;
}
<div class="flexparent">
  <div class="flexchild">
    <img src="../images/new/mw/icons/icon-trains.png" alt="">
    <h2>Col 1</h2>

  </div>
  <div class="flexchild">
    <img src="../images/new/mw/icons/icon-events.png" alt="">
    <h2>Col 2</h2>

  </div>
  <div class="flexchild">
    <img src="../images/new/mw/icons/icon-parks.png" alt="">
    <h2>Col 3</h2>
  </div>
  <div class="flexchild">
    <img src="../images/new/mw/icons/icon-walk.png" alt="">
    <h2>Col 4</h2>
  </div>
</div>
like image 915
obreezy Avatar asked Sep 03 '25 05:09

obreezy


1 Answers

Set the parent to flex-wrap: wrap; and the children to width: 50% to have 2 per row.

.flexparent {
  display: -webkit-flex;
  display: flex;
  width: 100%;
  max-width: 700px;
  margin: 0 auto;
  justify-content: center;
}

.flexparent .flexchild {
  width: 30%;
}

@media (max-width: 420px) {
  .flexparent {
    flex-wrap: wrap;
  }
  .flexparent .flexchild {
    width: 50%;
  }
}
<div class="flexparent">
  <div class="flexchild">
    <img src="../images/new/mw/icons/icon-trains.png" alt="">
    <h2>Col 1</h2>

  </div>
  <div class="flexchild">
    <img src="../images/new/mw/icons/icon-events.png" alt="">
    <h2>Col 2</h2>

  </div>
  <div class="flexchild">
    <img src="../images/new/mw/icons/icon-parks.png" alt="">
    <h2>Col 3</h2>
  </div>
  <div class="flexchild">
    <img src="../images/new/mw/icons/icon-walk.png" alt="">
    <h2>Col 4</h2>
  </div>
</div>
like image 53
Michael Coker Avatar answered Sep 04 '25 19:09

Michael Coker