🔨 All in one Utilities

CSS Subgrid Generator

Great utilities that help you design, program and maintain your website

utilities for you to make a website
Home » Utilities » CSS Subgrid Generator

CSS Subgrid Generator

The cards deliberately have titles of different lengths — that is the case subgrid exists for.
The card must span this many rows to inherit them.
Inherited from the parent unless overridden — this is the override.

Note: The commonest reason a subgrid declaration appears to do nothing is that the child is not spanning the tracks it wants to inherit. A card with three internal rows needs grid-row: span 3 as well.

Subgrid solves one problem precisely, and it is worth stating it exactly: a nested grid can inherit its parent's track sizes instead of creating its own. Before it existed, a row of cards could be made equal height easily enough, but the elements inside them — the heading, the body text, the button — sat wherever their own content put them. One card with a two-line title pushed its body down and every card in the row was internally misaligned.

With subgrid, each card spans a set of the parent's rows and declares grid-template-rows: subgrid. The heading row is now one shared track across all the cards, so it is as tall as the tallest heading and every body starts on the same line. The footers line up at the bottom without a single absolute position or a hardcoded min-height.

The two constraints that trip people up are both about inheritance. First, subgrid only applies to the axis you name — subgrid on rows does not affect columns, and a card needing both must say so. Second, the child must actually span the tracks it wants to inherit, which means grid-row: span 3 on a card that has three internal rows. Leaving that off is why a subgrid declaration often appears to do nothing at all.

The gap is inherited too, and can be overridden on the child, which is the neat detail: cards can sit twenty pixels apart while their internal rows sit eight pixels apart, and both are still one grid.

Frequently Asked Questions

Why does my subgrid declaration seem to do nothing?

The child almost certainly is not spanning the tracks it wants to inherit. subgrid means 'use my parent's tracks for the area I occupy', so a card that occupies a single row has exactly one track to inherit. Add grid-row: span 3 for three internal rows.

Does subgrid work on both axes at once?

Yes, but you have to say so on each. grid-template-rows: subgrid affects rows only; columns keep whatever definition you give them. Both lines are needed for a child that should inherit the full parent grid.

What is the fallback for browsers without subgrid?

All current browsers support it, so the question is only about older versions. The graceful path is a normal nested grid — the cards still work, the contents just are not cross-aligned. Wrap the subgrid rules in @supports (grid-template-rows: subgrid) and the generator includes that block.

Should I use subgrid or flexbox for card layouts?

Flexbox aligns items along one axis within a single container and knows nothing about its siblings, so it cannot align a heading in one card against a heading in another. That cross-container alignment is exactly and only what subgrid does. For a single card's internal stacking, flexbox is simpler.

Related Utilities