Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does grid-gap cause an overflow?

Tags:

css

css-grid

Why do I have an overflow on the X axis in the following snippet?

The overflow is generated once I apply grid-gap: 10px on my .body grid container.

div:not(.header):not(.body):not(.row) {    border: 1px solid grey;  }    .header {    margin-top: 20px;    display: grid;    grid-gap: 10px;    grid-template-areas: "header-left header-right-up" "header-left header-right-down";    grid-template-rows: 40px 40px;    grid-template-columns: minmax(50px, 200px) auto;  }    .header-left {    grid-area: header-left;  }    .header-right-up {    grid-area: header-right-up;  }    .header-right-down {    grid-area: header-right-down;  }    .body {    margin-top: 20px;    display: grid;    grid-template-columns: 25% 50% 25%;    grid-auto-rows: 80px;     grid-gap: 10px;  }    .row-left {  }    .row-center {  }    .row-right {  }
<div class="header">    <div class="header-left">image</div>    <div class="header-right-up">content</div>    <div class="header-right-down">long content</div>  </div>    <div class="body">      <div class="row-left"></div>      <div class="row-center"></div>      <div class="row-right"></div>        <div class="row-left"></div>      <div class="row-center"></div>      <div class="row-right"></div>        <div class="row-left"></div>      <div class="row-center"></div>      <div class="row-right"></div>    </div>  </div>

https://codepen.io/anon/pen/WdJExz?editors=1100

like image 351
Damiano Barbati Avatar asked Jan 11 '18 19:01

Damiano Barbati


People also ask

What does grid-gap do?

The gap CSS property sets the gaps (gutters) between rows and columns. It is a shorthand for row-gap and column-gap .

Should I use grid-Gap or Gap?

gap is intended to replace it so that gaps can be defined in multiple CSS layout methods, like flexbox, but grid-gap still needs to be used in instances where a browser may have implemented grid-gap but has yet to start supporting the newer gap property.

What does grid column-gap mean?

Definition and Usage The grid-column-gap property defines the size of the gap between the columns in a grid layout. Note: This property was renamed to column-gap in CSS3.

Is grid column-gap obsolete?

'grid-gap' has been deprecated in favor of 'gap'. In a similar vein 'grid-row-gap' and 'grid-column-gap' have been deprecated in favor of 'row-gap' and 'column-gap'. Unfortunately, the new features aren't recognized by Visual Studio yet either. As far as May 2020, 'grid-gap' is shown as "Obsolete" in VSC.


2 Answers

You have a 3-column grid container (.body):

grid-template-columns: 25% 50% 25% 

The total width of those columns is 100%.

You're then adding gutters between the columns (and rows):

grid-gap: 10px 

which is shorthand for:

grid-column-gap: 10px; grid-row-gap: 10px; 

So when you add up all the widths you get:

25% + 50% + 25% + 10px + 10px 

Put more briefly:

100% + 20px > 100%, which results in an overflow condition 

(Note: grid-*-gap properties apply only between grid items – never between items and the container. That's why we calculate two grid gaps, not four.)

As a solution, instead of percentage units, try using fr units, which apply only to free space. This means that fr lengths are calculated after any grid-gap lengths are applied.

grid-template-columns: 1fr 2fr 1fr 

div:not(.header):not(.body):not(.row) {    border: 1px solid grey;  }    .header {    margin-top: 20px;    display: grid;    grid-gap: 10px;    grid-template-areas: "header-left header-right-up" "header-left header-right-down";    grid-template-rows: 40px 40px;    grid-template-columns: minmax(50px, 200px) auto;  }    .header-left {    grid-area: header-left;  }    .header-right-up {    grid-area: header-right-up;  }    .header-right-down {    grid-area: header-right-down;  }    .body {    margin-top: 20px;    display: grid;    grid-template-columns: 1fr 2fr 1fr; /* ADJUSTMENT */    grid-auto-rows: 80px;    grid-gap: 10px;  }    .row-left {}    .row-center {}    .row-right {}
<div class="header">    <div class="header-left">image</div>    <div class="header-right-up">content</div>    <div class="header-right-down">long content</div>  </div>    <div class="body">    <div class="row-left"></div>    <div class="row-center"></div>    <div class="row-right"></div>      <div class="row-left"></div>    <div class="row-center"></div>    <div class="row-right"></div>      <div class="row-left"></div>    <div class="row-center"></div>    <div class="row-right"></div>  </div>

revised codepen demo

More details here: The difference between percentage and fr units in CSS Grid Layout

like image 51
Michael Benjamin Avatar answered Oct 17 '22 12:10

Michael Benjamin


If you want to use percentages or some other unit, there is one more solution. The use of the minmax() function will allow the columns to shrink to fit the parent container and not cause overflow.

The solution would look like this:

grid-template-columns: minmax(auto, 25%) minmax(auto, 50%) minmax(auto, 25%); gap: 10px; 

More on the minmax() function here.

like image 45
Valentin Genev Avatar answered Oct 17 '22 11:10

Valentin Genev