CSS Holy Grail Flexbox

The holy grail layout with Flexbox: sidebars and content wrap onto their own rows without media queries.

  • holy grail
  • flex-wrap
  • header
  • sidebar
  • no media query
HTML
<div class="hg-flex">
  <header>Header</header>
  <div class="hg-flex-body">
    <nav>Nav</nav>
    <main>Content</main>
    <aside>Aside</aside>
  </div>
  <footer>Footer</footer>
</div>
CSS
.hg-flex {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 100vh;
}

.hg-flex-body {
  display: flex;
  flex: 1;
  flex-wrap: wrap;
  gap: 16px;
}

/* When the row is narrower than 160 + 480 + 160px, items wrap */
.hg-flex-body nav,
.hg-flex-body aside {
  flex: 1 1 160px;
}

.hg-flex-body main {
  flex: 4 1 480px;
}

About this CSS Holy Grail Flexbox

This CSS Holy Grail Flexbox is 23 lines of pure CSS (0.3 KB) with no JavaScript and no dependencies. It uses Flexbox. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.

CSS features used

How to use this holy grail flexbox

  1. Copy the HTML and paste it where you want the element to appear.
  2. Copy the CSS into your stylesheet, or inside a <style> tag.
  3. Keep the .hg-flex class name, or rename it in both places. Then tweak colors, sizes and timing to match your design.
  4. Using React or Next.js? Change class to className in the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).

Building with an AI tool?

“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.

Feedback