Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make CSS Grid last row to take up remaining space

Tags:

css

css-grid

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?

like image 290
gfpacheco Avatar asked Oct 18 '25 14:10

gfpacheco


1 Answers

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

like image 124
service-paradis Avatar answered Oct 21 '25 01:10

service-paradis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!