Example HTML and CSS:
.root {
background-color: green;
}
.outer {
display: grid;
grid-template-rows: 100vh;
grid-template-columns: 100px;
padding: 5rem;
overflow-y: auto;
}
.nav {
background-color: red;
height: 150vh;
}
* {
box-sizing: border-box;
}
<div class="root">
<div class="outer">
<div class="nav"></div>
</div>
</div>
Fiddle example: https://jsfiddle.net/dr18cbet/
In this contrived example, scrolling to the bottom of the grid container doesn’t include the grid container’s bottom padding.
There are definitely better ways to organize this code to work, for example, moving the padding and overflow settings to the grid item (in this case, nav
). I also noticed that if I set grid-template-rows
to it’s default auto
the padding also appears as expected. However, this combo of overflow + padding settings on the grid container and the grid item getting an extrinsic height (in the example it is 100vh
) results in the container’s bottom padding not being applied. Why is that? I dug through the css specs, but couldn’t find an explanation.
2
Answers
The trick is that you have to consider the grid area which is a kind of "extra wrapper" between the grid container and the grid item.
Your overflow is applied to the grid container and the grid area is not overflowing but the grid item is overflowing the grid area (not the grid container).
Here is another code to better understand. The first div is not overflowing (the grid area) but the nested div (the grid item) is overflowing and in such configuration the bottom padding is not visible but still present and the nested div is overlapping it.
It’s a bit tricky but it’s not something related to CSS Grid. This is how overflow behaves.
Somewhere in the specification you can read:
The "visible overflow of a descendant" is the key here
As a supplement to @temani-afif’s answer, according to the spec:
Here, the grid container’s
max-content
size is determined based on the size of the track, not the height of the items that are placed. The scrolling area height of your scroll container computed based on the padding box100vh + your padding
, not the grid item’s height150vh
.