grid-template-columns

Term side of the Card.
Topic: Grids
Unit title: grid-template-columns
Categories: Property
Explanation side of the Card

Explanation: The property that defines the width of each column in a grid layout. Specific sizes, flexible units and repeating patterns can be applied, allowing precise control over the column structure and space distribution in the grid layout.

Browser compatibility:
  • The chrome browser supports this feature starting from version 57.
  • The edge browser supports this feature starting from version 16.
  • The firefox browser supports this feature starting from version 52.
  • The safari browser supports this feature starting from version 10.1.
Availability: 

Example

Wrongcss
.cards {
  display: grid;
  grid-template-columns: 25% 25% 25% 25%;
  gap: 16px;
}
Rightcss
.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.
  • 1fr columns 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 fr instead of percentages, so gap doesn'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.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy