What Is CSS repeat(auto-fit, minmax()) Used For?

The CSS repeat(auto-fit, minmax(...)) pattern is a responsive design technique that automatically calculates and adjusts grid columns based on available container space without requiring CSS media queries. By combining track repetition with dynamic minimum and maximum boundaries, this pattern allows child elements to wrap, shrink, and expand smoothly across all screen sizes, creating fluid multi-column card grids, product lists, and galleries.

Anatomy of the Pattern

To understand how the pattern functions as a whole, it helps to examine its core CSS Grid components:

Standard Implementation

The standard implementation pairs a fixed minimum size (such as 250px or 15rem) with a fractional unit (1fr) for the maximum limit:

.grid-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1.5rem;
}

In this setup:

  1. The browser checks the total width of .grid-container.
  2. It calculates how many 250px tracks can fit along the row while accounting for the 1.5rem gap.
  3. If there is leftover horizontal space, the 1fr maximum value distributes that remaining space equally across the existing columns.
  4. When the viewport narrows and a column cannot maintain its 250px minimum, the grid wraps that item to a new line and expands the remaining items to fill the previous row.

auto-fit vs. auto-fill

While both keywords create dynamic tracks, they handle surplus row space differently when there are fewer items than the container can hold:

Common Use Cases