grid-template-columns
Example
.cards {
display: grid;
grid-template-columns: 25% 25% 25% 25%;
gap: 16px;
}.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(min(250px, 100%), 1fr));
gap: 16px;
}Percentages plus gap overflow; auto-fill with minmax() fits as many columns as space allows.
Common mistakes
- Using
25% 25% 25% 25%with a gap, causing horizontal overflow. 1frcolumns grow wider than expected because of long words.
Key points
- Responsive card grids use
repeat(auto-fill, minmax(min(250px, 100%), 1fr)). - Flexible tracks use
frinstead of percentages, sogapdoesn't cause overflow. minmax(0, 1fr)is used when columns hold long text or code that must not widen the track.
Interview questions
auto-fill vs auto-fit: what is the difference?
auto-fill keeps empty tracks when there are few items. auto-fit collapses empty tracks, so existing items stretch to fill the row.
Why is my 1fr column wider than the others?
1fr has an automatic minimum, so long content pushes it wider. Use minmax(0, 1fr) to allow it to shrink.