Percentage Grid-Gap Overflow in CSS
In CSS Grid, setting the grid-gap property to a percentage value can lead to unexpected overflows. This issue arises because browsers handle percentage grid gaps differently.
Browser Behavior
Initially, the browser calculates the grid height, taking into account the content within the grid cells. However, it ignores the percentage grid gap (treating it as auto). This calculation results in a tight spacing between grid cells.
Calculating Height
To calculate the grid height, the browser evaluates the height of each grid cell and adds the content's margins and paddings, but not the grid gap:
console.log(document.querySelector('.grid').offsetHeight);
Resolving the Issue
To avoid overflows, consider the following approaches:
Additional Note
Opacity does not affect grid cell height calculations. If you need to hide or display grid items dynamically, use a different approach such as display: none.
The above is the detailed content of Why does using a percentage grid-gap in CSS Grid result in unexpected overflows?. For more information, please follow other related articles on the PHP Chinese website!