CSS Subgrid Layout Not Working Fix
In this tutorial, you'll learn about CSS Subgrid Layout Not Working Fix. We cover key concepts, practical examples, and best practices.
The Problem
CSS subgrid allows nested grid items to align with their parent grid's tracks. When subgrid does not work, the child grid creates its own independent track sizing, breaking alignment with the parent. Missing subgrid declaration or incorrect parent grid setup are the usual causes.
Quick Fix
Step 1: Enable subgrid on the child
/* Wrong — child creates its own independent grid */
.parent-grid {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}
.child-grid {
display: grid;
grid-template-columns: 1fr 1fr; /* Independent tracks */
}
/* Right — use subgrid to inherit parent tracks */
.parent-grid {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}
.child-grid {
display: grid;
grid-template-columns: subgrid; /* Inherits parent tracks */
grid-column: 1 / -1;
}
Step 2: Span the correct columns
/* Wrong — subgrid item does not span enough columns */
.parent-grid {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}
.child-grid {
display: grid;
grid-template-columns: subgrid;
grid-column: 1 / 2; /* Only spans 1 column */
/* Subgrid items only have 1 track available */
}
/* Right — span all columns */
.child-grid {
display: grid;
grid-template-columns: subgrid;
grid-column: 1 / -1; /* Spans all 3 columns */
}
Step 3: Subgrid on rows for consistent heights
.parent-grid {
display: grid;
grid-template-rows: auto 1fr auto;
}
/* Wrong — each child sizes rows independently */
.child-grid {
display: grid;
grid-template-rows: auto auto auto;
}
/* Right — subgrid inherits row tracks */
.child-grid {
display: grid;
grid-template-rows: subgrid;
grid-row: 1 / -1;
}
Step 4: Provide fallback for unsupported browsers
/* Wrong — no fallback */
.child-grid {
display: grid;
grid-template-columns: subgrid;
}
/* Right — provide standard grid as fallback */
.child-grid {
display: grid;
grid-template-columns: 1fr 1fr; /* fallback */
}
@supports (grid-template-columns: subgrid) {
.child-grid {
grid-template-columns: subgrid;
grid-column: 1 / -1;
}
}
Step 5: Subgrid with nested gaps
.parent-grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 1rem;
}
.child-grid {
display: grid;
grid-template-columns: subgrid;
grid-column: 1 / -1;
/* Wrong — can set own gap that overrides parent */
gap: 0;
}
/* Right — let parent gap propagate */
.child-grid {
display: grid;
grid-template-columns: subgrid;
grid-column: 1 / -1;
gap: inherit;
}
Prevention
- Ensure the parent grid has defined
grid-template-columns - Set
grid-columnon the subgrid element to span the right number of tracks - Use
@supportsfor progressive enhancement - Verify that
subgridis spelled correctly (common typo:subgridvssub-grid) - Test in Firefox DevTools which has excellent subgrid debugging support
Common Mistakes with subgrid error
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world CSS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro