Course contentsShow
← Web Development
Lesson 317 of 2,9826. CSS GridFree lesson

Defining Rows with grid-template-rows

Explicitly setting row track sizes and understanding how rows auto-generate when not defined.

Defining Rows with grid-template-rows

You'll learn how to control the height of your grid rows and what happens when you don't define them.

What Does grid-template-rows Do?

Just as grid-template-columns defines the width of vertical tracks, grid-template-rows defines the height of horizontal tracks. It gives you explicit control over how tall each row should be in your grid.

The Syntax

The property works identically to grid-template-columns, but controls rows instead of columns:

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

This creates three explicit rows: the first is 100px tall, the second 200px, and the third 150px.

What If You Don't Define Rows?

Here's the interesting part: if you don't use grid-template-rows, the grid automatically generates implicit rows as needed to hold your content. These implicit rows size themselves based on their content—they'll be just tall enough to fit whatever's inside.

Think of it like a bookshelf where you specify the width of each shelf section with grid-template-columns, but the height of each shelf automatically adjusts based on how tall the items are—unless you explicitly say "this shelf is 200px tall."

Common Pattern

Many grids only define columns explicitly and let rows auto-generate:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  /* No grid-template-rows — rows auto-size to content */
}

Key Takeaway: grid-template-rows explicitly sets row heights using the same unit syntax as columns; when omitted, rows auto-generate and size to their content.