I have a very simple CSS grid with just two rows: a header
and a div
. I want the second row to take up all remaining view port height. Here's a repro:
html, body { margin: 0; padding: 0; }
* { box-sizing: border-box; }
body {
display: grid;
height: 100vh;
width: 100vw;
}
header {
border: 1px solid red;
}
.content {
border: 1px solid blue;
}
<header>
<h1>IMG + Some header text</h1>
</header>
<div class="content">
Here will be dynamic content (a grid on its own).
Should take all the remaining height.
</div>
This uses way too much space for the header
. I want it to take up at most as much space as needed for its content not to overflow.
How can I do this?
I'm interested about any spec-based solution, though practically I would like to be able to use it at least in Chrome (or Firefox) latest stable build.
Just use width: 100% and height: 100% in the CSS class of the item you want to fill the grid. Join a max-width property and a max-height property if you don't want a grid item inside a grid container to grow more than some size.
By default, a grid item cannot be smaller than the size of its content. Grid items have an initial size of min-width: auto and min-height: auto . You can override this behavior by setting grid items to min-width: 0 , min-height: 0 or overflow with any value other than visible .
Fr is a fractional unit and 1fr is for 1 part of the available space. The following are a few examples of the fr unit at work. The grid items in these examples are placed onto the grid with grid areas.
Specify auto sizing for the header row and 1fr
to allocate all the remaining space to the content row.
html, body { margin: 0; padding: 0; }
* { box-sizing: border-box; }
body {
display: grid;
grid-template-rows: auto 1fr;
height: 100vh;
width: 100vw;
}
header {
border: 1px solid red;
}
.content {
border: 1px solid blue;
}
<header>
<h1>IMG + Some header text</h1>
</header>
<div class="content">
Here will be dynamic content (a grid on its own).
Should take all the remaining height.
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With