Michiel
Michiel

Reputation: 160

CSS flex grow does not work with added div on top

Consider the following fiddle: https://jsfiddle.net/7naxprzd/1/

Requirements are:

The code is working properly if the arrows are eliminated by deleting the div with class .arrow-wrapper, but I need the arrows.

A solution could be to absolute position the arrow, but is there a way to solve this layout issue with flex without absolute positioning the arrow?

Should work for modern browsers and IE11.

.row-wrapper {
  display: flex;
}

.col-wrapper-outer {
  display: flex;
  flex-wrap: wrap;
}

.arrow-wrapper {
  width: 100%;
  text-align: center;
  font-size: 30px;
}

.col-wrapper {
  width: 100%;
  display: flex;
  flex-direction: column;
  border: 2px solid red;
  color: white;
}

.col-wrapper .header {
  background: blue;
}

.col-wrapper .contents {
  flex: 1 0 auto;
  background: green;
}
<div class="row-wrapper">
  <div class="col-wrapper-outer">
    <div class="arrow-wrapper">
      &darr;
    </div>
    <div class="col-wrapper">
      <div class="header">Please align tops.</div>
      <div class="contents">Let me grow.</div>
    </div>
  </div>
  <div class="col-wrapper-outer">
    <div class="arrow-wrapper">
      &darr;
    </div>
    <div class="col-wrapper">
      <div class="header">please align tops.</div>
      <div class="contents">Let me grow.<br>Please<br>align<br>bottoms.</div>
    </div>
  </div>
</div>

Upvotes: 1

Views: 183

Answers (1)

Michael Benjamin
Michael Benjamin

Reputation: 371231

In your div with class col-wrapper-outer, instead of this:

.col-wrapper-outer {
  display: flex;
  flex-wrap: wrap;
}

Use this:

.col-wrapper-outer {
  display: flex;
  flex-direction: column;
}

Then add flex: 1 to .col-wrapper so it takes the full height of the container.

revised fiddle

.row-wrapper {
  display: flex;
}

.col-wrapper-outer {
  display: flex;
  /* flex-wrap: wrap; */
  flex-direction: column;    /* NEW */
}

.arrow-wrapper {
  width: 100%;
  text-align: center;
  font-size: 30px;
}

.col-wrapper {
  flex: 1;                   /* NEW */
  width: 100%;
  display: flex;
  flex-direction: column;
  border: 2px solid red;
  color: white;
}

.col-wrapper .header {
  background: blue;
}

.col-wrapper .contents {
  flex: 1 0 auto;
  background: green;
}
<div class="row-wrapper">
  <div class="col-wrapper-outer">
    <div class="arrow-wrapper">
      &darr;
    </div>
    <div class="col-wrapper">
      <div class="header">Please align tops.</div>
      <div class="contents">Let me grow.</div>
    </div>
  </div>
  <div class="col-wrapper-outer">
    <div class="arrow-wrapper">
      &darr;
    </div>
    <div class="col-wrapper">
      <div class="header">please align tops.</div>
      <div class="contents">Let me grow.
        <br>Please
        <br>align
        <br>bottoms.</div>
    </div>
  </div>
</div>

Upvotes: 1

Related Questions