Lesson 3 of 10
Auto-placement & Implicit Grid
When items don't fit in the explicitly defined grid, CSS Grid automatically creates new rows or columns - this is the implicit grid. Understanding auto-placement and implicit tracks is key to handling dynamic content.
Auto-placement Concepts
- Explicit grid - What you define: grid-template-columns, grid-template-rows
- Implicit grid - Rows/columns created automatically when items overflow the explicit grid
- grid-auto-rows - Size of implicitly created rows: grid-auto-rows: minmax(100px, auto)
- grid-auto-columns - Size of implicitly created columns
- grid-auto-flow - row (default, fill rows first), column (fill columns first), dense (fill gaps)
- dense packing - grid-auto-flow: dense tries to fill holes in the grid. Reorders items visually
- Accessibility with dense - Dense reorders visual display but NOT DOM order. Screen readers follow DOM order
Auto-placement Code
/* Implicit rows for dynamic content */
.blog-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: minmax(200px, auto); /* New rows at least 200px */
gap: 20px;
}
/* No matter how many blog posts, each gets a proper row */
/* Dense packing fills gaps */
.masonry-like {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 150px;
grid-auto-flow: dense; /* Fill holes */
gap: 12px;
}
.masonry-like .large {
grid-column: span 2;
grid-row: span 2;
}
/* Smaller items fill the gaps left by large items */Tip
Always define grid-auto-rows on dynamic grids that receive unknown content. Use grid-auto-rows: minmax(150px, auto) - items have a minimum height but grow to fit their content. This prevents collapsed zero-height rows and unexpected layout breakage when data changes.
Implicit grid tracks are automatically created when content overflows explicit track definitions
Common Mistake
Using grid-auto-flow: dense without understanding that it reorders items visually (not in the DOM). Dense packing fills gaps but changes the visual reading order - which can confuse both users and screen readers. Only use it for purely decorative grids like image galleries.
Practice Task
Create a blog post grid: (1) 3 columns with repeat(3, 1fr) + gap: 20px, (2) grid-auto-rows: minmax(200px, auto), (3) Add 8+ cards and observe auto-placement in action. Then switch to grid-auto-flow: dense and add some grid-column: span 2 cards to see gap-filling behavior.
Quick Quiz
Key Takeaways
- When items don't fit in the explicitly defined grid, CSS Grid automatically creates new rows or columns - this is the implicit grid.
- Explicit grid - What you define: grid-template-columns, grid-template-rows
- Implicit grid - Rows/columns created automatically when items overflow the explicit grid
- grid-auto-rows - Size of implicitly created rows: grid-auto-rows: minmax(100px, auto)
Finished this lesson?