Repeat() - Function CSS
Summary of characteristics of the repeat()
function
auto-fill
| auto-fit
| auto-areas
Syntactic schema of repeat()
.
On the diagram, the terms in bold are the predefined words of CSS, the other terms are described below:
integer
is an integer, positive, unitless number.length
is a positive numeric value, followed by one of the CSS dimension units.- Multiple
length
values can be specified, separated by spaces.
Description of the repeat()
function.
The repeat()
function is used to set the widths of columns in a grid or the heights of rows.
repeat()
avoids repeating identical values multiple times.
Example :
grid-template-columns: repeat(10, 10%);
/* Is equivalent to : */
grid-template-columns: 10% 10% 10% 10% 10% 10% 10% 10% 10% 10% ;
For more information about sizing rows or columns in a grid, see the grid-template-rows
and grid-template-columns
,
as well as the description of the fr
unit.
The repeat() fonction can be used with:
grid-template-columns
: Defines the number and width of columns in a grid.grid-template-rows
: Defines the number and height of lines in a grid.
Syntax of the repeat()
function.
- grid-template-columns: repeat(5, 200px); n w
n
is a positive integer, without units, that indicates the number of repetitions.w
is the width of a column or the height of a line. Refer to thegrid-template-columns
andgrid-template-rows
properties for different syntaxes accepted for this value.Note that the
w
value can be a series of several values, but therepeat()
function will not consider it to be of several parameters: everything after the decimal point is considered the value to be repeated.
Thus the syntaxrepeat(5, 5%, 15%)
is equivalent to
5% 15% 5% 15% 5% 15% 5% 15% 15% 15%.
The grid below has been defined with 5 columns with a width of 50 pixels each.
12345repeat(5, 50px);
- grid-template-columns: repeat(auto-fill, 100px); n w
When
auto-fill
is specified in place of the first parameter, therepeat()
function calculates the number of columns or rows given the dimensions of the container. The following conditions must be met forauto-fill
to be allowed:Careful! For
repeat()
to be usable withauto-fill
, the column dimensions must be expressed in a absolute unit (pixels for example).- If
auto-fill
is used to define columns, the width of the container must be defined, or at least its maximum width (See themax-width
property or theminmax()
function.) - If
auto-fill
is used to define rows, the maximum height or height of the container must be set (see themax-height
property or theminmax()
function). - The second parameter
w
should not be expressed as percentages.
The grid below has widths set with
auto-fill
: the number of columns depends on the width of the container.12345repeat(auto-fill, 75px);
- If
- grid-template-columns: repeat(auto-fit, 100px); n w
With
auto-fit
specified as the first parameter, therepeat()
function behaves as withauto-fill
but the columns (or lines) that would end up empty after positioning the elements will have their width reduced to 0.In the grid below, the second item (in blue) is positioned in column 3, leaving an empty cell before it. By increasing the width of the container, you can make the second column of the grid completely empty. In this case, the operation of
auto-fit
makes this column disappear (width reduced to 0).12345repeat(auto-fit, 60px);
But it's even more relevant to use
auto-fit
by setting the minimum and maximum values for widths (with theminmax()
for example). The result is something that adapts very well to the size of the screen, and without loosing space.12345repeat(auto-fit, minmax(60px, 1fr));
- grid-template-columns: repeat(auto-areas); ⚠
The
auto-areas
value is reserved for grids of the masonry type.12345repeat(auto-areas);
Simulator.
Browser compatibility.
Current browsers all correctly handle sizing rows or columns in a grid with the repeat()
function,
except for the new possibilities offered by masonry-type grilles.
repeat()
function to define the width of columns, using the grid-template-columns
property.repeat()
function to define the height of rows, using the grid-template-rows
property.repeat()
functionwith
grid-template-columns
repeat()
functionwith
grid-template-rows
Browsers on computers :
Mobile browsers :
Outdated or marginal browsers :

Internet Explorer

QQ Browser

Safari sur IOS

Firefox pour Androïd

Opéra Mobile

Baidu Browser

KaiOS Browser

UC Browser pour Androïd

Samsung Internet

Chrome

Edge

Androïd Brower

Chrome pour Androïd

Opéra

Safari

Firefox

Opéra mini
repeat()
function history.
-
CSS Grid Layout Module Level 1
Introducing therepeat()
function to make it easier to enumerate the columns of a grid.April 07, 2011Working Draft.September 29, 2016Candidate Recommendation. -
CSS Grid Layout Module Level 2
No change to the syntax of therepeat()
function.February 06, 2018Working Draft.August 18, 2020Candidate Recommendation. -
CSS Grid Layout Module Level 3
Adaptation of the syntax of therepeat()
function to the new "Masonry" type grids.September 19, 2024Working Draft.
Other definitions related to grids.
Properties:














Functions:
