Favorites

repeat() CSS Function

repeat()
CSS FunctionThis function is supported by all modern browsers.
Updated 21st November 2026

The repeat() function repeats a pattern of grid tracks, such as repeat(3, 1fr), or as many as fit with auto-fill or auto-fit.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

Description

The repeat() CSS function repeats a pattern of grid tracks a given number of times, so you do not have to write the same track sizes over and over. It is used in grid-template-columns and grid-template-rows. For example, repeat(4, 1fr) is the same as 1fr 1fr 1fr 1fr, and repeat(2, 100px 1fr) is the same as 100px 1fr 100px 1fr.

The first argument is the number of repetitions, either a positive integer or one of the keywords auto-fill and auto-fit. With these keywords the browser creates as many tracks as will fit in the container. The difference appears when there are fewer items than tracks: auto-fill keeps the empty tracks, while auto-fit collapses them so the existing items can stretch to fill the row. The second argument is the track list to repeat, which can include sizes, minmax(), fit-content() and named grid lines in square brackets.

/* Twelve-column layout */
.page {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
}

/* Repeating a pattern with named lines */
.table {
  grid-template-columns: repeat(3, [col-start] 1fr [col-end]);
}

/* As many 160px+ columns as fit */
.gallery {
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
}

Interactive Demo

One
Two
Three
Four
Five
Six
Demo Code
HTML Code
<div class="demo-element">
	<div>One</div>
	<div>Two</div>
	<div>Three</div>
	<div>Four</div>
	<div>Five</div>
	<div>Six</div>
</div>
CSS Code
.demo-element > div {
	padding: 8px;
	background: #4361ee;
	color: #fff;
	font-size: 13px;
}
.demo-element > div:nth-child(2n) {
	background: #f72585;
}
.demo-element {
	display: grid;
	width: 300px;
	gap: 6px;
	padding: 6px;
	box-sizing: border-box;
	background: #f4f4f4;
	outline: 1px dashed #b0b0b0;
	grid-template-columns: repeat(3, 1fr);
}

Syntax

repeat() = repeat( [ <integer [1,∞]> | auto-fill | auto-fit ], <track-list> )

Values

  • <integer>The number of times to repeat the track list. Must be 1 or more.
  • auto-fillRepeats the tracks as many times as will fit in the container without overflowing. Empty tracks are kept.
  • auto-fitLike auto-fill, but empty repeated tracks collapse to 0, so the filled tracks can grow to use the space.
  • <track-list>One or more track sizes to repeat, such as 1fr, 100px, minmax(), fit-content(), and optional [line-names].

Example

<div class="wrap">
<h1>CSS repeat() examples</h1>
<div class="card">
<div class="g four"><div>1fr</div><div>1fr</div><div>1fr</div><div>1fr</div></div>
<div class="label">repeat(4, 1fr)</div>
<div class="g pattern"><div>80px</div><div>1fr</div><div>80px</div><div>1fr</div></div>
<div class="label">repeat(2, 80px 1fr)</div>
<div class="g fill"><div>A</div><div>B</div></div>
<div class="label">repeat(auto-fill, minmax(100px, 1fr)) keeps empty tracks</div>
<div class="g fit"><div>A</div><div>B</div></div>
<div class="label">repeat(auto-fit, minmax(100px, 1fr)) stretches the items</div>
</div>
</div>
/* Base demo layout */
* { box-sizing: border-box; }

body {
margin: 24px;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
background: #f5f7fb;
color: #102a43;
}

.wrap {
max-width: 860px;
margin: 0 auto;
}

h1 {
font-size: 20px;
margin: 0 0 16px;
}

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}

.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.label {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
font-size: 12px;
color: #486581;
margin-top: 10px;
word-break: break-word;
}

.note {
font-size: 13px;
color: #486581;
margin-top: 14px;
}

/* repeat() examples */
.g {
display: grid;
gap: 8px;
margin-bottom: 6px;
}

.g div {
padding: 14px 6px;
border-radius: 6px;
background: #2563eb;
color: #fff;
font-size: 13px;
font-weight: 600;
text-align: center;
}

.four { grid-template-columns: repeat(4, 1fr); }
.pattern { grid-template-columns: repeat(2, 80px 1fr); }
.fill { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); }
.fit { grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); }

.pattern div:nth-child(odd) { background: #7c3aed; }
Open in editor

Browser Support

The following information will show you the current browser support for the CSS repeat() function. Hover over a browser icon to see the version that first introduced support for this CSS function.

This function is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.