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.

4 comments

@ibrahimab 2020-01-22 16:55:12

This is possible using a combination of display:flex, flex-direction: column, align-items: stretch

    .grid {
        display: flex;
        align-items: stretch;
        flex-wrap: wrap; /* disable this if you don't want to make the grid responsive */
    }
    .column {
        margin: 10px;
        padding: 10px;
        border: 1px solid red;
        display: flex;
        flex-direction: column;
    }
    .thumbnail {
        flex-grow: 1;
    }
    .caption {
        word-wrap: break-word;
        width: 342px; /* max size of caption */
    }
    <div class="grid">
        <a href="" class="column">
            <div class="thumbnail"><img src="thumbnail.jpg" /></div>
            <small class="caption">This is a captiond dfs dfsdfdfs dfssdf sfd fdsdfsdfsdfs fds fdsfdsfdsdfs dfsdfsdfsdfsdfsfdsdfsfdsfdsfds fdsdfssfd fsdfdfdsfdsdfs fdsdfssfdsdfsddfs fdsdfsdfsdf fsdfdsdfdfssdf dfssdffdsdfsdf dfsdfsdfsddfsdfs dfssdffdsdfs dfsdfdfsdfssdf fdsdfsdfsdfs sasads</small>
        </a>
        <a href="" class="column">
            <div class="thumbnail"><img src="thumbnail.jpg" /></div>
            <small class="caption">This is a caption</small>
        </a>
        <a href="" class="column">
            <div class="thumbnail"><img src="thumbnail.jpg" /></div>
            <small class="caption">This is a caption</small>
        </a>
        <a href="" class="column">
            <div class="thumbnail"><img src="thumbnail.jpg" /></div>
            <small class="caption">ThisisacaptionThisisacaptionThisisacaptionThisisacaptionThisisacaptionThisisacaptionThisisacaptionThisisacaptionThisisacaptionThisisacaptionThisisacaptionThisisacaptionThisisacaption</small>
        </a>
        <a href="" class="column">
            <div class="thumbnail"><img src="thumbnail.jpg" /></div>
            <small class="caption">This is a caption</small>
        </a>
        <a href="" class="column">
            <div class="thumbnail"><img src="thumbnail.jpg" /></div>
            <small class="caption">This is a caption</small>
        </a>
        <a href="" class="column">
            <div class="thumbnail"><img src="thumbnail.jpg" /></div>
            <small class="caption">This is a caption</small>
        </a>

@Hlsg 2020-01-23 17:16:08

You probably misunderstood the question. This pen should clear it: codepen.io/Hlsg/pen/EXKJba

@MTZ 2019-03-14 13:19:35

You can use percentages for grid-template-rows like so

grid-template-rows: 50% 50%;

@Michael_B 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.

@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.

https://codepen.io/Hlsg/pen/EXKJba

Related Questions

Sponsored Content

4 Answered Questions

[SOLVED] Can I make a CSS grid with dynamic number of rows or columns?

5 Answered Questions

[SOLVED] Can I have a varying number of columns per row in a CSS grid?

  • 2017-12-03 00:23:17
  • knitevision
  • 6249 View
  • 8 Score
  • 5 Answer
  • Tags:   css css-grid

2 Answered Questions

Grid layout images are not equal height

  • 2019-04-15 18:42:36
  • fightstarr20
  • 259 View
  • 0 Score
  • 2 Answer
  • Tags:   css css-grid

1 Answered Questions

[SOLVED] How do I specify row heights in CSS Grid layout?

1 Answered Questions

[SOLVED] Equal height children of grid items

1 Answered Questions

[SOLVED] Equal height multi column/multi element layout with only CSS

1 Answered Questions

Flexbox Grid with equal nested rows between columns

1 Answered Questions

Css Grid Wrap columns keep row height

  • 2017-08-15 10:14:18
  • elmcrest
  • 269 View
  • 0 Score
  • 1 Answer
  • Tags:   css css-grid

0 Answered Questions

CSS Grid - Align elements of unknown height vertically inside items

  • 2017-05-18 08:55:23
  • Gratz
  • 120 View
  • 0 Score
  • 0 Answer
  • Tags:   css css-grid

1 Answered Questions

[SOLVED] Equal height rows in a CSS grid

  • 2017-01-05 17:07:08
  • Ralph
  • 2112 View
  • 1 Score
  • 1 Answer
  • Tags:   css grid-layout

Sponsored Content