4

I have 10 divs: 2 hidden and 8 stacked one on top of each other.

Using a media query, when resizing the screen, I can display the 2 hidden divs.

So, now I have 4 red divs at the bottom but I want them to appear in pairs - 2 lines with 2 red divs one next to each other.

enter image description here

How do I do that?

html {
  font-size: 20px;
}

.box {
  color: white;
  font-size: 100px;
  text-align: center;
  text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
  padding: 10px;
  Margin: 5px;
  /*  width: calc(33.33% - 10px);*/
}


/* Flexbox code starts here */

.container {
  display: flex;
  /*Must!!!! on the container, in order to turn it to flex*/
  flex-direction: column;
  flex-wrap: wrap;
  justify-content: center;
}


/* Colors for each box */

.blue {
  background: blue;
}

.orange {
  background: Orange;
  height: 300px;
}

.green {
  background: green;
}

.red {
  background: red;
  height: 170px;
}

.hide-reds {
  display: none;
}


/*Media Queries for Different Screen Sizes*/

@media all and (min-width: 800px) {
  .red {
    display: block;
  }
}
<div class="container">
  <div class="box blue">Blue</div>
  <div class="box blue">Blue</div>
  <div class="box orange">Orange</div>
  <div class="box green">Green</div>
  <div class="box green">Green</div>
  <div class="box green">Green</div>
  <div class="box red">Red</div>
  <div class="box red">Red</div>
  <div class="box red hide-reds">Red</div>
  <div class="box red hide-reds">Red</div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
JLT
  • 49
  • 1
  • 2
  • 4

1 Answers1

5

Your container has flex-direction: column. Your layout has one column. But there's no way to wrap flex items next to each other in a single column. Flexbox doesn't work that way.

However, your layout can work with flex-direction: row and flex-wrap: wrap. By giving each item width: 100%, each item forces the next item to the next row. This creates a single column of stacked items. Then, give the last four items width: 50%, so two fit on each row.

.container {
  display: flex;
  flex-wrap: wrap;
}

.box {
  flex: 0 0 100%;
}

.red {
  flex: 1 0 100%;
  background: red;
  height: 170px;
}

.hide-reds {
  display: none;
}

@media all and (min-width: 800px) {
  .red {
    flex-basis: 40%; /* see note below */
    display: block;
  }
}


/* not relevant to the problem */
.blue {
  background: blue;
}

.orange {
  background: Orange;
  height: 300px;
}

.green {
  background: green;
}

html {
  font-size: 20px;
}

.box {
  color: white;
  font-size: 100px;
  text-align: center;
  text-shadow: 4px 4px 0 rgba(0, 0, 0, 0.1);
  padding: 10px;
  margin: 5px;
}
<div class="container">
  <div class="box blue">Blue</div>
  <div class="box blue">Blue</div>
  <div class="box orange">Orange</div>
  <div class="box green">Green</div>
  <div class="box green">Green</div>
  <div class="box green">Green</div>
  <div class="box red">Red</div>
  <div class="box red">Red</div>
  <div class="box red hide-reds">Red</div>
  <div class="box red hide-reds">Red</div>
</div>

jsFiddle demo

Note regarding flex-basis: 40%:

With flex-grow: 1 defined in the flex shorthand (see .red), there's no need for flex-basis to be 50%, which would actually result in one item per row due to the margins (see .box).

Since flex-grow will consume free space on the row, flex-basis only needs to be large enough to enforce a wrap. In this case, with flex-basis: 40%, there's plenty of space for the margins, but not enough space for a third item.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701