21

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,812
  • 3
  • 19
  • 36
JCraine
  • 997
  • 2
  • 16
  • 31

7 Answers7

13

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,036
  • 11
  • 22
  • 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
5

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
  • 111,264
  • 15
  • 131
  • 153
4

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,386
  • 15
  • 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,811
  • 1
  • 9
  • 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
  • 591
  • 3
  • 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,816
  • 57
  • 73
  • 92
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,387
  • 1
  • 10
  • 32