CSS Subgrid allows a nested grid to align with its parent grid's tracks. This powerful feature simplifies complex layouts by enabling inner grid items to participate in the outer grid's sizing and alignment, creating more consistent and manageable designs.
Example 1: Basic Subgrid Adoption
/* This code demonstrates a basic subgrid implementation. */
.parent-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
.nested-item {
grid-column: 2 / 4; /* Span across two columns of the parent grid */
display: grid;
grid-template-columns: subgrid; /* Inherit the parent's track sizing */
gap: 5px; /* Can have its own gap */
}
.sub-item {
background-color: #f0f0f0;
}
Explanation
The .nested-item
is a grid container itself but by setting grid-template-columns
to subgrid
, it doesn't define its own column tracks. Instead, it adopts the tracks of its parent, .parent-grid
, allowing its children, .sub-item
, to align perfectly within the parent's grid structure.
Example 2: Spanning Subgrid
/* This example shows a subgrid spanning specific parent tracks. */
.main-grid {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-rows: auto 1fr;
gap: 15px;
}
.header-area {
grid-column: 1 / -1; /* Span all columns */
display: grid;
grid-template-columns: subgrid;
}
.logo {
grid-column: 1; /* Aligns to the first track of the parent grid */
background-color: lightblue;
}
.nav {
grid-column: 2 / 4; /* Aligns to the second and third tracks of the parent */
background-color: lightgreen;
}
Explanation
Here, .header-area
spans all columns of .main-grid
. By using subgrid
, its children, .logo
and .nav
, can be placed on the parent's grid tracks as if they were direct children of .main-grid
, ensuring precise alignment across the header.
Example 3: Row and Column Subgrid
/* This code illustrates using subgrid for both rows and columns. */
.container-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 100px);
gap: 10px;
}
.card {
grid-column: 1 / 3;
grid-row: 1 / 3;
display: grid;
grid-template-columns: subgrid;
grid-template-rows: subgrid; /* Inherit parent's row tracks as well */
}
.card-title {
grid-column: 1;
grid-row: 1;
background-color: #ffcccb;
}
.card-body {
grid-column: 2;
grid-row: 1 / 3; /* Spans both inherited rows */
background-color: #add8e6;
}
Explanation
The .card
element adopts both the column and row tracks from .container-grid
. This allows elements inside the card, like .card-title
and .card-body
, to align with the overall page grid both horizontally and vertically.
Example 4: Subgrid for Form Layout
/* This demonstrates using subgrid for clean form alignment. */
.form-grid {
display: grid;
grid-template-columns: 100px 1fr;
gap: 20px;
}
.form-row {
grid-column: 1 / -1;
display: grid;
grid-template-columns: subgrid; /* Aligns all labels and inputs */
gap: 10px; /* Overrides parent gap */
}
.form-row label {
text-align: right;
background-color: #eee;
}
Explanation
Each .form-row
becomes a subgrid of the main .form-grid
. This ensures that all <label>
and <input>
elements across different rows align perfectly into the two columns defined by the parent, creating a visually consistent form.
Example 5: Aligning Nested Content
/* This shows how subgrid helps align deeply nested items. */
.page-layout {
display: grid;
grid-template-columns: 200px 1fr;
}
.content {
grid-column: 2;
display: grid;
grid-template-columns: subgrid;
}
.article-header {
grid-column: 1 / -1; /* Spans the subgridded column */
display: grid;
grid-template-columns: subgrid;
}
.article-title {
grid-column: 1;
background-color: #f9f9f9;
}
Explanation
This example features a nested subgrid. The .content
inherits tracks from .page-layout
, and .article-header
in turn inherits from .content
. This allows .article-title
to align with the grid track originally defined two levels up in .page-layout
, demonstrating subgrid's power in complex alignments.