πŸ“ grid-template-rows β€” Defining Rows in CSS Grid

🌐 What is grid-template-rows?

grid-template-rows is a CSS Grid property used to define theheight, number, and behavior of rows inside a grid container. It works just like grid-template-columns, but vertically. With this property, you can create fixed-height rows, flexible rows, auto-sized rows, or even fully responsive vertical layouts.

>>β€œIf columns define width, grid-template-rows defines vertical rhythm.”

πŸ“Œ Basic Example

Creating 3 Rows

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

This creates three rows with fixed heights.

πŸ“ Row Size Units

UnitDescription
pxFixed height
%Percentage of container height
frFractional space distribution
autoHeight based on content
minmax()Minimum + Maximum flexible size

πŸ”₯ Using fr Units for Row Flexibility

The fr unit divides the available vertical space proportionally.

Fractional Rows

grid-template-rows: 1fr 2fr 1fr;
/* Middle row is twice as tall */

πŸ“ auto Height Rows

auto Row Example

grid-template-rows: auto auto 1fr;

First two rows expand only as much as the content needs; the last row takes up remaining space.

πŸ” repeat() for cleaner syntax

Repeat Rows

grid-template-rows: repeat(3, 100px);
/* Same as: 100px 100px 100px */

πŸ“± Responsive Row Control with minmax()

MinMax Rows

grid-template-rows: minmax(150px, auto) 1fr 2fr;

The first row will always be at least 150px, but may grow larger if needed.

🧠 Explicit vs Implicit Rows

Explicit Rows

Rows defined using grid-template-rows.

Implicit Rows

When grid items overflow the defined rows, extra rows are created usinggrid-auto-rows.

Implicit Rows Example

.container {
  display: grid;
  grid-template-rows: 100px 100px;
  grid-auto-rows: 80px; /* rows added automatically */
}

Note

πŸ’‘ Use grid-auto-rows when you’re unsure how many rows will appear dynamically.

🧩 Practical Layout Example: Header + Content + Footer

Common Web Layout

.layout {
  display: grid;
  grid-template-rows: 80px 1fr 60px;
  height: 100vh;
}

β€’ Top row = fixed header
β€’ Middle row = flexible content
β€’ Bottom row = fixed footer

Media content

πŸ“Š Visual Examples of grid-template-rows

ValueRows CreatedDescription
100px 100px2Two fixed-height rows
1fr 1fr2Equal flexible rows
repeat(3, auto)3Fit content automatically

⚠️ Common Mistakes

  • Using only fixed px values β†’ breaks responsiveness
  • Forgetting that % units depend on container height
  • Not using minmax() for controlled flexibility
  • Expecting rows to grow automatically without auto or fr units

Note

🧠 Tip: Rows rarely need fixed px heights β€” prefer fr, auto, or minmax() for better layouts.

πŸ”₯ Summary

grid-template-rows defines the vertical structure of your grid. With units like fr, auto, and minmax(), you can build powerful, responsive layouts that adapt naturally to content and screen size.

>>β€œGreat layouts start with strong row and column foundations.”

Explore more here ➜MDN Docs πŸ“˜