Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

repeat(): repeated grid tracks and separate gap-color patterns

Repeat a track pattern, understand auto-fill versus auto-fit, and respect the grammar of each property.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));

In a grid track list, repeat() repeats a pattern containing one or more tracks and optional line names. A fixed repetition count must be a positive integer: zero is not an empty pattern, and nesting this function is not allowed. The automatic forms require the restricted fixed-size grammar. Thus repeat(auto-fill, 1fr) is invalid, while repeat(auto-fill, minmax(100px, 1fr)) is permitted because the minimum supplies a definite length. Only one automatic repetition is allowed in a track list.

The auto-fill form retains the tracks that fit the available space. After placement, auto-fit collapses empty repeated tracks and their adjacent gutters, so occupied flexible tracks can grow. It does not remove occupied tracks. With no definite sizing constraint, an automatic count must not be assumed to fill an arbitrary width. For a subgrid, the separate name-repeat form repeats line-name lists, not new track sizes.

The example creates two items in a 350px-wide grid with a 10px gap. A fixed two-track pattern repeats three times. The automatic forms produce three 110px tracks or two 170px tracks plus one collapsed 0px track. The rejected forms demonstrate the restrictions rather than a browser failure.

CSS Gaps also defines repeat() for separator colors, including repeat(2, red, blue) and an automatic form using auto, not the grid keywords. This is a distinct value grammar for properties such as column-rule-color. Chromium 157 accepts the tested color syntax, but that final probe does not verify painting or support across other engines; the CSS Gaps source remains a draft.

Examples

const grid = document.createElement('div');
grid.style.cssText = 'display:grid;width:350px;gap:10px';
grid.append(document.createElement('div'), document.createElement('div'));
document.body.append(grid);
for (const value of ['repeat(3,40px 20px)',
  'repeat(auto-fill,minmax(100px,1fr))',
  'repeat(auto-fit,minmax(100px,1fr))']) {
  grid.style.gridTemplateColumns = value;
  console.log(value + ':', getComputedStyle(grid).gridTemplateColumns);
}
for (const value of ['repeat(0,10px)', 'repeat(2,repeat(2,10px))',
  'repeat(auto-fill,1fr)', 'repeat(auto-fill,minmax(100px,1fr))']) {
  console.log('accepts ' + value + ':', CSS.supports('grid-template-columns', value));
}
console.log('gap color syntax:', CSS.supports('column-rule-color', 'repeat(2,red,blue)'));
grid.remove();

Result

repeat(3,40px 20px): 40px 20px 40px 20px 40px 20px
repeat(auto-fill,minmax(100px,1fr)): 110px 110px 110px
repeat(auto-fit,minmax(100px,1fr)): 170px 170px 0px
accepts repeat(0,10px): false
accepts repeat(2,repeat(2,10px)): false
accepts repeat(auto-fill,1fr): false
accepts repeat(auto-fill,minmax(100px,1fr)): true
gap color syntax: true

Grid values are measured after layout, including the collapsed track. The gap-color result is only a syntax-support observation and must not be presented as a painted separator test.

Sources