The grid-template-columns property
grid-template-columns is set on a grid container and describes its columns: how many there are and how wide. grid-template-rows does the same for rows.
Syntax
grid-template-columns: none | <column size>…;
grid-template-columns: 200px 1fr 2fr;
grid-template-columns: repeat(3, 1fr);
grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
grid-template-columns: auto 1fr; - Initial value
none- Inherited
- no
- Applies to
- grid containers (elements with display: grid or inline-grid)
Values
| Value | What it does |
|---|---|
none | the initial value: no explicit columns, the grid has one column |
<length> | a fixed width: 200px, 20rem; or a percentage of the container's width |
fr | a share of the free space: 1fr 2fr splits it 1 : 2 — after the fixed-width columns |
auto | sized to content, but the column may stretch into free space |
repeat() | repetition: repeat(3, 1fr) is the same as 1fr 1fr 1fr |
minmax() | width limits: minmax(140px, 1fr) — never narrower than 140px, never wider than a share |
auto-fill / auto-fit | inside repeat(): as many columns as fit; auto-fit also stretches them when there are fewer items |
[name] | a grid line name: [sidebar] 200px [main] 1fr — then grid-column: main |
Example
.grid {
display: grid;
gap: 8px;
margin-bottom: 12px;
}
.grid div {
padding: 8px;
background: #e0f2fe;
border: 1px solid #0284c7;
}
.fixed { grid-template-columns: 120px 1fr 2fr; }
.repeat { grid-template-columns: repeat(4, 1fr); }
.auto { grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); } <p>120px 1fr 2fr</p>
<div class="grid fixed">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
<p>repeat(4, 1fr)</p>
<div class="grid repeat">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<p>repeat(auto-fit, minmax(120px, 1fr)) — narrow the window</p>
<div class="grid auto">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div> Result
Good to know
- repeat(auto-fit, minmax(140px, 1fr)) is a responsive grid without media queries: there are fewer columns on a narrow screen.
- auto-fill and auto-fit only differ when there are fewer items than columns that fit: auto-fill keeps the empty columns, auto-fit stretches the existing items across the full width.
- fr shares the space left after fixed columns and gap, so 1fr is not 100% / n.
- Without display: grid on the same element the property has no effect.
Sources and specification
- MDN: the grid-template-columns property — MDN Web Docs
- CSS Grid Layout Module Level 1 (W3C) — W3C
See also
CSS Grid
CSS Grid lays out rows and columns: display: grid, grid-template-columns, fr, repeat() and minmax(), and a responsive grid without media queries.
The gap property
The CSS gap property sets the space between items of a flex or grid container without extra margins: one or two values, row-gap and column-gap.
Media queries (@media)
CSS media queries (@media) apply styles depending on screen width, orientation and user settings: min-width, max-width, mobile-first and common mistakes.
The display property
The CSS display property sets how an element takes part in layout: as a block, inline with text, as a flex or grid container, or not at all. Values, examples.
Responsive CSS Grid cards
A card grid that adapts its column count to screen width, with no media queries.