Starter
Perfect for small projects and experiments.
CSS Grid Flexbox Modern Layouts
CSS Grid and Flexbox are the two most powerful layout systems in modern CSS. They overlap in capabilities, which can make it hard to decide which one to use. This article compares them, shows real, visible examples, and gives practical guidelines for choosing the right tool for each layout problem.
At a glance:
Resize the window to see how the grid adapts from 4 columns to 2 columns on smaller screens.
Code for the gallery:
<div class="grid-gallery">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
<div>Item 6</div>
<div>Item 7</div>
<div>Item 8</div>
</div>
/* CSS */
.grid-gallery {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 0.75rem;
}
.grid-gallery div {
background: linear-gradient(135deg, #6366f1, #ec4899);
border-radius: 8px;
height: 70px;
color: white;
display: flex;
align-items: center;
justify-content: center;
}
@media (max-width: 800px) {
.grid-gallery {
grid-template-columns: repeat(2, 1fr);
}
}
This is where Grid shines: defining named areas and letting the browser place them.
This area expands to fill available space. On smaller screens, the sidebar stacks above or below it.
Code for the layout:
<div class="grid-layout">
<header class="grid-header">Header</header>
<aside class="grid-sidebar">Sidebar</aside>
<main class="grid-main">Main content</main>
<footer class="grid-footer">Footer</footer>
</div>
/* CSS */
.grid-layout {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 0.75rem;
}
.grid-header { grid-area: header; }
.grid-sidebar { grid-area: sidebar; }
.grid-main { grid-area: main; }
.grid-footer { grid-area: footer; }
@media (max-width: 800px) {
.grid-layout {
grid-template-columns: 1fr;
grid-template-rows: auto auto 1fr auto;
grid-template-areas:
"header"
"sidebar"
"main"
"footer";
}
}
On narrow screens, the nav collapses into a vertical stack thanks to a media query.
Code for the navigation:
<nav class="flex-nav">
<div class="flex-nav-logo">MySite</div>
<div class="flex-nav-links">
<a href="#">Home</a>
<a href="#">Features</a>
<a href="#">Pricing</a>
<a href="#">Contact</a>
</div>
</nav>
/* CSS */
.flex-nav {
display: flex;
align-items: center;
justify-content: space-between;
}
.flex-nav-links {
display: flex;
gap: 0.75rem;
}
@media (max-width: 600px) {
.flex-nav {
flex-direction: column;
align-items: flex-start;
gap: 0.5rem;
}
}
Flexbox makes it easy to create rows of cards that wrap naturally as space changes.
Perfect for small projects and experiments.
For growing teams that need more control.
Custom solutions for large organizations.
Code for the card row:
<div class="flex-cards">
<article class="flex-card">...</article>
<article class="flex-card">...</article>
<article class="flex-card">...</article>
</div>
/* CSS */
.flex-cards {
display: flex;
gap: 1rem;
flex-wrap: wrap;
}
.flex-card {
flex: 1 1 180px; /* grow, shrink, basis */
background: #fff;
border-radius: 10px;
padding: 0.75rem;
}
In real projects, you rarely choose only Grid or only Flexbox. A common pattern is:
Grid arranges the plans; Flexbox aligns actions inside each card.
Key idea: Grid defines the columns and spacing between plans, while Flexbox handles the internal alignment of buttons and text.
gap over margins for spacing in both Grid and Flexbox.repeat(auto-fit, minmax(...)) is powerful for responsive layouts.Next article: Speed up page loading with the Speculation Rules API