0

I have this layout:

enter image description here

Basically the HTML and CSS is the following:

<style>
  section { display: grid, grid-template-columns: repeat(3, 1fr) };
  div::first-child: { grid-column: 1/3}
</style>
<section>
  <div>...</div>
  <div>...</div>
  <div>...</div>
  <div>...</div>
  <div>...</div>
</section>

when the viewport becomes smaller, I want the grid to have 2 columns and then 1 column. I could use media-queries and redefine the number of columns but I'm wondering if there's an easy way to do that using auto-fill function?

This answer shows how to do it by defining the width of the columns, but ideally I wouldn't want to do that and have the width based on the available space for the number of columns.

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

1 Answers1

0

This little tidbit works pretty well. You have to have a min-width, and that controls the breakpoints. We generally set this to be approx 100% (less margins) of a mobile viewport screen-ish.

grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57