🎛️ Interactive Grid Playground
Experiment with CSS Grid properties and see their effects in real-time:
Container Properties:
Selected Item Properties:
Click on a grid item to select and modify it
Generated CSS:
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: auto auto auto;
gap: 15px;
justify-items: stretch;
align-items: stretch;
grid-row: auto;
justify-self: auto;
align-self: auto;
📐 Grid Template Areas
Learn to create complex layouts using named grid areas:
.container {
display: grid;
grid-template-areas:
"header header header"
"nav main sidebar"
"footer footer footer";
grid-template-columns: 200px 1fr 250px;
gap: 1rem;
}
.header { grid-area: header; }
.nav { grid-area: nav; }
.main { grid-area: main; }
.sidebar { grid-area: sidebar; }
.footer { grid-area: footer; }
.holy-grail {
display: grid;
grid-template-areas:
"header header header"
"nav main aside"
"footer footer footer";
grid-template-rows: auto 1fr auto;
grid-template-columns: 150px 1fr 150px;
min-height: 100vh;
}
.dashboard {
display: grid;
grid-template-areas:
"header header header"
"sidebar stats chart"
"sidebar table notifications";
grid-template-columns: 250px 1fr 300px;
grid-template-rows: 80px 200px 1fr;
}
.magazine {
display: grid;
grid-template-areas:
"hero hero article1"
"hero hero article2"
"article3 article3 ads";
grid-template-columns: 2fr 1fr 1fr;
grid-template-rows: 1fr 1fr 200px;
}
⚖️ Grid vs Flexbox Comparison
Understand when to use Grid vs Flexbox for different layout scenarios:
CSS Grid (2D Layout)
✅ Controls both rows and columns simultaneously
Flexbox (1D Layout)
✅ Controls either row OR column direction
Grid: Layout First
🏗️ Define the layout structure first, then place content
Flexbox: Content First
📄 Content size determines the layout behavior
Grid Alignment
🎯 Individual item alignment within grid cells
Flex Alignment
↔️ Alignment along flex axis
🎯 Use CSS Grid When:
- Creating complex 2D layouts
- Building page-level layouts
- You need precise positioning
- Overlapping elements are needed
- Layout structure is predetermined
- Working with grid-like designs
🔄 Use Flexbox When:
- Creating 1D layouts (rows or columns)
- Building component-level layouts
- Content size should affect layout
- You need flexible item sizing
- Centering content easily
- Navigation bars, card layouts
🤝 Combine Both When:
- Grid for overall page structure
- Flexbox for components within grid areas
- Complex responsive designs
- Modern web applications
- Best of both worlds approach
- Maximum layout flexibility
💪 Grid Layout Challenge
Test your CSS Grid skills by creating responsive layouts:
📸 Create a Responsive Photo Gallery
Build a photo gallery that adapts to different screen sizes using CSS Grid auto-fit and minmax.
Target Result:
Your CSS:
Your Result:
💡 Hint: Use repeat(auto-fit, minmax(200px, 1fr)) for responsive columns!
📚 CSS Grid Quick Reference
Container Properties
display: grid- Creates grid containergrid-template-columns- Define column tracksgrid-template-rows- Define row tracksgrid-template-areas- Named grid areasgap- Space between tracksjustify-items- Horizontal item alignmentalign-items- Vertical item alignment
Item Properties
grid-column- Column placementgrid-row- Row placementgrid-area- Area placement or shorthandjustify-self- Individual horizontal alignmentalign-self- Individual vertical alignment
Units & Functions
fr- Fraction of available spacerepeat()- Repeat track patternsminmax()- Size range for tracksauto-fit- Fit tracks to containerauto-fill- Fill container with tracksmin-content- Minimum content sizemax-content- Maximum content size
Common Patterns
repeat(auto-fit, minmax(250px, 1fr))- Responsive columns1fr 1fr 1fr- Equal columnsgrid-column: 1 / -1- Full widthplace-items: center- Center everythinggrid: repeat(3, 1fr) / repeat(3, 1fr)- 3x3 grid shorthand