|$ curl https://forge-ai.dev/api/markdown?path=docs/css/subgrid
$cat docs/css-subgrid.md
updated Today·22 min read·published

CSS Subgrid

CSSGridLayoutAdvanced🎯Free Tools
Introduction

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

Subgrid requires the nested element to span the parent tracks it should inherit — typically grid-column: 1 / -1 for full-width rows.
Basic Subgrid
subgrid-basic.css
CSS
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; }
preview
Form Rows Use Case

Align labels, inputs, and hints across multiple rows by making each row a subgrid of a shared parent template.

subgrid-forms.html
HTML
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

Subgrid inherits explicit tracks — it cannot invent sizes from purely implicit auto rows/columns in all cases. Define parent tracks clearly.
Rows, Gaps & Line Names

You can subgrid rows, columns, or both. Gap on the subgrid can differ from the parent depending on browser behavior — verify for your targets.

subgrid-rows.css
CSS
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}
AxisSyntaxTypical use
Columns onlygrid-template-columns: subgridForm rows, card headers
Rows onlygrid-template-rows: subgridSidebar sections sharing row heights
Bothboth set to subgridDense magazine-style nests
Subgrid vs Nested Grid vs Flex
ApproachAlignment across siblingsWhen to use
Nested grid (new tracks)Easy to driftTruly independent inner layout
SubgridShares parent linesVisual rhythm across components
Flex inside grid cellOne-dimensional onlyLocal packing inside a cell
Mastery Checklist

Use this checklist as a definition of done. Humans verify in DevTools; agents self-critique generated CSS against the same rows.

CheckPass criteriaFail if
Span parent tracksgrid-column spans inherited linesSubgrid on a single cell with no span
Explicit parent tracksParent defines sizesRelying on vague implicit tracks
Real use caseForms/cards align across rowsSubgrid used with no alignment need
Fallback@supports or simplified layoutBroken layout in unsupported browsers

best practice

Treat each critical fail as blocking — do not mark the topic complete until those rows pass.
Common Pitfalls

These failure modes appear in human PRs and AI-generated stylesheets. Add them to your review rubric.

PitfallWhy it hurtsFix
Forgetting to spanNothing to inheritgrid-column: 1 / -1
Expecting gap magicGap surprisesTest parent vs subgrid gap
Over-nestingHard to reasonOne subgrid level when possible
Using for everythingComplexity taxOnly when alignment is required

warning

If you repeat a pitfall, write a one-line constraint card and reuse it on the next change.
Practice

Exercise — Card titles align

Three cards in a grid; each card is a subgrid so titles, bodies, and footers share row tracks across cards.

cards-subgrid.css
CSS
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}
Deep Notes & Mental Models

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

SituationPreferAvoid
Ambiguous cascade winnerDevTools Computed + layer mapBlind !important
Reusable componentScoped styles + tokensGlobal element selectors
Motion UItransform/opacity + reduced-motionAnimating width/top
International layoutLogical propertiesHard-coded left/right
Agent generationFull markdown fetch + checklistTitles-only ingestion

Review Questions

  1. What is the primary problem this feature solves?
  2. What is the most common misuse you have seen?
  3. How does this interact with cascade layers or specificity?
  4. What accessibility or internationalization concern applies?
  5. What fallback exists when support is missing?

info

Continue with the Property Reference when you need defaults and inheritance for related properties.
📝

note

Install the CSS skill for agents: curl -s https://forgelearn.dev/skills/forgelearn-css/SKILL.md.

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.