repeat()

Term side of the Card.
Topic: Grids
Unit title: repeat()
Categories: Function
Explanation side of the Card

Explanation: The function used in grid layouts to repeat a pattern of columns or rows. It simplifies the grid definition by enabling to specify how many times a certain pattern should repeat, making it easier to create complex, responsive layouts.

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 76.
  • The safari browser supports this feature starting from version 10.1.
Availability: 

Example

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

Auto-repeat needs a track with a definite minimum, so minmax(200px, 1fr) makes it valid.

Common mistakes

  • Writing repeat(auto-fit, 1fr), which is invalid.
  • Expecting auto-fill to stretch items when there are few of them.

Key points

  • Responsive grids use auto-fill or auto-fit with minmax() inside repeat().
  • auto-fill and auto-fit are not combined with flexible-only tracks like repeat(auto-fill, 1fr).

Interview questions

auto-fill vs auto-fit in repeat(): what is the difference?

Both fit as many tracks as possible. auto-fit collapses empty tracks so items stretch, while auto-fill keeps them.

Can repeat() hold more than one track?

Yes. repeat(3, 100px 1fr) repeats the two-track pattern three times.

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