Sub topic 2
Spanning Tracks and Placing Items Inside a Subgrid
Assume you know CSS selectors, the box model, and basic Grid placement with tracks, lines, span, and gap.
Spanning Tracks and Placing Items Inside a Subgrid
A subgrid only inherits the parent tracks that its own grid area covers. Use grid-column or grid-row to make the grid item span the required tracks.
.layout {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}
.section {
display: grid;
grid-column: 1 / -1;
grid-template-columns: subgrid;
}
.section > .main {
grid-column: 2;
}Here, .section spans all three parent columns, so its subgrid has three inherited columns. Its children can use the subgrid’s local line numbers. A subgrid can also span only part of the parent:
.sidebar-section {
grid-column: 1 / 3;
display: grid;
grid-template-columns: subgrid;
}This subgrid inherits only the parent columns between lines 1 and 3.
Aligning Card Headers, Bodies and Footers
Nested cards often have different title and body heights. Independent grids cannot align their internal rows. A row subgrid lets every card borrow the same parent rows.
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1rem;
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
row-gap: 0.5rem;
}Each card spans three parent rows. Its three direct children fill those rows in order: title, body, and link. The links therefore line up across cards.
Subgrid in One Axis or Both
Subgrid applies independently to columns and rows:
.panel {
display: grid;
grid-template-columns: subgrid;
grid-template-rows: subgrid;
}Use it in one axis when only columns or rows need alignment. Use both when the nested items must follow the parent’s complete two-dimensional structure.
Inheriting and Overriding Gaps
A subgrid inherits the parent’s gap in each subgridded axis. You can override that spacing locally:
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid;
row-gap: 0.5rem;
}This keeps the parent’s column gap while changing the spacing between the card’s inherited rows.
Exercise
Why must a card span three parent rows in the aligned-card pattern?
Show answer
It needs to inherit three tracks so its title, body, and footer can occupy matching rows.