CSS propertyGrid

grid-template

A shorthand that sets the grid’s rows, columns, and named areas in one declaration.

Initial value
none
Inherited
No
Animatable
yes, just like the longhand properties
Applies to
grid containers
JavaScript
element.style.gridTemplate = "auto 1fr / 1fr 1fr"
On this page

Example

#
CSS
.card {
  display: grid;
  grid-template:
    "image title" auto
    "image text" 1fr
    / 80px 1fr;
  gap: 4px 12px;
}
Preview

Definition and usage

#

The grid-template property combines grid-template-rows, grid-template-columns, and grid-template-areas. In its simplest form you give the rows and columns separated by a slash: grid-template: auto 1fr / 200px 1fr.

You can also combine area names with row heights: each string of names gets its height right next to it, and after the slash you give the column widths. This form can be compact, but harder to read.

The shorthand resets all three longhand properties, so any part you leave out falls back to its default.

Syntax

#
Syntax
grid-template: none | <rows> / <columns> | [ "<areas>" <height>? ]+ / <columns>;

Values

#

Values in practice

#

none

No explicit grid.
.demo { grid-template: none; }

1fr 2fr / 1fr 1fr 1fr

Two rows (the second taller) and three equal columns.
.demo { grid-template: 1fr 2fr / 1fr 1fr 1fr; }

40px 1fr / 80px 1fr

A short row and a narrow column, with the rest of the space for the others.
.demo { grid-template: 40px 1fr / 80px 1fr; }

Browser support

#

Widely available

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

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • For complex layouts, write grid-template-areas and grid-template-columns separately. It’s easy to break the shorthand with a single character.
  • The slash in the shorthand always separates rows (before) from columns (after).

Related entries

#

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