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

ValueWhat it does
nonethe initial value: no explicit columns, the grid has one column
<length>a fixed width: 200px, 20rem; or a percentage of the container's width
fra share of the free space: 1fr 2fr splits it 1 : 2 — after the fixed-width columns
autosized 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-fitinside 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

▶ Open in HTML Editor

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

See also