CSS grid-template Property

The CSS grid-template property is a shorthand for defining the rows, columns, and named areas of a CSS Grid layout.

It combines grid-template-rows, grid-template-columns, and grid-template-areas into one declaration. You can use it to establish track sizes, create named layout regions, and organize page components within an explicit grid.

grid-template Syntax

The grid-template property accepts row and column track definitions separated by a forward slash, named grid area definitions, or the keyword none.

Basic Syntax

selector {
  grid-template: none;
}

Define Rows and Columns

.container {
  grid-template: 100px 200px / 1fr 2fr;
}

The values before the slash define two rows. The values after the slash define two columns.

Equal-Sized Tracks

.container {
  grid-template: repeat(3, 100px) / repeat(3, 1fr);
}

Named Grid Areas

.container {
  grid-template:
    "header header" 80px
    "sidebar main" 1fr
    "footer footer" 60px
    / 220px 1fr;
}

Named Grid Lines

.container {
  grid-template:
    [top] 80px [middle] 1fr [bottom]
    / [left] 200px [center] 1fr [right];
}

Automatic Row Sizing

.container {
  grid-template: auto 1fr auto / 200px 1fr;
}

Reset the Explicit Grid

.container {
  grid-template: none;
}

CSS-Wide Keywords

.container {
  grid-template: initial;
}
.container {
  grid-template: inherit;
}
.container {
  grid-template: unset;
}
.container {
  grid-template: revert;
}
.container {
  grid-template: revert-layer;
}

The property must be used with a grid formatting context, normally established by display: grid or display: inline-grid.

Property Values

The grid-template shorthand supports track definitions, named grid areas, and CSS-wide keywords.

Value Description
none Sets all three longhand properties to none.
<grid-template-rows> / <grid-template-columns> Defines explicit row and column tracks. The named area property is reset to none.
<string> Defines named grid areas using quoted strings, one for each row.
<track-size> Specifies a track size using lengths, percentages, flexible units, or supported sizing functions.
<line-names> Defines named grid lines using square brackets.
auto Allows a track to size automatically according to its content and available space.
repeat() Repeats track definitions in the row/column syntax where permitted.
minmax() Defines minimum and maximum track sizing limits.
initial Resets all three longhand properties to their initial values.
inherit Inherits the computed values of all three longhand properties from the parent.
unset Resets all three non-inherited longhand properties to their initial values.
revert Reverts the longhand properties according to cascade origin rules.
revert-layer Reverts the longhand properties according to cascade layer rules.

Property Characteristics

Characteristic Value
Initial value none for all three longhands
Inherited No
Applies to Grid containers
Computed value As specified for each longhand, with track sizing values computed according to their individual rules
Animation type Depends on the longhand properties
Longhand properties grid-template-rows, grid-template-columns, grid-template-areas

The grid-template shorthand defines the explicit grid. It does not directly set grid-auto-rows, grid-auto-columns, or grid-auto-flow.

How It Works

The grid-template property establishes the explicit row and column tracks of a grid container and can optionally assign names to rectangular regions.

Create a Grid Container

.container {
  display: grid;
  grid-template: 100px 100px / 1fr 1fr;
  gap: 12px;
}

This creates two rows, each 100 pixels tall, and two equal-width columns.

Place an Item

.item {
  grid-row: 1;
  grid-column: 2;
}

The item occupies the first row and second column.

Equivalent Longhand Declarations

.container {
  display: grid;
  grid-template-rows: 100px 100px;
  grid-template-columns: 1fr 1fr;
  grid-template-areas: none;
  gap: 12px;
}

These declarations produce the same explicit grid as the earlier shorthand example.

Important Consideration

The property defines the grid structure, but it does not automatically assign specific items to named areas. Use grid-area or grid line placement properties to position individual items.

Defining Rows and Columns

In the row/column form of grid-template, a forward slash separates row track definitions from column track definitions.

Three Rows and Two Columns

.container {
  display: grid;
  grid-template: 80px 1fr 60px / 200px 1fr;
}

The container has three rows and two columns. The first column is 200 pixels wide, while the second receives a flexible share of the available space.

Fixed-Size Tracks

.container {
  display: grid;
  grid-template: 100px 100px / 150px 150px 150px;
}

Flexible Columns

.container {
  display: grid;
  grid-template: auto auto / 1fr 2fr 1fr;
}

The middle column receives twice the flexible space of either outer column, after applicable sizing constraints are resolved.

Repeated Tracks

.container {
  display: grid;
  grid-template: repeat(3, 80px) / repeat(4, 1fr);
}

The repeat() function avoids writing identical track definitions multiple times.

Named Grid Areas

Named grid areas let you define a visual layout using quoted strings. Each string represents one row, and each space-separated name represents a column cell.

Define a Page Layout

.page-layout {
  display: grid;
  grid-template:
    "header header" 80px
    "sidebar main" 1fr
    "footer footer" 60px
    / 220px 1fr;
  gap: 12px;
}

Assign Grid Items

.header {
  grid-area: header;
}
.sidebar {
  grid-area: sidebar;
}
.main {
  grid-area: main;
}
.footer {
  grid-area: footer;
}

The header and footer span both columns. The sidebar and main content share the middle row.

Empty Grid Cells

.container {
  display: grid;
  grid-template:
    "header header" 70px
    "sidebar ." 150px
    / 180px 1fr;
}

A period represents an unnamed grid cell. It does not assign a named area to that position.

Rectangular Area Requirement

Each named area must form a single rectangle. An area cannot be split into disconnected regions or arranged in an L-shape.

Valid Area Definition

.container {
  grid-template:
    "main main" 100px
    "main main" 100px
    / 1fr 1fr;
}

Invalid Area Definition

.container {
  grid-template:
    "main main" 100px
    "main sidebar" 100px
    / 1fr 1fr;
}

The second definition is invalid because main occupies an L-shaped region instead of a rectangle.

Important Consideration

Every row string must define the same number of cells. In the named-area syntax, use individual track sizes and optional line names rather than the repeat() track-list function.

Track Sizing

The grid-template property supports the track sizing values accepted by its row and column longhand properties.

Fixed Lengths

.container {
  grid-template: 100px 150px / 200px 300px;
}

Flexible Units

.container {
  grid-template: 1fr 2fr / 1fr 1fr;
}

The fr unit represents a fraction of the available flexible space in the grid container.

Minimum and Maximum Sizes

.container {
  grid-template:
    minmax(80px, auto) minmax(120px, 1fr)
    / minmax(180px, 1fr) 2fr;
}

Content-Based Sizing

.container {
  grid-template: min-content max-content / auto 1fr;
}

Fit Content

.container {
  grid-template: auto auto / fit-content(250px) 1fr;
}

Responsive Column Tracks

.container {
  grid-template:
    auto
    / repeat(auto-fit, minmax(180px, 1fr));
}

The auto-fit keyword adjusts the number of repeated columns according to the available inline space.

Important Consideration

Track sizes are subject to the grid sizing algorithm. Content, intrinsic minimum sizes, and available container space can affect the final dimensions.

Named Grid Lines

Named grid lines let you reference track boundaries with meaningful identifiers.

Define Named Lines

.container {
  display: grid;
  grid-template:
    [header-start] 80px
    [header-end content-start] 1fr
    [content-end]
    / [sidebar-start] 200px
      [sidebar-end main-start] 1fr
      [main-end];
}

Place an Item Using Named Lines

.main {
  grid-row: content-start / content-end;
  grid-column: main-start / main-end;
}

Names Generated by Grid Areas

.container {
  display: grid;
  grid-template:
    "header" 80px
    "content" 1fr
    / 1fr;
}
.content {
  grid-row: content-start / content-end;
}

Named grid areas automatically establish corresponding -start and -end line names.

Important Consideration

Grid line names are case-sensitive. Use consistent names across track definitions and item placement rules.

Shorthand Behavior

The grid-template shorthand sets three longhand properties together.

Longhand Properties

.container {
  grid-template-rows: 80px 1fr 60px;
  grid-template-columns: 200px 1fr;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}

Equivalent Shorthand

.container {
  grid-template:
    "header header" 80px
    "sidebar main" 1fr
    "footer footer" 60px
    / 200px 1fr;
}

Resetting Named Areas

.container {
  grid-template-areas:
    "header header"
    "sidebar main";
  grid-template: 100px 1fr / 200px 1fr;
}

The final shorthand declaration resets grid-template-areas to none because the row/column form does not define named areas.

Resetting All Three Longhands

.container {
  grid-template: none;
}

This removes explicit track definitions and named areas established through these properties.

Important Consideration

Later shorthand declarations can overwrite earlier longhand values. When changing only one aspect of a grid, use the corresponding longhand property.

Implicit Grid Tracks

The grid-template property defines the explicit grid. CSS Grid can create additional implicit tracks when items require space outside that structure.

Define an Explicit Grid

.container {
  display: grid;
  grid-template: repeat(2, 80px) / repeat(2, 1fr);
}

Place an Item Outside the Explicit Grid

.item {
  grid-row: 4;
  grid-column: 3;
}

The item requires additional implicit rows and a column beyond the two explicitly defined tracks in each direction.

Size Implicit Tracks

.container {
  display: grid;
  grid-template: repeat(2, 80px) / repeat(2, 1fr);
  grid-auto-rows: 100px;
  grid-auto-columns: 150px;
}

Control Automatic Placement

.container {
  display: grid;
  grid-template: repeat(2, 80px) / repeat(2, 1fr);
  grid-auto-flow: row;
}

The grid-auto-flow property controls how automatically positioned items fill the grid.

Important Consideration

Unlike the broader grid shorthand, grid-template does not reset the implicit track sizing and auto-placement properties.

Responsive Layouts

The grid-template property is useful for rearranging page regions at different viewport widths.

Mobile Layout

.page-layout {
  display: grid;
  grid-template:
    "header" auto
    "main" 1fr
    "sidebar" auto
    "footer" auto
    / minmax(0, 1fr);
  gap: 16px;
}

Desktop Layout

@media (min-width: 768px) {
  .page-layout {
    grid-template:
      "header header" auto
      "sidebar main" 1fr
      "footer footer" auto
      / 220px minmax(0, 1fr);
  }
}

Assign Named Areas

.header {
  grid-area: header;
}
.sidebar {
  grid-area: sidebar;
}
.main {
  grid-area: main;
}
.footer {
  grid-area: footer;
}

On smaller screens, the areas form a single column. On larger screens, the sidebar and main content appear beside each other.

Important Consideration

Changing visual placement does not change the source order or keyboard navigation order. Keep the document structure logical and accessible.

grid-template vs. grid

The grid-template and grid properties are both shorthands, but they affect different sets of properties.

Property What It Controls
grid-template Explicit rows, columns, and named areas
grid Explicit grid properties plus implicit track sizing and automatic placement properties

Using grid-template

.container {
  display: grid;
  grid-template: 100px 100px / 1fr 1fr;
}

Using grid

.container {
  display: grid;
  grid: 100px 100px / 1fr 1fr;
}

Both examples establish the same explicit tracks. However, the grid shorthand also resets the implicit grid properties that are not explicitly specified.

Important Consideration

Choose grid-template when defining the explicit grid without changing existing implicit track sizing or auto-placement settings.

Inheritance

The grid-template property is not inherited.

Parent Grid Container

.parent {
  display: grid;
  grid-template: 100px 100px / 1fr 1fr;
}

Nested Grid Container

.nested {
  display: grid;
  grid-template: none;
}

A nested grid container does not automatically receive the explicit track definitions of its parent.

Explicit Inheritance

.nested {
  grid-template: inherit;
}

The inherit keyword copies the computed values of the three longhand properties from the parent. The nested grid still establishes its own grid formatting context.

Reset to Initial Values

.container {
  grid-template: initial;
}

Reset with unset

.container {
  grid-template: unset;
}

Because the longhand properties are not inherited, unset resets them to their initial values.

grid-template Example

This example creates a complete page layout with a header, sidebar, main content area, and footer. The grid-template shorthand defines both the track sizes and the named areas.

HTML Example

<div class="grid-template-demo">
  <div class="demo-header">Header</div>
  <div class="demo-sidebar">Sidebar</div>
  <div class="demo-main">Main Content</div>
  <div class="demo-footer">Footer</div>
</div>

CSS Example

.grid-template-demo {
  display: grid;
  grid-template:
    "header header" 60px
    "sidebar main" 120px
    "footer footer" 50px
    / minmax(100px, 1fr) minmax(0, 2fr);
  gap: 10px;
}
.grid-template-demo > div {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
  background-color: #596579;
  color: #ffffff;
  text-align: center;
  font-weight: bold;
  border-radius: 4px;
}
.grid-template-demo .demo-header {
  grid-area: header;
  background-color: #2456a6;
}
.grid-template-demo .demo-sidebar {
  grid-area: sidebar;
}
.grid-template-demo .demo-main {
  grid-area: main;
}
.grid-template-demo .demo-footer {
  grid-area: footer;
  background-color: #2456a6;
}
Browser Result
Header
Sidebar
Main Content

The header and footer span both columns. The sidebar occupies the first column, while the main content occupies the second column.

The layout contains exactly three rows, with no unused grid tracks. The gap property adds spacing between the areas without creating additional rows.

Equivalent Longhand Properties

.grid-template-demo {
  grid-template-rows: 60px 120px 50px;
  grid-template-columns: minmax(100px, 1fr) minmax(0, 2fr);
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}

These longhand declarations define the same grid structure as the shorthand used in the example.

Common Mistakes

Forgetting to Enable CSS Grid

.container {
  grid-template: 100px 100px / 1fr 1fr;
}

The declaration does not create a grid formatting context by itself. Add display: grid or display: inline-grid.

Using Different Numbers of Area Cells

.container {
  grid-template:
    "header header" 80px
    "sidebar main extra" 1fr
    / 200px 1fr;
}

This definition is invalid because the area strings contain different numbers of cells.

Creating Nonrectangular Named Areas

A named area must form one rectangle. Disconnected cells or L-shaped areas are not valid.

Forgetting the Slash

In the row/column syntax, the slash separates row definitions from column definitions. Omitting it can make the declaration invalid or change how the values are interpreted.

Using repeat() Inside Named Area Syntax

The named-area syntax requires individual row sizes and an explicit column track list. Do not use repeat() in that syntax.

Assuming Named Areas Place Items Automatically

Defining an area does not automatically assign an item to it. Use grid-area to place the item in the named region.

Overlooking Shorthand Resets

A later grid-template declaration can reset earlier row, column, or named area definitions.

Confusing Explicit and Implicit Tracks

The grid-template property defines explicit tracks. Additional tracks created by placement are controlled by the implicit grid sizing properties.

Important Considerations

Keep the following points in mind when using grid-template:

  • The property combines grid-template-rows, grid-template-columns, and grid-template-areas.
  • It defines the explicit grid rather than the implicit grid.
  • The row/column syntax separates track definitions with a forward slash.
  • Named grid areas must form rectangular regions.
  • Each named area string must contain the same number of cells.
  • Use a period to represent an unnamed cell in a named area string.
  • The fr unit distributes available flexible space among tracks.
  • The minmax() function can help prevent tracks from becoming too small.
  • Use grid-area to assign items to named areas.
  • The shorthand resets all three associated longhand properties.
  • The property does not reset grid-auto-rows, grid-auto-columns, or grid-auto-flow.
  • Responsive layouts can redefine grid templates through media queries.
  • Visual placement does not change document order or keyboard navigation order.

Browser Support

The CSS grid-template property is widely available in modern browsers, including Chrome, Edge, Firefox, Safari, and Opera.

For current compatibility information, visit Can I Use? and search for grid-template.

Some advanced grid features, including subgrid and newer track sizing capabilities, have separate browser compatibility requirements.

Specifications

The grid-template property is defined in the CSS Grid Layout specification.

CSS Grid Layout Module Level 1: grid-template

The specification defines explicit grid track sizing, named grid areas, grid line names, and shorthand behavior.

CSS Grid Layout Module Level 2

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: grid-template