2

I am using flexbox to get the height of the parents to be the same (need them to match) and flex-wrap to wrap every 3.

I then have a button that is always going to be at the bottom.

The first child will always have different sized heights, but always the same width.

Now the trick is, I want child 2 to be at the bottom on top of the button and have both clear the content of child 1.

Absolute positioning will not clear the text of child 1.

I tried flex-grow, but it didn't work for me, maybe because I am new to flexbox.

Please look at the fiddle: https://jsfiddle.net/mm783qc6/4/

.contain {
  display: flex;
  border: 1px solid #000;
  width: 100%;
  flex-wrap: wrap;
}
.p {
  width: 29.5%;
  float: left;
  position: relative;
  margin: 0 10px 30px;
  border: 1px solid red;
}
.bb {
  position: absolute;
  bottom: 0;
  border: 1px solid green;
}
.c1 {
  border: 1px solid blue;
}
.c2 {
  border: 1px solid orange;
}
<div class="contain">

  <div class="p">
    <div class="c1">
      <p>Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text</p>
    </div>
    <div class="c2">
      <p>Go to bottom, but on top of bottom bumper - text can grow sdfkjlkfdj;lksjdf;</p>
    </div>
    <div class="bb">
      <p>---------------Button--------------</p>
    </div>
  </div>

  <div class="p">
    <div class="c1">
      <p>Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and textRandom Height and text Random Height and
        text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text</p>
    </div>
    <div class="c2">
      <p>Go to bottom, but on top of bottom bumper - text can grow sdfkjlkfdj;lksjdf;</p>
    </div>
    <div class="bb">
      <p>-------Button-------</p>
    </div>
  </div>

  <div class="p">
    <div class="c1">
      <p>Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and textRandom Height and text Random Height and
        text Random Height and text Random Height and text Random Height and text Random Height and</p>
    </div>
    <div class="c2">
      <p>Go to bottom, but on top of bottom bumper - text can grow sdfkjlkfdj;lksjdf;</p>
    </div>
    <div class="bb">
      <p>-------Button-------</p>
    </div>
  </div>


  <div class="p">
    <div class="c1">
      <p>Random Height and text Random Height</p>
    </div>
    <div class="c2">
      <p>Go to bottom, but on top of bottom bumper - text can grow sdfkjlkfdj;lksjdf;</p>
    </div>
    <div class="bb">
      <p>---------------Button--------------</p>
    </div>
  </div>

  <div class="p">
    <div class="c1">
      <p>Random Height and text Random Height</p>
    </div>
    <div class="c2">
      <p>Go to bottom, but on top of bottom bumper - text can grow sdfkjlkfdj;lksjdf;</p>
    </div>
    <div class="bb">
      <p>-------Button-------</p>
    </div>
  </div>

  <div class="p">
    <div class="c1">
      <p>Random Height and text Random Height</p>
    </div>
    <div class="c2">
      <p>Go to bottom, but on top of bottom bumper - text can grow sdfkjlkfdj;lksjdf;</p>
    </div>
    <div class="bb">
      <p>-------Button-------</p>
    </div>
  </div>
</div>
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
RooksStrife
  • 1,647
  • 3
  • 22
  • 54

1 Answers1

2

Try making your class p elements into flex containers with column-direction. Then using auto margins to push children 2 and 3 down to the bottom. This method aligns the items as you want, and prevents overlapping.

Revised CSS

.contain {
    display: flex;
    border: 1px solid #000;
    width: 100%;
    flex-wrap: wrap;
}

.p {
    width: 29.5%;
    /* float: left;              <-- remove; not necessary */
    /* position: relative;       <-- remove; not necessary */
    margin: 0 10px 30px;
    border: 1px solid red;

    display: flex;               /* new */
    flex-direction: column;      /* new */
}

.bb {
    /* position: absolute;       <-- remove; not necessary */
    /* bottom: 0;                <-- remove; not necessary */
    border: 1px solid green;
}

.c1 {
    border: 1px solid blue;
    margin-bottom: auto;         /* new; push self to top, everything else to bottom */
    flex: 1;                     /* new; consume all available free space */
}

.c2 {
    border: 1px solid orange;
}

Revised Fiddle

Learn more about flex auto margins here: Methods for Aligning Flex Items along the Main Axis

Also, if you are wanting equal height columns to extend beyond the first row – i.e., you want the items on the second row to match the height of items on the first row – this is not possible with flexbox. I explain the reason here: Equal height rows in a flex container

Community
  • 1
  • 1
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • Works like a charm. Thank you for your answer and the time it took. It was really helpful. – RooksStrife Mar 29 '16 at 19:27
  • Second question - https://jsfiddle.net/mm783qc6/8/ How would you get c1's height to be equal - then have c2 snap to the bottom of c1 and c3 on bottom of the parent. See fiddle for example. As you see in the first column "Go to" is not aligned with the others. Didn't think of that. I can add another question to stack if need be. – RooksStrife Mar 29 '16 at 19:37
  • I want all c1 to be equal height and all c2 to snap to the bottom of c1. Then c3 to be at the bottom of the parent. All clearing each other of course. I started typing the second question, let me know. Also, each row can have a different c1 height - as in row one all three 100px, row 2 all 146px, etc. – RooksStrife Mar 29 '16 at 19:41
  • Can't do that with flexbox. Equal height columns exist among *siblings* in a flex container. So your class `p` elements share equal height. Your `c1` elements are not siblings. They are cousins. As such, there is no flex equal height feature. You could hard-code an equal height for the `c1` class, or find a way to re-structure your HTML making `c1` siblings in the same container. – Michael Benjamin Mar 29 '16 at 19:42
  • 1
    Drat, oh well. Thanks again. – RooksStrife Mar 29 '16 at 19:46
  • 1
    Got it to work with some crazy useage of padding-bottom. http://jsfiddle.net/kJL3u/394/ I took off the flex-child and added a couple divs. To note the c1's

    are really images for me - they will always be square. The width of the square-box containers are driven by bootstrap - .square-box{ width: 100%; text-align: center; padding-bottom: 100%; position:relative; } .box-item { position:absolute; }

    – RooksStrife Mar 29 '16 at 20:16