liveBook · Manning

Repeat()

As explained in CSS in Depth, Second Edition

The CSS repeat() function represents repeated grid track sizes or patterns in a grid-template declaration.

The CSS repeat() function abbreviates repeated grid tracks or repeated track patterns in a grid-template declaration. It makes long grid definitions easier to read while preserving the same layout. section 5.2

Repeat identical tracks

grid-template-rows: repeat(4, auto) creates four auto-sized rows—the same as auto auto auto auto. Each row grows as needed to fit its content. section 5.2

Repeat a pattern

repeat(3, 2fr 1fr) expands to 2fr 1fr 2fr 1fr 2fr 1fr, producing six tracks. A longer declaration can combine fixed tracks and repetition: 1fr repeat(3, 3fr) 1fr. section 5.2

The repeated value can include named grid lines as well as track sizes. For example, repeat(4, [row] auto) gives each repeated horizontal grid line the name row, allowing placement such as grid-row: row 3 / span 2. section 5.3.1

Responsive card grid

grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)) lets the grid determine how many equal-width columns fit the viewport, while smaller layouts retain stacked cards. section 17.2.1

How CSS in Depth, Second Edition covers it

How deep it goes
Explains shorthand expansion, repeated patterns, named lines, and responsive auto-fit, without descending into browser layout internals.
Examples
Uses four auto rows, repeated fractional patterns, named row lines, and responsive card columns as concrete examples.
What it assumes
Assumes CSS Grid and grid-template declarations while defining repeat() and its basic track-expansion behavior.
What you can do after
Apply repeat() to write compact fixed, patterned, named-line, and responsive CSS Grid track definitions.
How it connects
Connects grid-template declarations with grid tracks, fractional units, named grid lines, minmax(), auto-fit, and responsive layouts.

Also explained in

Compare how 3 books explain repeat() →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.