1

I'm trying to build a grid with 2 rows inside a flex column. The flex container has a minimum height to fill the window. The first line of the grid should fill the available space, so I was thinking of the fr unit. A simplified version may look like that:

.container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
.grid {
  flex-grow: 1;
  background-color: red;
  padding: 1rem;
  grid-gap: 1rem;
  display: grid;
  grid-template-rows: 1fr auto;
}

.grid > * {
  background-color: white;
}
<div class="container">
  <h1>some title</h1>
  <div class="grid">
    <div>line 1</div>
    <div>line 2</div>
  </div>
</div>

This works perfectly fine with firefox:

test on firefox

But not with chrome:

test on chrome

What am I missing?

rocambille
  • 15,398
  • 12
  • 50
  • 68

1 Answers1

1

The grid container is interpreting the min-height: 100vh on the flex parent differently in Chrome and Firefox.

As you noted, in Firefox everything works as you expect. But in Chrome, the min-height is effectively ignored (even though flex-grow: 1 works on the same element).

If you switch to height: 100vh you'll see the 1fr work in Chrome, as well.

I would have to research more to tell you if this is a bug or not.

Consider nesting the grid inside another grid, as opposed to flex, container.

.container {
  display: grid;
  min-height: 100vh;
  grid-template-rows: min-content 1fr;
}

.grid {
  background-color: red;
  padding: 1rem;
  grid-gap: 1rem;
  display: grid;
  grid-template-rows: 1fr auto;
}

.grid>* {
  background-color: white;
}
<div class="container">
  <h1>some title</h1>
  <div class="grid">
    <div>line 1</div>
    <div>line 2</div>
  </div>
</div>

jsFiddle demo

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • 1
    indeed, nesting the grid inside a grid instead of a flex container also solved the problem. I will also do some research to find if this is a bug. Please let me know if you find something. thx :) – rocambille May 17 '21 at 19:28
  • 1
    @rgmt I also vote for a bug (like here too: https://stackoverflow.com/a/67332926/8620333) and I got the confirmation that the grid layout is buggy and being reworked: https://stackoverflow.com/a/67011770/8620333 – Temani Afif May 17 '21 at 20:12