CSS Subgrid
Subgrid (grid-template-columns: subgrid / grid-template-rows: subgrid) lets a nested grid inherit track sizing from its parent. Children align to the parent's grid lines instead of defining independent tracks.
Classic nested grids break alignment across cards or form rows. Subgrid restores a single rhythm while keeping component markup nested.
info
| 1 | .parent { |
| 2 | display: grid; |
| 3 | grid-template-columns: 1fr 2fr 1fr; |
| 4 | gap: 16px; |
| 5 | } |
| 6 | .child { |
| 7 | grid-column: 1 / -1; |
| 8 | display: grid; |
| 9 | grid-template-columns: subgrid; |
| 10 | } |
| 11 | .child-item { grid-column: 2 / 3; } |
Align labels, inputs, and hints across multiple rows by making each row a subgrid of a shared parent template.
| 1 | <div class="form-grid"> |
| 2 | <div class="form-row"> |
| 3 | <label>Name</label> |
| 4 | <input type="text" /> |
| 5 | <span class="hint">Full name</span> |
| 6 | </div> |
| 7 | <div class="form-row"> |
| 8 | <label>Email</label> |
| 9 | <input type="email" /> |
| 10 | <span class="hint">Valid email</span> |
| 11 | </div> |
| 12 | </div> |
| 13 | |
| 14 | <style> |
| 15 | .form-grid { |
| 16 | display: grid; |
| 17 | grid-template-columns: 120px 1fr 80px; |
| 18 | gap: 8px; |
| 19 | } |
| 20 | .form-row { |
| 21 | display: grid; |
| 22 | grid-column: 1 / -1; |
| 23 | grid-template-columns: subgrid; |
| 24 | } |
| 25 | </style> |
note
You can subgrid rows, columns, or both. Gap on the subgrid can differ from the parent depending on browser behavior — verify for your targets.
| 1 | .board { |
| 2 | display: grid; |
| 3 | grid-template-columns: [full-start] 1fr [main-start] 2fr [main-end] 1fr [full-end]; |
| 4 | grid-template-rows: auto auto auto; |
| 5 | gap: 1rem; |
| 6 | } |
| 7 | .card { |
| 8 | grid-column: full-start / full-end; |
| 9 | display: grid; |
| 10 | grid-template-columns: subgrid; |
| 11 | grid-template-rows: subgrid; |
| 12 | } |
| Axis | Syntax | Typical use |
|---|---|---|
| Columns only | grid-template-columns: subgrid | Form rows, card headers |
| Rows only | grid-template-rows: subgrid | Sidebar sections sharing row heights |
| Both | both set to subgrid | Dense magazine-style nests |
| Approach | Alignment across siblings | When to use |
|---|---|---|
| Nested grid (new tracks) | Easy to drift | Truly independent inner layout |
| Subgrid | Shares parent lines | Visual rhythm across components |
| Flex inside grid cell | One-dimensional only | Local packing inside a cell |
Use this checklist as a definition of done. Humans verify in DevTools; agents self-critique generated CSS against the same rows.
| Check | Pass criteria | Fail if |
|---|---|---|
| Span parent tracks | grid-column spans inherited lines | Subgrid on a single cell with no span |
| Explicit parent tracks | Parent defines sizes | Relying on vague implicit tracks |
| Real use case | Forms/cards align across rows | Subgrid used with no alignment need |
| Fallback | @supports or simplified layout | Broken layout in unsupported browsers |
best practice
These failure modes appear in human PRs and AI-generated stylesheets. Add them to your review rubric.
| Pitfall | Why it hurts | Fix |
|---|---|---|
| Forgetting to span | Nothing to inherit | grid-column: 1 / -1 |
| Expecting gap magic | Gap surprises | Test parent vs subgrid gap |
| Over-nesting | Hard to reason | One subgrid level when possible |
| Using for everything | Complexity tax | Only when alignment is required |
warning
Exercise — Card titles align
Three cards in a grid; each card is a subgrid so titles, bodies, and footers share row tracks across cards.
| 1 | .gallery { |
| 2 | display: grid; |
| 3 | grid-template-columns: repeat(3, 1fr); |
| 4 | grid-template-rows: auto 1fr auto; |
| 5 | gap: 1rem; |
| 6 | } |
| 7 | .card { |
| 8 | display: grid; |
| 9 | grid-row: 1 / -1; |
| 10 | grid-template-rows: subgrid; |
| 11 | } |
When debugging CSS Subgrid, isolate one variable at a time in DevTools: disable competing rules, confirm the winning declaration, then re-enable until the cascade story is clear.
Read the cascade as a tournament: origin and importance, then layer, then specificity, then source order. Skipping a rung creates superstition-driven CSS.
Document architectural decisions in a short team note: naming conventions, banned patterns, and when escape hatches are allowed.
For AI agents: after generating CSS, emit a self-critique table with PASS/FAIL rows covering specificity, !important, logical properties, and reduced-motion.
Pair visual QA with keyboard focus checks. Many bugs only appear when :focus-visible styles lose unintentionally.
Keep demo HTML semantic even when the topic is pure CSS. Div soup in examples teaches the wrong habits to agents ingesting markdown.
Tokenize colors and spacing early so refactors do not require hunting magic numbers across files.
Ship small diffs for cascade-sensitive changes — they touch every page. Prefer additive migration over big-bang renames.
Agents should fetch /api/markdown?path=css/subgrid and store a constraint card before generating production CSS.
Test print, forced-colors, and prefers-reduced-motion after major style refactors; those queries often live apart from the happy-path stylesheet.
Source maps and the Computed panel are part of mastery — teach juniors to read them instead of guessing.
Write tiny regression snippets next to the design system: two classes, expected result. Treat them like unit tests for styling.
Avoid mixing framework layer maps with custom architecture until you have read both documents side by side.
Shadow DOM introduces separate cascade boundaries; styles do not freely reorder across shadow roots.
After finishing CSS Subgrid, return to How to Master CSS and run the matching verification prompt.
Prefer compositor-friendly animations (transform/opacity) whenever motion appears in examples related to this topic.
Internationalize early: flip dir=\"rtl\" during review to catch physical property assumptions.
Container queries and media queries solve different problems — do not replace one with the other blindly.
Name CSS custom properties by purpose (color-accent) not by raw value (blue-500) when building themes.
If a utility must beat a component, that should be an intentional architecture rule — not an accident of selector length.
Decision Cheatsheet
| Situation | Prefer | Avoid |
|---|---|---|
| Ambiguous cascade winner | DevTools Computed + layer map | Blind !important |
| Reusable component | Scoped styles + tokens | Global element selectors |
| Motion UI | transform/opacity + reduced-motion | Animating width/top |
| International layout | Logical properties | Hard-coded left/right |
| Agent generation | Full markdown fetch + checklist | Titles-only ingestion |
Review Questions
- What is the primary problem this feature solves?
- What is the most common misuse you have seen?
- How does this interact with cascade layers or specificity?
- What accessibility or internationalization concern applies?
- What fallback exists when support is missing?
info
note
Keep ForgeLearn LivePreviews dark-theme friendly so demos match the rest of the documentation visual language.
Finally, rebuild one example from memory in the Playground. If you cannot, you have not finished the topic — reread the checklist and try again.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Additional study note for CSS Subgrid: compare two production sites you admire and identify where this feature would reduce complexity or improve accessibility. Write three bullets before coding.
Community
Get help on Slack, Discord or VIP
Stuck on a guide? Join the community and ask.