Lesson 47 of 50
cssCSS Grid + Flexbox Hybrid Layouts
11 mins
Course progress94%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 47 of 50
css11 mins
Keep these ideas in mind as you work through the lesson.
Grid excels at the overall page skeleton (rows × columns). Flexbox shines at micro‑alignment inside each grid cell. Combining them yields responsive, maintainable layouts.
<div class="page">
<aside class="sidebar">…</aside>
<main class="main">
<section class="posts">
<!-- each post is a flex container -->
<article class="post">
<img src="…" alt="" />
<h2>Title</h2>
<p>Excerpt …</p>
</article>
<!-- repeat -->
</section>
</main>
</div>
.page {
display:grid;
grid-template-columns: 1fr 3fr; /* sidebar + main */
gap:2rem;
}
.sidebar { grid-column:1 / 2; }
.main { grid-column:2 / 3; }
/* Inside the main area – flex for the post list */
.posts { display:flex; flex-wrap:wrap; gap:1.5rem; }
.post { flex:1 1 min(300px, 100%); display:flex; flex-direction:column; }
.post img { width:100%; height:auto; }
.posts, Flexbox handles the wrap and equal‑height columns without needing media queries.display:grid element inside a Flex item cannot affect the height of its sibling Flex items because Flex uses the outer size of the grid container. Use align-self:stretch on the Flex item if you need the columns to match heights.order (Flex/Grid) – screen‑reader users will still encounter the navigation first.:root { --gap:2rem; }
.page { gap:var(--gap); }
.posts { gap:calc(var(--gap) / 2); }
Now adjusting the global gap instantly updates both macro‑ and micro‑layout spacing.