Unlike columns, which frequently need to be defined explicitly for a specific layout, rows are often left to be created implicitly, automatically, as content naturally requires more of them, sized according to grid-auto-rows, rather than needing a fixed, predetermined number of rows defined upfront via grid-template-rows. Explicitly defining rows with grid-template-rows is most useful for a layout with a genuinely fixed, known number of distinct rows, like a header, main content area, and footer each needing their own specific height within an overall page grid.
1Understanding Grid-template-rows
Unlike columns, which frequently need to be defined explicitly for a specific layout, rows are often left to be created implicitly, automatically, as content naturally requires more of them, sized according to grid-auto-rows, rather than needing a fixed, predetermined number of rows defined upfront via grid-template-rows. Explicitly defining rows with grid-template-rows is most useful for a layout with a genuinely fixed, known number of distinct rows, like a header, main content area, and footer each needing their own specific height within an overall page grid.
For a grid where the number of rows naturally grows with the amount of content, like a card gallery, let rows be created implicitly rather than trying to predefine a fixed number with grid-template-rows — explicit row definitions are better suited to a layout with a known, fixed number of distinct row regions.
.page {
display: grid;
grid-template-rows: 80px 1fr 60px;
height: 100vh;
}2Practical Example
Here is a real-world application of Grid-template-rows showing how it is used in production CSS code.
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 200px;
}3Best Practices
Follow these guidelines when working with Grid-template-rows:
1. Use grid-template-rows explicitly for layouts with a genuinely fixed, known number of row regions, like a header/main/footer page structure
2. Let rows be created implicitly, sized via grid-auto-rows, for content that naturally grows the number of rows needed, like a card gallery with an unpredictable number of items
3. Use fr units in grid-template-rows the same way as in grid-template-columns, to distribute available vertical space flexibly among rows
Tip: For a grid where the number of rows naturally grows with the amount of content, like a card gallery, let rows be created implicitly rather than trying to predefine a fixed number with grid-template-rows — explicit row definitions are better suited to a layout with a known, fixed number of distinct row regions.
.page {
display: grid;
grid-template-rows: 80px 1fr 60px;
height: 100vh;
}