Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Grid does not grow when inside flexbox

My understanding of CSS Grid is that it will grow to fill its parent, however that does not seem to work correctly when the parent's size is controlled by a flexbox.

In this example, I expected the light blue grid items to grow to cover the red area. This works correctly if the grid is on it's own, however it does not grow at all once it is placed into a flex element (even with flex-grow: 1);

What is happening here and is there any way to adjust the style to behave as I expect?


Code:

Layout

<div class="normal-grid">
  <div class="grid-item">Thing 1</div>
  <div class="grid-item">Thing 2</div>
</div>
<div class="flex">
  <div class="flex-grid">
    <div class="grid-item">Thing 1</div>
    <div class="grid-item">Thing 2</div>
  </div>
</div>

Style

.normal-grid {
  height: 100px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr;
  background: red;
}

.flex {
  margin-top: 20px;
  height: 100px;
  display: flex;
  flex-direction: column;
}

.flex-grid {
  flex-grow: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr;
  background: red;
}

.grid-item {
  background: lightblue;
}
like image 651
Vlad274 Avatar asked Aug 22 '18 20:08

Vlad274


People also ask

Can I use CSS Grid inside flexbox?

You can combine them by using a Flexbox container inside a CSS Grid container. Note, however, that you cannot use a CSS Grid Container inside a Flexbox container.

How do I make my flexbox grid responsive?

Creating the responsive gridUsing display: flex , our grid-row stretches to the full size of the container. We use flex-flow: wrap to designate that child divs (our columns/grid-items) should wrap if they exceed the width of the row. Then, flex-flow: row means our grid-items will flex from left to right.

What can flexbox do that grid cant?

For example, a flexbox can layout the elements either in a row or column i.e. horizontally or vertically but not both at the same time whereas grids can layout the components horizontally and vertically i.e. grid allows to place the elements on rows and columns simultaneously.

What are the disadvantages of using flexbox?

One of the major drawbacks of using flexbox is performance issues. The use of flexbox can increase the page loading time if you have a larger project. You can compare the page loading time through Chrome development tools using the flexbox and the same pages constructed without using flexbox.


2 Answers

Simply keep the row direction (the default one) and you will have the stretch effect:

.normal-grid {
  height: 100px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr;
  background: red;
}

.flex {
  margin-top: 20px;
  height: 100px;
  display: flex;
  /*flex-direction: column;*/
}

.flex-grid {
  flex-grow: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr;
  background: red;
}

.grid-item {
  background: lightblue;
}
<div class="normal-grid">
  <div class="grid-item">Thing 1</div>
  <div class="grid-item">Thing 2</div>
</div>
<div class="flex">
  <div class="flex-grid">
    <div class="grid-item">Thing 1</div>
    <div class="grid-item">Thing 2</div>
  </div>
</div>
like image 107
Temani Afif Avatar answered Oct 23 '22 05:10

Temani Afif


You can add flex-basis: 100%; to .flex-grid in order to get the behavior you wish for.

.normal-grid {
  height: 100px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr;
  background: red;
}

.flex {
  margin-top: 20px;
  height: 100px;
  display: flex;
  flex-direction: column;
}

.flex-grid {
  flex-grow: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr;
  background: red;
  flex-basis: 100%;
}

.grid-item {
  background: lightblue;
}
<div class="normal-grid">
  <div class="grid-item">Thing 1</div>
  <div class="grid-item">Thing 2</div>
</div>
<div class="flex">
  <div class="flex-grid">
    <div class="grid-item">Thing 1</div>
    <div class="grid-item">Thing 2</div>
  </div>
</div>

When elements are controlled by a flexbox, their initial dimensions have to come into consideration. in this case, .flex-grid seems to be shrinking - so adding flex-basis: 100% will tell the browser that its initial size should be 100% of its size.

like image 35
Itay Ganor Avatar answered Oct 23 '22 05:10

Itay Ganor