# Subgrid

> Align elements inside nested containers perfectly with each other by sharing the parent's grid tracks.

Subject: CSS | Lesson 25 of 44 | 7 min read
Interactive lesson: https://codewithanimation.in/css/subgrid
Previous: [Grid Areas & Dynamic Functions](https://codewithanimation.in/css/grid-areas-and-functions.md) | Next: [Media Queries & Mobile-First](https://codewithanimation.in/css/media-queries.md)

## What it is

`subgrid` is a special value for `grid-template-columns` or `grid-template-rows`. It allows a nested grid container to adopt the grid tracks defined by its parent, rather than creating its own independent grid.

## Why it matters

Imagine a row of three product cards. Each card has an image, a title, a description, and a 'Buy' button at the bottom. If Card B has a very long title, it pushes its description and button down.

Before `subgrid`, it was impossible to align the 'Buy' buttons across all three cards natively, because the contents of Card A had no idea how tall the contents of Card B were.

With `subgrid`, all three cards can share the exact same row tracks from a master parent grid, ensuring their internal contents perfectly align with each other.

## How it works

1. Create a parent grid with rows (or columns) that the children should share: `.parent { display: grid; grid-template-rows: auto 1fr auto; }`

2. Make the child span across those rows in the parent: `.card { grid-row: span 3; }`

3. Make the child a grid itself, and tell it to use the parent's tracks: `.card { display: grid; grid-template-rows: subgrid; }`

## Common mistake: The Span Requirement Trap

The most common mistake with `subgrid` is forgetting that the child element must *span* the tracks it wants to sub-divide. If a card needs to align 3 internal elements (image, text, button), it must explicitly span 3 tracks in the parent grid (`grid-row: span 3`).

## Tip: Gaps are inherited

A subgrid automatically inherits the `gap` defined by its parent. You don't need to redeclare it. However, if you explicitly set a gap on the subgrid, it will override the inherited gap.

## Try it: Try it

Toggle Subgrid on and off. Notice how the 'Buy' buttons at the bottom of the cards behave. Without Subgrid, they stick to the text above them. With Subgrid, they snap to a shared parent track and perfectly align horizontally!

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/subgrid

## Check yourself

### Question 1

What is the primary problem that `subgrid` solves?

- A. Aligning nested elements across different sibling containers
- B. Creating a grid inside a grid with a completely independent layout
- C. Allowing grid items to overlap each other

Correct answer: A

Subgrid allows nested containers to share the tracks of their common parent. This makes it possible to perfectly align the internal contents of multiple sibling cards.

- Why not B: You can do that with standard nested `display: grid`. Subgrid explicitly links them, it doesn't make them independent.
- Why not C: Standard grid line placement allows overlapping; subgrid is for track sharing.

### Question 2

Which property actually activates the subgrid behavior on a nested container?

- A. grid-template-rows: subgrid;
- B. display: subgrid;
- C. grid-auto-flow: subgrid;

Correct answer: A

The nested element is declared as a normal grid (`display: grid`), but its tracks are explicitly set to inherit the parent's tracks via `grid-template-columns: subgrid` or `grid-template-rows: subgrid`.

- Why not B: `display: subgrid` does not exist. You use `display: grid` combined with `grid-template-rows: subgrid`.
- Why not C: `grid-auto-flow` controls how items are placed automatically, not track definition.

### Question 3

If a card using `grid-template-rows: subgrid` contains an image, a title, and a button, what must be true about the card's placement in the parent grid?

- A. It must explicitly span 3 rows in the parent grid (e.g., grid-row: span 3)
- B. It must be placed in a specific column
- C. It must have a fixed height

Correct answer: A

A subgrid can only use the tracks it spans across in the parent. To align 3 elements using a row subgrid, the container card must span 3 rows of the parent grid.

- Why not B: It can be placed automatically in any column.
- Why not C: Grid tracks can be flexible (auto or 1fr).

## Remember this

- `subgrid` allows nested grids to participate in their parent's track sizing.
- It's perfect for aligning card contents (like headers or footers) across a row.
- The subgrid child must explicitly span the number of tracks it needs to utilize.
- Use `display: grid` combined with `grid-template-rows: subgrid`.
