CSS Snippet

CSS Grid Responsive Layout Snippet

Difficulty: Easy

A responsive card grid that fills as many columns as fit and never produces an awkward last row is a one-line CSS Grid trick. This snippet covers the canonical `auto-fill` plus `minmax` recipe, the `auto-fit` variant that collapses empty tracks, and a row-spanning variant for masonry-like layouts. No media queries, no JavaScript.

Code Snippets
/

CSS Grid Responsive Layout Snippet

CSS Grid Responsive Layout Snippet

A responsive card grid that fills as many columns as fit and never produces an awkward last row is a one-line CSS Grid trick. This snippet covers the canonical `auto-fill` plus `minmax` recipe, the `auto-fit` variant that collapses empty tracks, and a row-spanning variant for masonry-like layouts. No media queries, no JavaScript.

CSS
Easy
3 snippets
css-selectors
css-grid
css-responsive-design
code-template

458 views

15

repeat(auto-fill, minmax(240px, 1fr)) tells the grid to lay out as many tracks as fit at a minimum of 240px and let each one stretch up to 1fr. The result is a fluid grid that reflows from one column on a phone to four or more on a wide monitor without a single media query. gap handles the spacing instead of margins, which keeps the math simple and avoids extra spacing at the container edges. This is the recipe to memorise; everything else in this snippet is a variation on it.