Skip to content Skip to sidebar Skip to footer

Prevent Next Row Taking Up Height Of Tallest Item In Previous Row With Flexbox?

I made a layout with flexbox consisting of 2 columns. When everything is the same height it works fine: https://codepen.io/anon/pen/rJZeJm

Solution 1:

Flexbox can't do that by itself in a flexible way.

As you mentioned yourself, CSS Grid can do this, though since you didn't want that, this suggestion is based on the assumption that you at narrower screens want the b to be at the bottom.

By simply initially use float, and then at a certain break point, swap between float and display: flex, it is very easy to take advantage of both.

Additionally, using Flexbox's order property, one can easily position the elements in any desired order, and with this avoid script and get a reasonable level of browser support.

Updated codepen

Stack snippet

* {
  box-sizing: border-box;  
}

.cont {
  background: gold;
  overflow: hidden;
}

.a,
.b,
.c {
  width: 45%;
  padding: 10px;
  float: left;
}

.a {
  background: red;
}

.b {
  background: blue;
  color: white;
  float: right;
}

.c {
  background: orange;
}

@media (max-width: 500px) {
  .a,
  .b,
  .c {
    width: auto;
    float: none;
  }
  .b {
    order: 1;
  }
  .cont {
    display: flex;
    flex-direction: column;
  }
}
<div class="cont">
  <div class="a">
    A
  </div>
  <div class="b">
    B
    <br>
    More B
  </div>
  <div class="c">C</div>
</div>

Post a Comment for "Prevent Next Row Taking Up Height Of Tallest Item In Previous Row With Flexbox?"