By Hlsg

2017-06-11 20:16:49 8 Comments

I gather that this is impossible to achieve using Flexbox, as each row can only be the minimal height required to fit its elements, but can this be achieved using the newer CSS Grid?

To be clear, I want equal height for all elements in a grid across all rows, not just per each row. Basically, the highest "cell" should dictate the height of all cells, not just the cells in its row.


@Michael Benjamin 2017-06-12 00:56:18

Short Answer

If the goal is to create a grid with equal height rows, where the tallest cell in the grid sets the height for all rows, here's a quick and simple solution:

  • Set the container to grid-auto-rows: 1fr

How it works

Grid Layout provides a unit for establishing flexible lengths in a grid container. This is the fr unit. It is designed to distribute free space in the container and is somewhat analogous to the flex-grow property in flexbox.

If you set all rows in a grid container to 1fr, let's say like this:

grid-auto-rows: 1fr;

... then all rows will be equal height.

It doesn't really make sense off-the-bat because fr is supposed to distribute free space. And if several rows have content with different heights, then when the space is distributed, some rows would be proportionally smaller and taller.

Except, buried deep in the grid spec is this little nugget:

7.2.3. Flexible Lengths: the fr unit


When the available space is infinite (which happens when the grid container’s width or height is indefinite), flex-sized (fr) grid tracks are sized to their contents while retaining their respective proportions.

The used size of each flex-sized grid track is computed by determining the max-content size of each flex-sized grid track and dividing that size by the respective flex factor to determine a “hypothetical 1fr size”.

The maximum of those is used as the resolved 1fr length (the flex fraction), which is then multiplied by each grid track’s flex factor to determine its final size.

So, if I'm reading this correctly, when dealing with a dynamically-sized grid (e.g., the height is indefinite), grid tracks (rows, in this case) are sized to their contents.

The height of each row is determined by the tallest (max-content) grid item.

The maximum height of those rows becomes the length of 1fr.

That's how 1fr creates equal height rows in a grid container.

Why flexbox isn't an option

As noted in the question, equal height rows are not possible with flexbox.

Flex items can be equal height on the same row, but not across multiple rows.

This behavior is defined in the flexbox spec:

6. Flex Lines

In a multi-line flex container, the cross size of each line is the minimum size necessary to contain the flex items on the line.

In other words, when there are multiple lines in a row-based flex container, the height of each line (the "cross size") is the minimum height necessary to contain the flex items on the line.

@Od Chan 2020-08-19 18:14:43

In the mentioned paragraph, what does this portion exactly mean: "and dividing that size by the respective flex factor to determine a “hypothetical 1fr size". For example, if the highest size in the first row of a grid container was 100px and the grid-auto-row for that row was 2, does this means the size of 1fr for the first row is equal to 50px?

@Hlsg 2017-06-12 15:10:54

The short answer is that setting grid-auto-rows: 1fr; on the grid container solves what was asked.

Related Questions

Sponsored Content

51 Answered Questions

[SOLVED] How can I transition height: 0; to height: auto; using CSS?

  • 2010-08-18 02:50:35
  • Hailwood
  • 1102741 View
  • 2212 Score
  • 51 Answer
  • Tags:   css css-transitions

37 Answered Questions

[SOLVED] Change an HTML5 input's placeholder color with CSS

33 Answered Questions

[SOLVED] Is there a CSS parent selector?

  • 2009-06-18 19:59:36
  • jcuenod
  • 2142245 View
  • 3290 Score
  • 33 Answer
  • Tags:   css css-selectors

28 Answered Questions

[SOLVED] Set cellpadding and cellspacing in CSS?

6 Answered Questions

[SOLVED] In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?

33 Answered Questions

[SOLVED] Make a div fill the height of the remaining screen space

  • 2008-09-18 05:06:17
  • Vincent McNabb
  • 1086520 View
  • 2011 Score
  • 33 Answer
  • Tags:   html css html-table

35 Answered Questions

[SOLVED] How to make a div 100% height of the browser window

  • 2009-10-15 21:18:43
  • mike
  • 2072460 View
  • 2207 Score
  • 35 Answer
  • Tags:   html css height

19 Answered Questions

[SOLVED] Is it possible to apply CSS to half of a character?

  • 2014-05-09 16:16:57
  • Mathew MacLean
  • 262927 View
  • 2864 Score
  • 19 Answer
  • Tags:   javascript html css

Sponsored Content