How to limit the amount of columns in larger viewports with CSS Grid and auto-fill/fit?

前端 未结 3 1151
一生所求
一生所求 2020-12-19 18:22

I\'m starting to work with CSS Grid, I\'ve been reading about the properties to help with responsiveness; so I\'m trying to build a small grid with 6 elements; my intention

3条回答
  •  孤城傲影
    2020-12-19 19:17

    Use this syntax:

    grid-template-columns: 260px 260px 260px;

    Or

    grid-template-columns: repeat(3,260px);

    Instead of this:

    grid-template-columns: repeat(auto-fill, 260px);

    Use media queries to set less columns on smaller screens.

    Also if the row and column gap is the same you can use grid-gap.

    Documentation

    .grid-container{
      display: grid;
      grid-template-columns: 260px 260px 260px;
      grid-gap: 18px;
      background-color: #fff;
      color: #444;
      justify-content: center; 
    }
    
    .card {
       border: 1px solid #000;
       width: 260px;
       height: 260px;
    }

提交回复
热议问题