Defining Grid Rows with grid-template-rows
You'll learn how to explicitly control the height of rows in your CSS Grid layout.
What Does grid-template-rows Do?
Just as grid-template-columns defines your vertical tracks (columns), grid-template-rows defines your horizontal tracks (rows). This property lets you specify exactly how tall each row should be in your grid container.
Think of it like setting up shelves in a bookcase: you decide whether the top shelf should be 6 inches tall for paperbacks, the middle shelf 12 inches for hardcovers, and the bottom shelf flexible to hold whatever's left.
How to Use It
The syntax mirrors grid-template-columns:
.grid-container {
display: grid;
grid-template-rows: 100px 200px 1fr;
}
This creates three explicit rows:
- First row: 100px tall (fixed)
- Second row: 200px tall (fixed)
- Third row: Takes up remaining space (flexible with
fr)
Fixed vs Flexible Rows
You can mix fixed and flexible values just like with columns:
.grid-container {
display: grid;
grid-template-rows: 80px 2fr 1fr;
}
Here, the first row is always 80px, while the remaining space is split 2:1 between the second and third rows.
Why This Matters
Without grid-template-rows, rows automatically size to fit their content. This property gives you precise control over vertical layout structure—essential for headers, content areas, and footers with predictable heights.
Key Takeaway: Use grid-template-rows to explicitly define row heights with fixed values (like px) or flexible units (like fr), giving you complete control over your grid's vertical structure.