Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS Grid + Flexbox Hybrid La | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 47 of 50

·css

CSS Grid + Flexbox Hybrid Layouts

11 mins

Course progress94%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Grid defines **macro‑layout** (rows/columns).
  • Flexbox handles **micro‑alignment** inside each grid cell.
  • Hybrid approach reduces the need for complex media queries.

Grid excels at the overall page skeleton (rows × columns). Flexbox shines at micro‑alignment inside each grid cell. Combining them yields responsive, maintainable layouts.


Example – Blog Layout

<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; }

Why This Works

  • Grid positions the sidebar and main content, independent of the inner layout.
  • Inside .posts, Flexbox handles the wrap and equal‑height columns without needing media queries.
  • Adding a new layout change (e.g., switch sidebar to the bottom on mobile) only requires changing the grid‑template – the flex‑based post cards stay untouched.

Pro Tip – Avoid Grid‑Inside‑Flex When Not Needed

A 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.

Accessibility Considerations

  • Keep DOM order logical. If you move the sidebar below the main content on small screens, you can keep it first in the markup and just reorder via CSS order (Flex/Grid) – screen‑reader users will still encounter the navigation first.

Performance Note

Only one layout calculation is required: the browser solves the grid tracks first, then the flex layout inside each cell – this is highly efficient compared to nested floats.

Testing

  • Resize the viewport; the post cards should wrap from three‑column → two‑column → single‑column automatically.
  • Use Chrome DevTools Elements panel to verify the grid overlay (Cmd+Shift+P → “Show Grid”).

Real‑World Tip – Use CSS Variables for the gap

: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.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonCSS for Dark Mode & ThemingContinue to lesson 48CSS for Performance – Avoiding Layout ThrashBack to CSS Basics to Advanced curriculum