Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS repeat() Function

CSS repeat() Function

CSS Functions CSS Functions

Example

Repeat a fragment of the track list:

#container

{

display:

grid

;

grid-template-columns:

repeat

(

2

,

50

px

1

fr

)

100

px

;

grid-gap:

5

px

;

box-sizing:

border-box

;

height:

200

px

;

width:

100

%

;

background-color:

#8cffa0

;

padding:

10

px

;

}

#container

div

{

background-color:

#8ca0ff

;

padding:

5

px

;

}

Try it »


Definition and usage

The repeat() function represents a repeated fragment of the track list, allowing a large number of columns or rows with a repeating pattern to be written in a more compact form.

This function can be used in CSS Grid properties grid-template-columns and grid-template-rows.

Supported version: CSS3


Browser support

The numbers in the table indicate the first browser version that supports the function.

function
var() 57.0 16.0 76.0 10.1 44.0

CSS Syntax



/*  values */

repeat(4, 1fr)

repeat(4, [col-start] 250px [col-end])

repeat(4, [col-start] 60% [col-end])

repeat(4, [col-start] 1fr [col-end])

repeat(4, [col-start] min-content [col-end])

repeat(4, [col-start] max-content [col-end])

repeat(4, [col-start] auto [col-end])

repeat(4, [col-start] minmax(100px, 1fr) [col-end])

repeat(4, [col-start] fit-content(200px) [col-end])

repeat(4, 10px [col-start] 30% [col-middle] auto [col-end])

repeat(4, [col-start] min-content [col-middle] max-content [col-end])



/*  values */

repeat(auto-fill, 250px)

repeat(auto-fit, 250px)

repeat(auto-fill, [col-start] 250px [col-end])

repeat(auto-fit, [col-start] 250px [col-end])

repeat(auto-fill, [col-start] minmax(100px, 1fr) [col-end])

repeat(auto-fill, 10px [col-start] 30% [col-middle] 400px [col-end])



/*  values */

repeat(4, 250px)

repeat(4, [col-start] 250px [col-end])

repeat(4, [col-start] 60% [col-end])

repeat(4, [col-start] minmax(100px, 1fr) [col-end])

repeat(4, [col-start] fit-content(200px) [col-end])

repeat(4, 10px [col-start] 30% [col-middle] 400px [col-end])

Value Description
length A positive integer length.
percentage A non-negative percentage relative to the inline size of the grid container for column tracks and the block size for row tracks. If the grid container's size depends on its track sizes, the percentage must be treated as auto. A user-agent can adjust the track's intrinsic size contribution to the grid container, increasing the track's final size to the minimum amount that respects the percentage.
flex A non-negative size with the fr unit specifies the track's flex factor. Any track sized with flex distributes the remaining space proportionally according to its flex factor.
max-content Represents the maximum of the largest content areas allocated to grid items occupying the grid track.
min-content Represents the minimum of the smallest content areas allocated to grid items occupying the grid track.
* auto* As a maximum, it is equivalent to max-content. As a minimum, it represents the largest of the minimum sizes of the grid items occupying the grid track (as specified by min-width/min-height).
auto-fill If the grid container has a definite size or maximum size on the relevant axis, the repeat count is the largest possible positive integer that does not cause the grid to overflow its container. If defined, each track is treated as its maximum track sizing function (grid-template-rows or grid-template-columns for each independent value). Otherwise, as the minimum track sizing function, grid gaps are added to the calculation. If the repeat count would be too large, the repeat value is 1. Otherwise, if the grid container has a definite minimum size on the relevant axis, the repeat count is the smallest possible positive integer that meets that minimum requirement. Otherwise, the specified track list is repeated only once.
auto-fit Behaves the same as auto-fill, except that after placing grid items, all empty repeated tracks are collapsed. An empty track is one with no grid items flowing into or spanning it. (If all tracks are empty, this may cause all tracks to be collapsed.) A collapsed track is treated as having a single fixed track sizing function of 0px, with the gutters on both sides collapsed. To find the number of auto-repeated tracks, the user-agent limits the track size to a user-agent-specified value (e.g. 1px) to avoid division by zero.

CSS Functions CSS Functions