repeat()

Repeats a pattern of grid columns or rows. With auto-fill or auto-fit and minmax() it creates a responsive grid without media queries.

On this page

Example

#
CSS
.courses {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(140px, 100%), 1fr));
  gap: 10px;
}
Preview

The grid decides the number of columns itself: more fit on a computer, fewer on a phone. There is not a single media query here.

Definition and usage

#

The repeat() function shortens the notation of tracks in a CSS Grid. Instead of 1fr 1fr 1fr 1fr you write repeat(4, 1fr), and instead of 100px 1fr 100px 1fr simply repeat(2, 100px 1fr). It works in grid-template-columns and grid-template-rows, and can be combined with regular values, for example 200px repeat(3, 1fr).

Instead of a number you can write auto-fill or auto-fit. The browser then calculates by itself how many columns fit in the container. Paired with minmax() this gives a responsive grid without media queries: repeat(auto-fill, minmax(160px, 1fr)) creates as many columns at least 160px wide as will fit, and divides the excess space equally between them.

The minmax(MIN, MAX) function sets the size range of a single track: the column won’t be narrower than MIN or wider than MAX. 1fr, an equal share of the free space, is often used as the maximum. On a very narrow screen the minimum can be wider than the whole container, so it is safer to write minmax(min(160px, 100%), 1fr).

The difference between auto-fill and auto-fit shows when there are few items. auto-fill creates spare columns, even empty ones, so the items keep their size. auto-fit collapses empty columns to zero and lets the existing items stretch across the full width. With many items both versions look the same.

Syntax

#
Syntax
repeat(<number> | auto-fill | auto-fit, <track size>+)
minmax(<minimum>, <maximum>)

Common patterns

#
  • repeat(3, 1fr)

    three equal columns

    Three columns, each an equal share of the width. Six tiles arrange themselves into two rows.
  • repeat(2, 60px 1fr)

    a repeated pattern

    You can repeat a whole pattern. Here a pair twice: a narrow 60 px column and a wide 1fr one, four columns in total.
  • repeat(3, minmax(50px, 90px))

    columns from 50 to 90 px

    Each column is between 50 and 90 px wide. In a wide frame they stop at 90 px, and when you narrow it (drag the bottom-right corner) they shrink, but not below 50 px.
  • repeat(auto-fill, minmax(70px, 1fr))

    spare columns

    The browser creates as many columns as fit, even empty ones. Two tiles take only part of the width and the rest stays free. Narrow the frame to see the columns disappear one by one.
  • repeat(auto-fit, minmax(70px, 1fr))

    empty columns disappear

    Empty columns collapse to zero, so the same two tiles stretch across the whole width. Compare with auto-fill next to it.

Common patterns in practice

#

repeat(3, 1fr)

Three columns, each an equal share of the width. Six tiles arrange themselves into two rows.
.demo {
  grid-template-columns: repeat(3, 1fr);
}

repeat(2, 60px 1fr)

You can repeat a whole pattern. Here a pair twice: a narrow 60 px column and a wide 1fr one, four columns in total.
.demo {
  grid-template-columns: repeat(2, 60px 1fr);
}

repeat(3, minmax(50px, 90px))

Each column is between 50 and 90 px wide. In a wide frame they stop at 90 px, and when you narrow it (drag the bottom-right corner) they shrink, but not below 50 px.
.demo {
  grid-template-columns:
    repeat(3, minmax(50px, 90px));
}

repeat(auto-fill, minmax(70px, 1fr))

The browser creates as many columns as fit, even empty ones. Two tiles take only part of the width and the rest stays free. Narrow the frame to see the columns disappear one by one.
.demo {
  grid-template-columns:
    repeat(auto-fill, minmax(70px, 1fr));
}

repeat(auto-fit, minmax(70px, 1fr))

Empty columns collapse to zero, so the same two tiles stretch across the whole width. Compare with auto-fill next to it.
.demo {
  grid-template-columns:
    repeat(auto-fit, minmax(70px, 1fr));
}

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • With auto-fill and auto-fit the minimum in minmax() must be fixed, for example 160px, not auto or 1fr. Otherwise the browser can’t calculate how many columns fit.
  • When there are few items and they should stretch across the full width, choose auto-fit. When they should keep their size, choose auto-fill.
  • The expression minmax(min(160px, 100%), 1fr) protects against horizontal scrolling on screens narrower than 160 px.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.