Holy Grail Layout with Flexbox
Implement the classic Holy Grail layout using CSS Flexbox: a header, footer, main content area, and two sidebars, all with proper sizing and responsiveness.
Detailed Explanation
The Holy Grail Layout
The Holy Grail layout is a classic web design pattern consisting of a header, footer, and three columns (left sidebar, main content, right sidebar). For years it was difficult to implement reliably; Flexbox makes it straightforward.
CSS Code
.page {
display: flex;
flex-wrap: wrap;
min-height: 100vh;
}
.header {
flex: 0 0 100%;
}
.nav {
flex: 0 0 200px;
order: 1;
}
.main {
flex: 1;
min-width: 0;
order: 2;
}
.aside {
flex: 0 0 200px;
order: 3;
}
.footer {
flex: 0 0 100%;
order: 4;
}
How It Works
The layout uses a single flex container with flex-wrap: wrap:
- Header and Footer:
flex: 0 0 100%forces them to occupy the full width, creating their own line. - Sidebars:
flex: 0 0 200pxgives each a fixed width of 200px. - Main content:
flex: 1fills all remaining horizontal space. - Order: The
orderproperty controls visual placement independently of HTML source order.
Responsive Adaptation
@media (max-width: 768px) {
.nav, .aside {
flex-basis: 100%;
}
.nav { order: 2; }
.main { order: 3; }
.aside { order: 4; }
}
On small screens, all columns become full-width and stack vertically. The order property rearranges them logically without changing HTML.
Source Order vs Visual Order
A major advantage of the Flexbox approach is that the HTML source order can be optimized for accessibility and SEO (main content first), while the visual order is controlled entirely by CSS. This separation of concerns is one of Flexbox's most powerful features.
Sticky Header Variant
Add position: sticky; top: 0; to the header element for a header that stays visible while scrolling.
Use Case
Use the Holy Grail layout for traditional multi-column websites, blog platforms, e-commerce sites, or any page that needs a header, footer, primary content, and one or two sidebars. It is one of the most requested layout patterns in web development.