Grid Placement and Common Patterns
Introduction
After learning Grid tracks, the next step is placing items intentionally. Grid placement lets you control where components start, how far they span, and how template areas define full-page structure. This chapter focuses on practical placement techniques and reusable layout patterns for real projects.
Prerequisites
- Grid basics (
display: grid, tracks,fr,gap) - Basic responsive layout understanding
- Familiarity with DevTools Grid overlay
Placement Fundamentals
Grid placement is based on grid lines and spans.
You usually control placement with:
grid-columngrid-rowgrid-column-start/grid-column-endgrid-row-start/grid-row-end
Basic example:
.feature {
grid-column: 1 / 3;
}This item starts at column line 1 and ends at line 3 (spans 2 columns).
Line-Based Placement
A simple 4-column grid:
.layout {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 1rem;
}Place items:
.sidebar {
grid-column: 1 / 2;
}
.content {
grid-column: 2 / 5;
}This creates a narrow sidebar + wide content area.
Using span
span is often clearer than counting end lines manually:
.hero {
grid-column: span 2;
}Meaning:
- occupy two columns from current auto-placement position
Useful for card grids where some cards should be wider.
Row Placement
You can control vertical placement the same way:
.banner {
grid-row: 1 / 3;
}This item spans two row tracks.
Be careful with fixed row heights. Content-heavy sections usually need flexible row sizing.
Named Grid Areas
For full-page or dashboard shells, named areas improve readability.
Benefits:
- visual mapping in CSS
- easier maintenance for team collaboration
- clear intent in large layouts
grid-template-areas Rules
When using template areas:
- each row string must have same number of cells
- repeated area names merge into larger regions
- use
.for empty cells
Example:
grid-template-areas:
"hero hero sidebar"
"content content sidebar"
"footer footer footer";This is expressive and easier to scan than many line numbers.
Auto-Placement Behavior
If you do not set explicit positions, Grid auto-places items by source order.
Useful for simple card lists:
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 1rem;
}You can combine mostly auto-placement with a few explicitly placed “featured” items.
Dense Packing
grid-auto-flow: dense; can backfill visual gaps:
.gallery {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 0.75rem;
grid-auto-flow: dense;
}Use carefully:
- can improve visual packing
- may produce visual order different from DOM order
For content where reading order matters, avoid dense reordering side effects.
Alignment in Grid
Container-level:
justify-itemsalign-itemsplace-items
Item-level:
justify-selfalign-selfplace-self
Example:
.grid {
display: grid;
place-items: start stretch;
}
.cta {
justify-self: end;
}This gives per-item control without affecting all siblings.
Pattern 1: Marketing Hero + Side Panel
Simple and stable for landing pages.
Pattern 2: Dashboard Cards with Featured Tile
.dashboard {
display: grid;
grid-template-columns: repeat(4, minmax(160px, 1fr));
gap: 1rem;
}
.featured {
grid-column: span 2;
grid-row: span 2;
}Creates visual hierarchy while keeping overall grid consistency.
Pattern 3: Holy Grail Page Shell
.shell {
display: grid;
grid-template-columns: 220px 1fr 280px;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header header"
"left main right"
"footer footer footer";
min-height: 100dvh;
gap: 1rem;
}Great for docs, admin tools, and content-heavy interfaces.
Responsive Area Redefinition
You can redefine areas in media queries:
@media (max-width: 900px) {
.page {
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}
}This keeps semantic structure while adapting layout cleanly.
Tip
Areas Improve Team Readability
For complex page shells, named areas often communicate intent faster than long line-based placement rules.
Common Mistakes
| Mistake | Why it hurts | Better approach |
|---|---|---|
| Hardcoding many line numbers everywhere | Hard to maintain | Use areas for macro layout, lines for local adjustments |
| Incorrect area row width consistency | Invalid template parsing | Ensure each area row has equal cell count |
| Overusing dense auto-flow | Visual order confusion | Use only where reading order is not critical |
| Fixed rows for dynamic text content | Overflow/clipping | Prefer auto or flexible row sizing |
| Ignoring responsive redefinition | Layout breaks on narrow screens | Redefine tracks/areas in media queries |
Debugging Placement Issues
When an item appears in the wrong place:
- enable Grid overlay in DevTools
- inspect computed
grid-column/grid-row - verify track count and line numbers
- confirm template area names match exactly
- test responsive breakpoints for area redefinition conflicts
Grid overlay labels are the fastest way to verify placement intent.
Mini Exercise
- Build a 4-column dashboard grid with 8 cards
- Make one card span 2 columns and 2 rows
- Rebuild layout using
grid-template-areas - Add a mobile media query switching to single-column flow
- Use DevTools overlay to verify lines and area mapping
FAQ
Should I use line numbers or template areas?
Use line numbers for local item adjustments; use template areas for larger page-level structure.
Why is grid-template-areas not working?
Common causes: mismatched area names, unequal column count per row string, or missing grid-area assignments on items.
Is grid-auto-flow: dense good for content layouts?
Usually avoid it for reading-heavy content because visual order may diverge from source order.
Can Grid items overlap intentionally?
Yes, with explicit placement. If overlapping, manage layer order with z-index and ensure interaction/accessibility remain clear.
Do I still need Flexbox if I use Grid?
Yes. Grid handles macro two-dimensional layout; Flexbox is still excellent for one-dimensional alignment inside components.