CSS grid wrapping

前端 未结 5 907
借酒劲吻你
借酒劲吻你 2020-12-04 04:53

Is it possible to make a CSS grid wrap without using media queries?

In my case, I have a non-deterministic number of items that I want placed in a grid and I want th

5条回答
  •  暖寄归人
    2020-12-04 05:58

    You want either auto-fit or auto-fill inside the repeat() function:

    grid-template-columns: repeat(auto-fit, 186px);
    

    The difference between the two becomes apparent if you also use a minmax() to allow for flexible column sizes:

    grid-template-columns: repeat(auto-fill, minmax(186px, 1fr));
    

    This allows your columns to flex in size, ranging from 186 pixels to equal-width columns stretching across the full width of the container. auto-fill will create as many columns as will fit in the width. If, say, five columns fit, even though you have only four grid items, there will be a fifth empty column:

    Using auto-fit instead will prevent empty columns, stretching yours further if necessary:

提交回复
热议问题