Why does grid-gap cause an overflow?

前端 未结 3 1183
一个人的身影
一个人的身影 2021-01-01 12:14

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 co

3条回答
  •  爱一瞬间的悲伤
    2021-01-01 12:48

    If you have a fixed number of grid columns that should be the same width and use grid-gap, there's a way to do it:

    display: grid;
    grid-template-columns: repeat(4, calc(25% - 0.75em));
    grid-gap: 1em;
    

    In this case, each row would have 4 grid columns, and the total space occupied by grid-gap would be 3em. In order to distribute the overflowing space evenly, divide the space by the number of grid columns.

    3em / 4 = 0.75em

    This makes all the grid columns have the same width.

    Codepen demo

提交回复
热议问题