CSS grid-template-rows Property
The CSS grid-template-rows property defines the number and sizes of rows in the explicit grid of a CSS Grid container.
It supports fixed heights, percentages, flexible fr units, content-based sizing, and functions such as repeat() and minmax(). You can also name row grid lines, create responsive layouts, and use subgrid to share row tracks with a parent grid.
grid-template-rows Syntax
The grid-template-rows property accepts a list of row track sizes, optional named grid lines, none, or subgrid.
Basic Syntax
selector {
grid-template-rows: none;
}
Fixed Row Heights
.container {
grid-template-rows: 80px 150px 80px;
}
Flexible Rows
.container {
grid-template-rows: 1fr 2fr 1fr;
}
Percentage-Based Rows
.container {
grid-template-rows: 30% 70%;
}
Repeated Rows
.container {
grid-template-rows: repeat(3, 100px);
}
Minimum and Maximum Heights
.container {
grid-template-rows: minmax(80px, auto) 1fr;
}
Automatically Repeated Rows
.container {
height: 400px;
grid-template-rows: repeat(auto-fit, minmax(100px, 1fr));
}
Named Grid Lines
.container {
grid-template-rows:
[header-start] 80px
[header-end content-start] 1fr
[content-end footer-start] 60px
[footer-end];
}
Subgrid
.nested-grid {
grid-template-rows: subgrid;
}
CSS-Wide Keywords
.container {
grid-template-rows: initial;
}
.container {
grid-template-rows: inherit;
}
.container {
grid-template-rows: unset;
}
.container {
grid-template-rows: revert;
}
.container {
grid-template-rows: revert-layer;
}
Each track size defines one row unless a repetition function expands into multiple tracks. Row definitions are separated by whitespace rather than commas.
Property Values
The grid-template-rows property supports the following values and sizing functions.
| Value | Description |
|---|---|
none |
Defines no explicit row tracks through this property. |
<length> |
Sets a row height using a length such as px, rem, or em. |
<percentage> |
Sizes a row relative to the grid container's block size when that size is definite. |
<flex> |
Uses the fr unit to allocate a share of available flexible space. |
auto |
Sizes the row according to its contents and available space. |
min-content |
Uses the smallest intrinsic block size needed by the row's contents. |
max-content |
Uses the row's maximum intrinsic block size. |
minmax(min, max) |
Defines minimum and maximum track sizing limits. |
fit-content(limit) |
Limits track growth while respecting intrinsic sizing requirements. |
repeat() |
Repeats a row track pattern a specified number of times or automatically. |
auto-fill |
Creates as many repeated rows as fit while retaining empty tracks. |
auto-fit |
Creates as many repeated rows as fit, then collapses empty repeated tracks. |
[name] |
Assigns one or more names to a row grid line. |
subgrid |
Uses the parent grid's corresponding row tracks for a nested grid item. |
initial |
Resets the property to none. |
inherit |
Inherits the computed value from the parent. |
unset |
Resets this non-inherited property to none. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | Grid containers |
| Computed value | As specified, with relative lengths converted to absolute lengths |
| Animation type | Track-list interpolation when compatible; otherwise discrete |
| Percentages | Refer to the grid container's block size |
| Related shorthands | grid-template, grid |
The actual row heights depend on the grid track sizing algorithm, content requirements, and the available block size of the grid container.
How It Works
The grid-template-rows property establishes explicit row tracks in a grid container. Grid lines separate those tracks and provide boundaries for positioning items.
Create a Grid Container
.container {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: 80px 150px 60px;
gap: 12px;
}
This creates three explicit rows with heights of 80, 150, and 60 pixels.
Position an Item
.item {
grid-row: 2;
grid-column: 1 / 3;
}
The item occupies the second row and spans both columns.
Span Multiple Rows
.item {
grid-row: 1 / 3;
}
The item begins at row line 1 and ends at line 3, occupying the first two rows.
Grid Lines and Row Tracks
Three explicitly defined rows create four row boundary lines. The first row lies between lines 1 and 2, the second between lines 2 and 3, and the third between lines 3 and 4.
Important Consideration
The property controls row track sizes, not the placement of individual items. Use grid-row, grid-row-start, or grid-row-end for row placement.
Fixed Row Sizes
Fixed lengths give rows predictable heights. They are useful for headers, toolbars, and other regions that require specific dimensions.
Pixel-Based Rows
.container {
grid-template-rows: 60px 180px 50px;
}
Relative Length Units
.container {
grid-template-rows: 4rem 12rem 3rem;
}
Mixed Fixed and Automatic Rows
.container {
grid-template-rows: 70px auto 50px;
}
The first and last rows use fixed heights. The middle row sizes according to its content and available space.
Percentage-Based Rows
.container {
height: 400px;
display: grid;
grid-template-rows: 25% 75%;
}
With a definite container height of 400 pixels, the rows are 100 and 300 pixels tall before accounting for any additional spacing.
Important Consideration
Fixed-height rows do not automatically expand to accommodate overflowing content. Use auto or minmax() when the content height may vary.
Percentages require a definite block size for straightforward resolution. When the grid container's block size depends on its contents, percentage track sizing may behave like auto during intrinsic sizing.
Fractional Units
The fr unit represents a fraction of the available flexible space along the grid's row axis.
Two Equal Rows
.container {
height: 300px;
display: grid;
grid-template-rows: 1fr 1fr;
}
Unequal Row Proportions
.container {
height: 400px;
display: grid;
grid-template-rows: 1fr 2fr 1fr;
}
When sizing constraints allow, the middle row receives two shares of flexible space while each outer row receives one.
Fixed Header and Flexible Content
.container {
height: 400px;
display: grid;
grid-template-rows: 70px minmax(0, 1fr);
}
Allow a Flexible Row to Shrink
.container {
height: 400px;
display: grid;
grid-template-rows: 70px minmax(0, 1fr) 50px;
}
The minmax(0, 1fr) track can shrink below its content's automatic minimum size. Overflowing content may require additional handling.
Important Consideration
A definite container height makes the distribution of flexible row space predictable. In an automatically sized container, fractional rows are influenced by intrinsic content sizing rather than simply dividing a fixed height.
The repeat() Function
The repeat() function creates repeated row track definitions without writing the same values multiple times.
Repeat Three Rows
.container {
grid-template-rows: repeat(3, 100px);
}
Equivalent Expanded Declaration
.container {
grid-template-rows: 100px 100px 100px;
}
Repeat Flexible Rows
.container {
height: 400px;
grid-template-rows: repeat(4, minmax(0, 1fr));
}
Repeat a Row Pattern
.container {
grid-template-rows: repeat(2, 60px 100px);
}
This produces four rows with heights of 60, 100, 60, and 100 pixels.
Combine Individual and Repeated Tracks
.container {
grid-template-rows: 70px repeat(3, 100px) 50px;
}
The grid contains five explicit rows: a 70-pixel row, three 100-pixel rows, and a 50-pixel row.
Important Consideration
The repetition count must be a positive integer or an allowed automatic repetition keyword. The repeated track definitions must follow the syntax requirements for the chosen repetition type.
The minmax() Function
The minmax() function sets minimum and maximum limits for a row track, allowing its height to adapt within those constraints.
Minimum Height with Automatic Growth
.container {
grid-template-rows: minmax(100px, auto) 60px;
}
The first row is at least 100 pixels tall and can grow when its content requires more space.
Fixed Minimum and Maximum
.container {
grid-template-rows: minmax(100px, 250px) 1fr;
}
Flexible Maximum
.container {
height: 400px;
grid-template-rows: minmax(80px, 1fr) 2fr;
}
Remove the Automatic Minimum
.container {
height: 400px;
grid-template-rows: 70px minmax(0, 1fr);
}
Important Consideration
The minimum argument cannot be a flexible fr value. A flexible maximum is allowed. If the maximum is smaller than the minimum, the maximum is effectively ignored.
auto-fill and auto-fit
The auto-fill and auto-fit keywords allow repeat() to determine how many rows fit within the available block size.
Using auto-fill
.container {
height: 400px;
display: grid;
grid-template-rows: repeat(auto-fill, 100px);
gap: 10px;
}
The grid creates as many 100-pixel rows as fit within the available height, accounting for gaps. Empty repeated rows remain.
Using auto-fit
.container {
height: 400px;
display: grid;
grid-template-rows: repeat(auto-fit, minmax(100px, 1fr));
gap: 10px;
}
The grid determines how many repeated rows fit, then collapses empty repeated tracks. Occupied flexible rows can expand into the freed space.
Comparison
| Keyword | Behavior |
|---|---|
auto-fill |
Preserves empty repeated row tracks. |
auto-fit |
Collapses empty repeated row tracks after item placement. |
Important Consideration
Automatic row repetition is most useful when the grid container has a definite or constrained block size. Without a definite sizing basis, the number of repetitions may not respond as expected.
A declaration such as repeat(auto-fit, 1fr) is invalid because the track size does not provide the definite sizing information required for automatic repetition.
Content-Based Sizing
Content-based sizing allows row heights to respond to the intrinsic dimensions of their grid items.
Automatic Row Heights
.container {
grid-template-rows: auto auto auto;
}
Each row can grow according to its content and available space.
Minimum Content Size
.container {
grid-template-rows: min-content auto;
}
Maximum Content Size
.container {
grid-template-rows: max-content auto;
}
Limit Track Growth
.container {
grid-template-rows: fit-content(200px) auto;
}
The fit-content() function limits the track's maximum sizing contribution while respecting intrinsic minimum requirements.
Minimum Height with Content Growth
.container {
grid-template-rows: minmax(120px, auto) auto;
}
The first row maintains a minimum height of 120 pixels but can grow to accommodate additional content.
Important Consideration
Content-based row heights can change when text wraps, images load, or content is added. Use minimum heights rather than restrictive fixed heights when flexibility is important.
Named Grid Lines
Named grid lines allow you to reference row boundaries using descriptive identifiers instead of numbered lines.
Define Named Row Lines
.container {
display: grid;
grid-template-rows:
[header-start] 80px
[header-end main-start] 1fr
[main-end footer-start] 60px
[footer-end];
}
Place an Item Using Named Lines
.main {
grid-row: main-start / main-end;
}
Multiple Names for One Line
.container {
grid-template-rows:
[top] 80px
[middle header-end content-start] 1fr
[bottom];
}
The line between the first and second rows has three names.
Repeated Named Lines
.container {
grid-template-rows: repeat(3, [row-start] 100px);
}
.item {
grid-row-start: row-start 2;
}
The integer selects the second occurrence of the named grid line.
Named Lines from Grid Areas
.container {
display: grid;
grid-template-areas:
"header"
"content"
"footer";
}
.content {
grid-row: content-start / content-end;
}
Named grid areas automatically establish corresponding -start and -end line names.
Important Consideration
Named grid lines are case-sensitive. Use consistent names in the grid definition and item placement declarations.
Subgrid
The subgrid keyword allows a nested grid to use the row tracks of its parent grid rather than creating an independent row track list.
Define the Parent Grid
.parent-grid {
display: grid;
grid-template-rows: 80px 120px 80px;
gap: 12px;
}
Create a Row Subgrid
.nested-grid {
display: grid;
grid-row: 1 / 4;
grid-template-rows: subgrid;
}
The nested grid spans all three parent rows and shares their row track structure.
Add Line Names
.nested-grid {
grid-template-rows: subgrid [top] [middle-one] [middle-two] [bottom];
}
Additional line names can be assigned to the inherited row lines.
Important Consideration
Subgrid works when the nested grid is itself a grid item participating in the parent grid. It shares the parent track sizing rather than copying independent track size declarations.
Check browser support for subgrid separately from basic CSS Grid support.
Implicit Rows
CSS Grid creates implicit rows when items need space beyond the explicitly defined row tracks.
Define Two Explicit Rows
.container {
display: grid;
grid-template-rows: repeat(2, 100px);
}
Place an Item Beyond the Explicit Grid
.item {
grid-row: 4;
}
The item occupies the fourth row, requiring additional implicit rows beyond the two explicitly defined tracks.
Size Implicit Rows
.container {
display: grid;
grid-template-rows: repeat(2, 100px);
grid-auto-rows: 80px;
}
Allow Implicit Rows to Grow
.container {
display: grid;
grid-auto-rows: minmax(80px, auto);
}
Automatically Generate Rows
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-flow: row;
grid-auto-rows: 100px;
}
Automatically placed items can generate new rows as the grid fills.
Important Consideration
The grid-template-rows property defines explicit row sizes. Use grid-auto-rows to control the sizes of implicit rows.
Responsive Layouts
Responsive layouts can change row definitions to accommodate different screen sizes and content arrangements.
Content-Based Mobile Rows
.page-layout {
display: grid;
grid-template-columns: minmax(0, 1fr);
grid-template-rows: auto auto auto;
gap: 16px;
}
Desktop Layout with Flexible Content
@media (min-width: 768px) {
.page-layout {
min-height: 500px;
grid-template-columns: 220px minmax(0, 1fr);
grid-template-rows: 70px minmax(0, 1fr) 60px;
}
}
Automatically Sized Content Rows
.content-grid {
display: grid;
grid-template-rows: repeat(3, minmax(100px, auto));
gap: 12px;
}
Each row has a minimum height of 100 pixels but can grow when its content requires more space.
Important Consideration
Fixed row heights can cause overflow when text wraps differently on narrow screens. Use auto or minmax() for content-heavy regions.
Changing row sizes does not automatically change the placement of items with explicit grid-row values. Review placement rules when restructuring the grid.
Shorthand Relationship
The grid-template-rows property is a longhand component of the grid-template and grid shorthands.
Separate Longhand Properties
.container {
grid-template-rows: 80px 1fr 60px;
grid-template-columns: 200px 1fr;
}
Equivalent grid-template Shorthand
.container {
grid-template: 80px 1fr 60px / 200px 1fr;
}
Override Only the Rows
.container {
grid-template: 80px 1fr 60px / 200px 1fr;
grid-template-rows: 100px minmax(0, 1fr) 70px;
}
The final declaration changes the row track definitions while preserving the existing column tracks.
Shorthand Reset Behavior
.container {
grid-template-rows: repeat(3, 100px);
grid-template: 80px 1fr / 200px 1fr;
}
The later shorthand replaces the earlier three-row definition with two rows.
Important Consideration
The grid-template shorthand also sets grid-template-areas. Use grid-template-rows when you want to change only the explicit row track definitions.
Inheritance
The grid-template-rows property is not inherited.
Parent Grid Container
.parent {
display: grid;
grid-template-rows: 100px 150px;
}
Nested Grid Container
.nested {
display: grid;
grid-template-rows: 80px 80px;
}
The nested grid establishes its own row tracks rather than automatically receiving the parent's row definitions.
Explicit Inheritance
.nested {
grid-template-rows: inherit;
}
The inherit keyword copies the computed value from the parent. It does not provide the track-sharing behavior of subgrid.
Reset to the Initial Value
.container {
grid-template-rows: initial;
}
Reset with unset
.container {
grid-template-rows: unset;
}
Because the property is not inherited, unset resets it to none.
grid-template-rows Example
This example creates a three-row layout with a 60-pixel header, a 120-pixel content row, and a 50-pixel footer. Each item occupies one explicitly defined row.
HTML Example
<div class="grid-template-rows-demo">
<div class="grid-item demo-header">Header - 60px</div>
<div class="grid-item demo-content">Main Content - 120px</div>
<div class="grid-item demo-footer">Footer - 50px</div>
</div>
CSS Example
.grid-template-rows-demo {
display: grid;
grid-template-columns: minmax(0, 1fr);
grid-template-rows: 60px 120px 50px;
gap: 10px;
}
.grid-template-rows-demo .grid-item {
display: flex;
align-items: center;
justify-content: center;
padding: 10px;
background-color: #596579;
color: #ffffff;
text-align: center;
font-weight: bold;
border-radius: 4px;
min-width: 0;
}
.grid-template-rows-demo .demo-header {
background-color: #2456a6;
}
.grid-template-rows-demo .demo-footer {
background-color: #2456a6;
}
The grid contains exactly three rows. The 10-pixel gaps separate the tracks without adding additional rows.
Change to Equal Rows
.grid-template-rows-demo {
grid-template-rows: repeat(3, 100px);
}
All three rows become 100 pixels tall.
Allow the Middle Row to Grow
.grid-template-rows-demo {
grid-template-rows: 60px minmax(120px, auto) 50px;
}
The middle row maintains a minimum height of 120 pixels but can expand to accommodate additional content.
Common Mistakes
Forgetting to Enable CSS Grid
.container {
grid-template-rows: 100px 100px;
}
This declaration does not create a grid formatting context by itself. Add display: grid or display: inline-grid.
Using Commas Between Tracks
.container {
grid-template-rows: 100px, 200px;
}
Row track sizes are separated by whitespace, not commas.
Using Invalid Fractional Syntax
.container {
grid-template-rows: 1 fr 2 fr;
}
The unit must be attached to the number, such as 1fr or 2fr.
Expecting Fractional Rows to Divide an Undefined Height
When a grid container has an automatic height, fr tracks are influenced by intrinsic sizing. They do not necessarily divide a fixed amount of available height.
Using Invalid Automatic Repetition
.container {
grid-template-rows: repeat(auto-fit, 1fr);
}
Automatic repetition requires a definite sizing basis. Use a suitable fixed or constrained track size instead.
Forgetting About Gaps
Gaps add space between tracks. Percentage-based rows totaling 100% can overflow a definite-height container when gaps are added.
Using Fixed Heights for Variable Content
Content can overflow fixed-height rows when text wraps or additional elements are inserted. Use auto or minmax() when rows need to grow.
Confusing Explicit and Implicit Rows
The grid-template-rows property defines explicit tracks. Use grid-auto-rows to size implicitly generated rows.
Important Considerations
Keep the following points in mind when using grid-template-rows:
- The property defines row tracks in the explicit grid.
- Each track size creates a row unless a repetition function expands the track list.
- Fixed lengths provide predictable row heights.
- The
frunit distributes available flexible space when the grid's block size permits it. - The
repeat()function simplifies repeated track definitions. - The
minmax()function establishes minimum and maximum row sizing limits. - The
auto-fillandauto-fitkeywords support automatic track repetition. - Named grid lines provide descriptive row placement references.
- The
subgridkeyword allows nested grids to share parent row tracks. - The
grid-auto-rowsproperty controls implicit row sizing. - Grid gaps occupy space between rows.
- Content-based sizing can accommodate variable text and other content.
- Percentage row heights depend on the grid container's block size.
- The
grid-templateandgridshorthands can override row definitions. - Grid row placement does not change document or keyboard navigation order.
Browser Support
The CSS grid-template-rows 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-rows.
Advanced features such as subgrid have separate compatibility requirements. Check support for those features when using them in production layouts.
Specifications
The grid-template-rows property is defined in the CSS Grid Layout specification.
CSS Grid Layout Module Level 1: grid-template-rows
The specification defines explicit grid row tracks, track sizing, repetition, and named grid lines.
CSS Grid Layout Module Level 2
For additional documentation and examples, consult MDN Web Docs.
