Modern CSS Techniques: Mastering Grid and Flexbox

CSS layout has evolved dramatically. With Grid and Flexbox now having excellent browser support, creating complex layouts is more intuitive than ever. Let's master these essential tools.
Flexbox vs Grid: When to Use Which#
Flexbox is for one-dimensional layouts. Grid is for two-dimensional layouts.
— Rachel Andrew, CSS Grid Expert
Flexbox Fundamentals#
The Flex Container#
.container {
display: flex;
flex-direction: row; /* row | row-reverse | column | column-reverse */
justify-content: center; /* Controls main axis alignment */
align-items: center; /* Controls cross axis alignment */
gap: 1rem; /* Space between items */
}
Main Axis Alignment (justify-content)#
Practical Example: Navigation Bar#
<nav class="navbar">
<a href="/" class="logo">Brand</a>
<ul class="nav-links">
<li><a href="/products">Products</a></li>
<li><a href="/about">About</a></li>
<li><a href="/contact">Contact</a></li>
</ul>
<button class="cta-button">Get Started</button>
</nav>
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
background: var(--color-bg-surface);
}
.nav-links {
display: flex;
gap: 2rem;
list-style: none;
margin: 0;
padding: 0;
}
/* On mobile, stack vertically */
@media (max-width: 768px) {
.navbar {
flex-direction: column;
gap: 1rem;
}
.nav-links {
flex-direction: column;
align-items: center;
}
}
Flex Item Properties#
.item {
flex-grow: 1; /* How much item should grow relative to others */
flex-shrink: 1; /* How much item should shrink relative to others */
flex-basis: auto; /* Initial size before growing/shrinking */
/* Shorthand */
flex: 1 1 auto; /* grow shrink basis */
flex: 1; /* Same as flex: 1 1 0% */
}
CSS Grid Fundamentals#
Defining a Grid#
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
grid-template-rows: auto 1fr auto; /* Header, main, footer */
gap: 1.5rem;
min-height: 100vh;
}
Grid Template Areas#
One of Grid's most powerful features for readable layouts:
.page-layout {
display: grid;
grid-template-areas:
"header header header"
"sidebar main aside"
"footer footer footer";
grid-template-columns: 250px 1fr 200px;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }
Responsive Grid Patterns#
Auto-Fill vs Auto-Fit#
/* Auto-fill: Creates as many columns as fit, even if empty */
.grid-auto-fill {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 1rem;
}
/* Auto-fit: Same, but collapses empty columns */
.grid-auto-fit {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1rem;
}
Card Grid Layout#
<div class="card-grid">
<article class="card">...</article>
<article class="card">...</article>
<article class="card featured">...</article>
<article class="card">...</article>
</div>
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 2rem;
}
/* Featured card spans 2 columns on larger screens */
@media (min-width: 768px) {
.card.featured {
grid-column: span 2;
}
}
.card {
display: flex;
flex-direction: column;
background: var(--color-bg-surface);
border-radius: var(--radius-lg);
overflow: hidden;
}
.card-content {
flex: 1; /* Push footer to bottom */
padding: 1.5rem;
}
.card-footer {
padding: 1rem 1.5rem;
border-top: 1px solid var(--color-border-subtle);
}
Advanced Techniques#
Subgrid#
Subgrid allows nested grids to align with parent grid tracks:
.card-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 2rem;
}
.card {
display: grid;
grid-template-rows: subgrid;
grid-row: span 3; /* Card spans 3 row tracks */
}
Container Queries#
Style elements based on their container's size, not the viewport:
.card-container {
container-type: inline-size;
container-name: card;
}
@container card (min-width: 400px) {
.card {
display: grid;
grid-template-columns: 200px 1fr;
}
.card-image {
grid-row: span 2;
}
}
@container card (max-width: 399px) {
.card {
display: flex;
flex-direction: column;
}
}
CSS Grid for Form Layouts#
.form-grid {
display: grid;
grid-template-columns: max-content 1fr;
gap: 1rem 1.5rem;
align-items: center;
}
.form-grid label {
text-align: right;
}
.form-grid input,
.form-grid textarea,
.form-grid select {
width: 100%;
}
/* Full-width elements */
.form-grid .full-width {
grid-column: 1 / -1;
}
/* Button group at the end */
.form-grid .actions {
grid-column: 2;
display: flex;
gap: 1rem;
justify-content: flex-end;
}
Common Layout Patterns#
Holy Grail Layout#
.holy-grail {
display: grid;
grid-template: auto 1fr auto / auto 1fr auto;
min-height: 100vh;
}
.header { grid-column: 1 / -1; }
.footer { grid-column: 1 / -1; }
.nav { grid-column: 1; }
.main { grid-column: 2; }
.aside { grid-column: 3; }
/* Responsive: stack on mobile */
@media (max-width: 768px) {
.holy-grail {
grid-template-columns: 1fr;
}
.nav, .main, .aside {
grid-column: 1;
}
}
Sticky Footer#
.page {
display: grid;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
/* OR with Flexbox */
.page-flex {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.page-flex main {
flex: 1;
}
Centering (The Ultimate Guide)#
/* Flexbox centering */
.center-flex {
display: flex;
justify-content: center;
align-items: center;
}
/* Grid centering (shortest!) */
.center-grid {
display: grid;
place-items: center;
}
/* Grid centering (alternative) */
.center-grid-alt {
display: grid;
place-content: center;
}
Expert Tips from the Community#
Here's a great tip about CSS Grid from the community:
"Stop using flexbox for everything. Grid isn't just for page layouts—it's perfect for component layouts too. The moment you need alignment in two dimensions, reach for Grid."
— CSS Tricks
Debugging Tools#
Firefox Grid Inspector#
Firefox has the best Grid debugging tools. Enable them in DevTools:
- Open DevTools (F12)
- Click the "grid" badge next to
display: gridelements - Customize overlay colors, line numbers, area names
Chrome Grid Tools#
Chrome's grid overlay shows:
- Grid lines and track sizes
- Gap visualization
- Area names (if defined)
Performance Considerations#
- Avoid layout thrashing: Batch DOM reads and writes
- Use
will-changesparingly: Only for frequently animated properties - Prefer Grid for complex layouts: It's often more performant than nested Flexbox
- Use
contain: layout: Isolate layout calculations for complex components
.isolated-component {
contain: layout style;
}
Browser Support#
Both Flexbox and Grid have excellent browser support:
| Feature | Chrome | Firefox | Safari | Edge |
|---|---|---|---|---|
| Flexbox | 29+ | 28+ | 9+ | 12+ |
| Grid | 57+ | 52+ | 10.1+ | 16+ |
| Subgrid | 117+ | 71+ | 16+ | 117+ |
| Container Queries | 105+ | 110+ | 16+ | 105+ |
Further Learning#
- CSS-Tricks Complete Guide to Flexbox
- CSS-Tricks Complete Guide to Grid
- Grid by Example by Rachel Andrew
- Flexbox Froggy - Learn Flexbox with a game
- Grid Garden - Learn Grid with a game
Mastering Flexbox and Grid opens up endless possibilities for creating beautiful, responsive layouts. Start with simple patterns and gradually incorporate more advanced techniques as you become comfortable.



