Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Grid inside Flexbox not working as expected only on Chrome

I'm trying to create a flexbox that contains a header and a 2x2 CSS grid.

  • The flexbox (.container) fills the entire viewport.
  • The height of the header is variable. It may change dynamically while the page is open.
  • The four cells in the grid must occupy the remaining part of the viewport and they must have the equal width and height.

Here's what I have now:

html,
body {
  height: 100%;
  padding: 0;
  margin: 0;
}

* { box-sizing: border-box; }

.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  border: 2px solid red;
}

.header {
  background: lime;
}

.grid {
  flex: auto; /* fills the remaining part of the viewport below header */
  background: cyan;
  display: grid;
  grid-template: 1fr 1fr / 1fr 1fr;
  grid-gap: 2px;
}

.cell {
  padding: 10px;
  background: linear-gradient(to bottom right, orange, white);
}
<div class="container">
  <div class="header">Variable Height<br />Header</div>
  <div class="grid">
    <div class="cell a">Cell A</div>
    <div class="cell b">Cell B</div>
    <div class="cell c">Cell C</div>
    <div class="cell d">Cell D</div>
  </div>
</div>

This works as I expect on Firefox, but not on Chrome. Here's the desired behavior:

Desired output

And here's the undesired behavior on Chrome:

Wrong output

The confusing part is that my div.grid (cyan) has a desired height (100vh minus the header height), so the flexbox itself is working correctly. And when I remove the flexbox and the header, the grid itself works as expected outside flexbox, too.

html,
body {
  height: 100%;
  padding: 0;
  margin: 0;
  font-family: sans-serif;
}

* { box-sizing: border-box; }

.container {
  height: 100%;
  background-color: pink;
  border: 2px solid red;
}

.grid {
  height: 100%;
  background: cyan;
  display: grid;
  grid-template: 1fr 1fr / 1fr 1fr;
}

.cell {
  padding: 10px;
  background: linear-gradient(to bottom right, orange, white);
}
<div class="container">
  <div class="grid">
    <div class="cell a">Cell A</div>
    <div class="cell b">Cell B</div>
    <div class="cell c">Cell C</div>
    <div class="cell d">Cell D</div>
  </div>
</div>

So it appears to me that the problem happens only by the combination of Chrome, flexbox and css grid. What am I missing, and how can I fix this? (Please note that including the header in the grid is not an option now.)

like image 539
naruto Avatar asked Oct 04 '18 11:10

naruto


People also ask

Can I use grid inside flexbox?

Of course CSS grid and flexbox can work together in a layout. You can use flexbox inside CSS grid and vice versa. For instance, you could use flexbox to center an element like a button both vertically and horizontally within a particular grid cell (since centering with other CSS methods is … tricky).

Does flexbox work on all browsers?

Flexbox is very well supported across modern browsers, however there are a few issues that you might run into.

Is CSS grid all browsers compatible?

Most developers are afraid to start using CSS Grid because of browser support, but CSS grid is fully supported in all main browsers: Chrome, Firefox, Safari, Edge including their mobile versions.


2 Answers

Instead of flex: auto on grid, use flex: 1 on it so that the grid, and you have the desired behavior in both Firefox and Chrome. See demo below:

html,
body {
  height: 100%;
  padding: 0;
  margin: 0;
}

* { box-sizing: border-box; }

.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  border: 2px solid red;
}

.header {
  background: lime;
}

.grid {
  flex: 1; /* fills the remaining part of the viewport below header */
  background: cyan;
  display: grid;
  grid-template: 1fr 1fr / 1fr 1fr;
  grid-gap: 2px;
}

.cell {
  padding: 10px;
  background: linear-gradient(to bottom right, orange, white);
}
<div class="container">
  <div class="header">Variable Height<br />Header</div>
  <div class="grid">
    <div class="cell a">Cell A</div>
    <div class="cell b">Cell B</div>
    <div class="cell c">Cell C</div>
    <div class="cell d">Cell D</div>
  </div>
</div>
like image 101
kukkuz Avatar answered Nov 15 '22 15:11

kukkuz


<style>
    html,
    body {
      height: 100%;
      padding: 0;
      margin: 0;
    }

    * { box-sizing: border-box; }

    .container {
      height: 100%;
      display: flex;
      flex-direction: column;
      border: 2px solid red;
    }

    .header {
      background: lime;
    }

    .grid {
      flex: 1; /* fills the remaining part of the viewport below header */
      background: cyan;
      display: grid;

      grid-template-columns:1fr 1fr;
   
      grid-gap: 2px;
    }

    .cell {
      padding: 10px;
      background: linear-gradient(to bottom right, orange, white);
    }
</style>
    </head>
    </body>
<html>
    </head>
<!-- begin snippet: js hide: false console: true babel: null -->
like image 22
designcoded Avatar answered Nov 15 '22 15:11

designcoded