Search code examples
cssflexboxalignment

Flex wrap - stack rows without stretch?


I have a flex container with two children of fixed dimensions that are aligned flex-end (the bottom of the parent).

When I resize the parent I want them to wrap on top of each other, but instead they each take up 50% of the parent height.

Is there a way to do this without adding another div?

FIDDLE

.wrapper {
  width: 600px;
  height: 500px;
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: flex-end;
  background-color: #eeeeee;
  resize: horizontal;
  overflow: auto;
  flex-wrap: wrap;
}

.one {
  width: 200px;
  height: 100px;
  background: red;
}

.two {
  width: 200px;
  height: 100px;
  background: blue;
}
<div class="wrapper">
  <div class="one">1</div>
  <div class="two">2</div>
</div>


Solution

  • You need to also consider align-content like below:

    .wrapper {
      width: 600px;
      height: 500px;
      display: flex;
      flex-direction: row;
      justify-content: flex-start;
      align-content:flex-end; /*added this*/
      align-items: flex-end;
      background-color: #eeeeee;
      resize: horizontal;
      overflow: auto;
      flex-wrap: wrap;
    }
    
    .one {
      width: 200px;
      height: 100px;
      background: red;
    }
    
    .two {
      width: 200px;
      height: 100px;
      background: blue;
    }
    <div class="wrapper">
      <div class="one">1</div>
      <div class="two">2</div>
    </div>