4

I have a grid with variable number of rows and I want the last one to be 1fr height.

Something like this:

enter image description here

Is there any way of doing that?

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
gfpacheco
  • 2,831
  • 2
  • 33
  • 50

1 Answers1

4

You could use flex for this.

The parent container should have display: flex;. We want to use it vertically, so we will change the flex direction like this flex-direction: column;.

After this, we will use the property flex-shrink: 1; for every child. This way, it will take only the space needed/specified. The tricks for the last child is to use the property flex-grow: 1; so it will take the available space.

See the snippet below:

html, body {
  height: 100%;
}

.fill-height {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.fill-height > div {
  border: 1px solid red;
  min-height:2em;
  flex-shrink: 1;
}

.fill-height > div:last-child {
  flex-grow: 1;
}
<section class="fill-height">
  <div>
    row 1 (shrink)
  </div>
  <div>
    row 2 (shrink)
  </div>
  <div>
    row 3 (grow)
  </div>
</section>

Link to JsFiddle

service-paradis
  • 3,333
  • 4
  • 34
  • 43
  • 1
    Thanks for the great answer! I was hoping to be able to use grid, but I really think that's not possible. – gfpacheco Nov 20 '18 at 19:53