CSS propertyGrid

grid-template-columns

Defines the grid’s columns: their number and widths.

Initial value
none
Inherited
No
Animatable
yes, when only the values change while the number of columns stays the same
Applies to
grid containers
JavaScript
element.style.gridTemplateColumns = "1fr 2fr"
On this page

Example

#
CSS
.courses {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
}
Preview

Definition and usage

#

The grid-template-columns property is the heart of a CSS Grid layout. Each value in the list is one column, so 200px 1fr 1fr creates three columns: the first with a fixed width, and two that split the rest of the space evenly.

The fr unit represents a fraction of the free space, and the repeat() function shortens repeating patterns. repeat(auto-fill, minmax(160px, 1fr)) creates as many columns at least 160px wide as will fit, without any @media queries.

Grid items are placed automatically into the next cells, row by row. Rows you don’t define are created on their own and get their height from grid-auto-rows.

Syntax

#
Syntax
grid-template-columns: none | <track-size>+ | repeat(…) | subgrid;

Values

#

Values in practice

#

100px 100px

Two columns with a fixed width.
.demo { grid-template-columns: 100px 100px; }

1fr 2fr

The second column is twice as wide as the first.
.demo { grid-template-columns: 1fr 2fr; }

repeat(3, 1fr)

Three equal columns.
.demo { grid-template-columns: repeat(3, 1fr); }

80px 1fr auto

A fixed column, a flexible one, and one sized to its content.
.demo { grid-template-columns: 80px 1fr auto; }

repeat(auto-fill, minmax(70px, 1fr))

As many columns at least 70px wide as will fit. The grid adapts to the available width on its own.
.demo { grid-template-columns: repeat(auto-fill, minmax(70px, 1fr)); }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • minmax(160px, 1fr) with auto-fill or auto-fit is the simplest responsive card grid, with no screen-width queries at all.
  • fr divides up free space, so 1fr 1fr doesn’t always give equal columns when content is large. minmax(0, 1fr) helps with that.

Related entries

#

Spotted a mistake or missing an example? Write to us.