Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do grid auto-fit and auto-fill change a responsive card layout?

Quick answer

Both auto-fit and auto-fill create as many repeated columns as fit the container. If there’s space for more columns than you have cards, auto-fill keeps the unused tracks, while auto-fit collapses them after placing items. With flexible 1fr tracks, that means existing cards can grow wider with auto-fit; with auto-fill, empty tracks remain part of the layout. Choose based on whether you want that space preserved.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have a responsive card grid. What’s the actual difference between auto-fit and auto-fill?

Geez

Both modes repeat as many columns as can fit, subject to the track sizing and the available container width. The difference shows up when there’s room for more columns than there are cards: auto-fill leaves the unoccupied tracks in place, while auto-fit collapses empty tracks after items are placed. A track counts as empty only if no in-flow grid item is placed in it or spans across it. [S1]

With flexible 1fr tracks, the practical result is usually that cards using auto-fit expand to use the space freed by collapsed tracks. With auto-fill, the empty tracks still take part in the layout, so occupied cards may stay narrower. [S1] The difference is about the grid’s behavior, not a different number of cards.

Follow-up question

Can you show both versions with the same card sizing so I can compare them?

Geez

Yes. Keep the minimum card size and gap the same, and change only the repeat mode:

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

.cards--fill {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;
}

Here, 200px is an example minimum, not a universal recommendation. A minmax() track gives each column a minimum size and lets it grow; the 1fr maximum distributes available space among flexible tracks. [S2] If you have two cards but the container can fit four columns at that minimum, the auto-fit version can collapse the two empty tracks and let the occupied columns grow. The auto-fill version preserves the empty tracks. The exact widths still depend on the container, gap, minimum size, and items. [S1]

Follow-up question

When would I want to keep those empty tracks instead of letting the cards stretch?

Geez

Choose auto-fill when preserving the column pattern matters more than making a small number of cards fill the row. For example, if a listing may have only a few results but you want its columns to keep the same general placement as the grid gains items, test auto-fill with your real container width and minimum card size.

Choose auto-fit when you’d rather the available cards use the row’s spare space. That can be a better visual fit for a short row, but a card may become much wider than it is in a full row. Neither choice guarantees a particular card width: track sizing and the layout determine the result. [S1]

Follow-up question

How can I decide without guessing, especially when the row has only one or two cards?

Geez

Use a small comparison with your actual card content. Keep the markup, minmax() values, and gap identical; test both modes at the same wide container size with one card, two cards, and a full row. Check whether the cards become uncomfortably wide with auto-fit, or look unexpectedly narrow with auto-fill. Then check a narrow container too, since fewer columns will fit there.

For example, if your grid fits four minimum-width columns but currently has two cards, the key question is: should those two cards expand into the unused space, or should the layout retain the empty column tracks? That’s the choice the two declarations make. The behavior described here does not establish a universally better option; the intended appearance and your track sizing decide.

A practical checklist

  • Keep the card minimum size and gap identical while comparing both modes.

  • Test a wide container with one card, two cards, and a full row.

  • Check whether sparse rows look better with wider cards or preserved empty tracks.

  • Repeat the comparison at a narrow width and with your real card content.

  • Choose auto-fit to collapse empty tracks, or auto-fill to preserve them.

A few more useful answers

Do `auto-fit` and `auto-fill` always produce different layouts?

No. If there are no empty repeated tracks to collapse, their behavior can look the same. The distinction matters when the grid can fit more tracks than the items occupy. [S1]

Will `auto-fit` always make cards wider?

Not necessarily. It collapses empty tracks, but the result depends on track sizing and the items. The expansion described here is the typical effect when the tracks have a flexible 1fr maximum. [S1]

Is `200px` the right minimum for every card grid?

No. It is just an example. Choose a minimum that works for your content and container, then test the resulting layout at the widths your page needs.

Sources

References used for this guide.

  1. [S1] repeat() CSS function reference
    developer.mozilla.org · Checked
  2. [S2] Realizing common layouts using grids
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work