02. Grid Items, Spanning, Template Areas & Subgrid
🟡 Intermediate
1. Definition
Grid Item properties place child elements across tracks: grid-column: span 2 (spans multiple columns), grid-template-areas (named ASCII layout map), and modern subgrid (enables nested grid items to inherit parent grid track alignments).
2. Why It Is Used
Template areas create visual ascii layout maps for pages. Subgrid ensures equal card heights and aligned button footers across nested cards in a grid row.
3. How It Works
A child grid item sets grid-template-rows: subgrid, inheriting row track sizes directly from the parent grid row.
4. Syntax
.parent-layout {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
.nested-card {
display: grid;
grid-template-rows: subgrid;
}
5. Grid Spanning & Subgrid Reference Table
| Property | Function | Example |
|---|---|---|
grid-column | Column track start / end span | grid-column: 1 / -1; (Full width) |
grid-area | Binds child to named template area | grid-area: header; |
subgrid | Inherits parent grid track sizing | grid-template-rows: subgrid; |
6. Beginner Example
.featured-card {
grid-column: span 2;
}
7. Intermediate Example
.page-layout {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-areas:
"sidebar main";
}
.sidebar-panel { grid-area: sidebar; }
.main-content-panel { grid-area: main; }
8. Advanced Modern Subgrid Example
.card-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.card {
display: grid;
grid-row: span 3;
grid-template-rows: subgrid; /* Aligns header, body, footer across all cards! */
}
9. Line-by-Line Code Explanation
grid-template-rows: subgrid;: Forces nested card elements (image, text, button) to align to parent grid row lines.
10. Expected Terminal / Browser Output
A multi-card grid where button footers align on the same baseline across all cards regardless of text length variations.
11. Common Mistakes & Pitfalls
- Forgetting
grid-row: span Nwith subgrid: Subgrid requires specifying how many parent tracks the subgrid spans.
12. Important Rules & Limitations
- Subgrid is supported across all modern evergreen browsers (Firefox 71+, Chrome 117+, Safari 16+).
13. Multilingual Explanation: English
Grid items span tracks using grid-column and grid-area. Use subgrid to align nested card components.
14. Multilingual Explanation: Hindi (Roman Script)
Grid items ko span karne ke liye grid-column aur nested card components align karne ke liye subgrid ka use karein.
15. Multilingual Explanation: Marathi (Roman Script)
Nested card footers align karnyasathi subgrid cha waparn kara.
16. Practice Questions
- How does
subgridsolve card footer alignment issues in multi-column card grids?
17. Practical Coding Challenge
Write a .header-full rule that spans across all columns using grid-column: 1 / -1.