24

I'm trying to build a pricing table where each column contains a card. I want all cards to stretch to the height of their parent (.col) elements.

Note: I'm using Bootstrap 4, and trying to achieve this with the existing grid system (for the sake of consistency) and with this particular piece of markup. I can't get the cards to grow to the height of their parent containers. Is this even possible with the current markup?

The basic markup is this:

<div class="row">
    <div class="col">
        <div class="card">
          blah
          blah
          blah
        </div>
        <div class="card">
          blah
        </div>
        <div class="card">
          blah
        </div>
    </div>
</div>

Here is my pen: https://codepen.io/anon/pen/oZXWJB

enter image description here

Brian
  • 3,850
  • 3
  • 21
  • 37
JCraine
  • 1,159
  • 2
  • 20
  • 38

8 Answers8

17

Add flex-grow : 1; to your .card rule. HTML markup is fine.

.row {
  display: flex; 
  flex-flow: row wrap; 
  background: #00e1ff;
  margin: -8px;
}
.col { 
  display: flex; 
  flex: 1 0 400px;
  flex-flow: column; 
  margin: 10px;
  background: grey;
}
.card {
  display: flex;
  padding: 20px;
  background: #002732;
  color: white;
  opacity: 0.5;
  align-items: stretch;
  flex-direction: column;
  flex-grow: 1;
}

You may also look at Foundation 6 Equalizer plugin. They use JavaScript though.

Sumi Straessle
  • 1,116
  • 12
  • 23
  • 1
    That's perfect. Thanks for that! As a bonus question, is it possible to have all cards remain the same height when they stack? (Guessing not possible without JS) – JCraine Feb 27 '17 at 23:38
  • Look at this [codepen](https://codepen.io/sumi1985/pen/mWeXVV). That's how I would go about it. I'm using jQuery though. Do you need to achieve the same result in pure JS? I'm not aware of a pure CSS technique to keep track of the biggest height among siblings. – Sumi Straessle Feb 28 '17 at 17:57
  • Thanks Sumi, that's fine - was just curious if such a flex feature existed. JQuery will do. – JCraine Mar 01 '17 at 00:05
7

You just need to add height: 100% on .card

.card {
  display: flex;
  padding: 20px;
  background: #002732;
  color: white;
  opacity: 0.5;
  align-items: stretch;
  flex-direction: column;
  height: 100%;
}

DEMO

Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
5

Just add flex: 1 to your card class. Should be enough. And you don't need display: flex; align-items: stretch; flex-direction: column in this class.

Marcin
  • 1,426
  • 16
  • 19
1

Add height: 100% to .card

.card {
  display: flex;
  height: 100%;
  padding: 20px;
  background: #002732;
  color: white;
  opacity: 0.5;
  align-items: stretch;
  flex-direction: column;
}

example - https://codepen.io/anon/pen/zZGzyd

grinmax
  • 1,835
  • 1
  • 10
  • 13
0

Just make your .cardheight to 100%.

.card{
   height: 100%;
   display: flex;
   padding: 20px;
   background: #002732;
   color: white;
   opacity: 0.5;
   align-items: stretch;
   flex-direction: column;
}
Chaitali
  • 631
  • 4
  • 12
0

If you're using Bootstrap 4, they already have build-in classes for what you're trying to achieve.

Add card-deck to <div class="row card-deck">

here's screenshot

TylerH
  • 20,799
  • 66
  • 75
  • 101
0

Add display: inline-block; on .col and min-height: 100%; on .class

.row {
  display: flex; 
  ...
  }
.col { 
  display: inline-block; 
  ...
  }
.card {
  min-height:100%;
  ...
  }`
Rojo
  • 2,749
  • 1
  • 13
  • 34
0

To make elements stretch vertically within a .col class using flexbox, simply add inline-style to the .col element:

<div style={{alignItems:"stretch"}} class="col">

or

add the align-items: stretch; property to the .col class:

.col {
    align-items: stretch;
    ...rest
}
32lp1
  • 21
  • 1