CSS Grid: The Complete Guide
Master CSS Grid from first principles. Learn grid-template-areas, auto-placement, subgrid, and real-world layout patterns every developer needs.
Grid vs Flexbox
Flexbox is one-dimensional — it handles rows OR columns. Grid is two-dimensional — it handles rows AND columns simultaneously. Use both; they complement each other.
Defining a Grid
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
gap: 1.5rem;
}
Named Areas
The most readable way to define complex layouts:
.layout {
display: grid;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
grid-template-columns: 250px 1fr 1fr;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
Auto-Placement
/* Cards that automatically fill available space */
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 1.5rem;
}
Spanning Items
.featured {
grid-column: span 2;
grid-row: span 2;
}
Practical Patterns
- Holy Grail Layout: header, footer, sidebar, main — all in ~10 lines of CSS
- Masonry-like:
grid-template-rows: masonry(coming to browsers) - Overlapping elements: place items in the same grid cell intentionally
CSS Grid eliminates the need for most layout hacks. If you're still using floats or complex flexbox nesting for page layout, it's time to switch.
Written by
Elena Marchetti
Editor. Writes about language, localisation, and why the web still reads like it was built in one country.
Related Articles
Advanced Tailwind CSS Patterns
Go beyond utility classes. Learn Tailwind's plugin system, custom design tokens, component extraction, and the patterns that scale to large codebases.
Accessibility in Web Development
Build websites that work for everyone. A practical guide to WCAG compliance, semantic HTML, ARIA, keyboard navigation, and screen reader testing.
How to Contribute to Open Source
Your first open source contribution doesn't have to be a major feature. A practical guide to finding projects, making meaningful contributions, and building your reputation.