This question gets asked a lot, but no one seems to have an answer, other than using JavaScript to calculate the middle row's height using window.innerHeight
, and .offsetHeight
. That's what I am doing already, but I'd like to do it the "right way" using only native CSS Grid.
I am trying to accomplish this:
+-----------------------------------+
| Header (fixed height in px) |
|------+-------------------+--------|
| | | |
| Left | Center | Right |
| | | |
| | fill remaining | |
| | height | |
| | | |
|------+-------------------+--------|
| Footer (fixed height in px) |
+-----------------------------------+
So far, I have the following. But it doesn't work if I change header and footer height to a fixed height. It only works cleanly when my heights are all "vh" based and all add up to 100vh.
The grid is exactly what I need except for the fixed height header and footer.
HTML:
<div class="content-main">
<div class="header">Header</div>
<div class="left">Left</div>
<div class="center">Center</div>
<div class="right">Right</div>
<div class="footer">Footer</div>
</div>
CSS:
.content-main {
height: 100vh;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: minmax(auto, auto);
grid-template-areas:
"hd hd hd"
"lt cn rt"
"ft ft ft";
}
.header {
grid-area: hd;
height: 10vh;
}
.left {
grid-area: lt;
height: 80vh;
width: 20vw;
}
.center {
grid-area: cn;
height: 80vh;
width: 60vw;
}
.right {
grid-area: rt;
height: 80vh;
width: 20vw;
}
.footer {
grid-area: ft;
height: 10vh;
}