Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CSS Grid, why do explicit rows not render if the overall space exceeds 100% of the parent?

Tags:

css

css-grid

Using CSS Grid, I am trying to create a grid that (sometimes) takes more than 100% of the containing element's height. How can this be achieved?

When creating 3 rows that together take 100%, the explicit grid is shown just fine:

enter image description here

https://codepen.io/tommedema/pen/dKqwrZ?editors=1100#0

However, if prior rows take more than 100%, the third row is suddenly gone, even though it is explicitly defined:

enter image description here

https://codepen.io/tommedema/pen/jKvdOO?editors=1100#0

Note that there is no scrollbar going 40% (140 - 100) further down, which I would have expected there to be.

Updated question

When explicitly defining grid-row from the first to last row to at least one child, the overflow (and scrollbar) does show. This is surprising, as I would expect it to overflow without such statement too:

New CSS:

html, body {
  height: 100%;
}

body {
  background-color: #ddd;

  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 20% 100% 20%;
}

/* 
  allows for row overflow to not be cropped
  see also https://stackoverflow.com/questions/51048661
*/
body::after {
  content: "";
  grid-row: 1 / -1;
}

https://codepen.io/tommedema/pen/MXqRPW?editors=1100#0

Even more surprising, when you put a grid inside this grid with the same setup, the ::after trick does not work for this inner grid.

like image 974
Tom Avatar asked Jun 26 '18 17:06

Tom


People also ask

What are the properties of grid in CSS?

All CSS Grid Properties Property Description grid-area Either specifies a name for the grid ite ... grid-auto-columns Specifies a default column size grid-auto-flow Specifies how auto-placed items are inse ... grid-auto-rows Specifies a default row size 17 more rows ...

How many columns and rows does the explicit grid have?

The explicit grid has now three columns and three rows. You can tell this by using the Firefox Grid Inspector to check the continuous line “closing” the grid at the bottom, after the third row:

How to create a CSS grid with 3 columns and 3 rows?

The CSS code creates a grid container and 3 columns inside that container. The distance between rows and columns (gap) is 20 px. The browser places the fourth item on the second row. The second and third rows are implicit tracks. You can tell by the dotted style of the horizontal grid lines. The explicit grid has now three columns and three rows.

What is grid-template-rows in CSS?

1 Definition and Usage. The grid-template-rows property specifies the number (and the heights) of the rows in a grid layout. 2 Browser Support. The numbers in the table specify the first browser version that fully supports the property. 3 CSS Syntax 4 Property Values. No size is set. Sets the size of the rows, by using a legal length value.


1 Answers

It's because you've limited the height of the container to a fixed 100%. The overflow is clipped.

In addition, percentage heights don't work as expected with grid rows (see note below).

For a simpler and stronger solution use vh units.

body {
  background-color: #ddd;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 20vh 100vh 20vh;
}

revised codepen 1

From the CSS Grid specification, with regard to grid-template-columns and grid-template-rows:

If the size of the grid container depends on the size of its tracks, then <percentage> must be treated as auto.

So if you release the fixed height and use, let's say, min-height: 100% on the container, then your 20% 100% 20% determine the height of the container, compute to auto as a result and, per the auto algorithm, resolve to three equal height rows.

revised codepen 2

like image 112
Michael Benjamin Avatar answered Nov 15 '22 06:11

Michael Benjamin