CSS

Implementing a Responsive Holy Grail Layout with CSS Flexbox

Create the classic header, footer, main content, and two sidebar layout using CSS Flexbox for robust responsiveness across devices.

body {
    margin: 0;
    font-family: sans-serif;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}
header, footer {
    background-color: #333;
    color: white;
    padding: 1rem;
    text-align: center;
}
.main-container {
    display: flex;
    flex: 1; /* Allows main container to take available space */
    flex-wrap: wrap; /* Allow columns to wrap on smaller screens */
}
.sidebar {
    background-color: #f0f0f0;
    padding: 1rem;
    flex: 0 0 200px; /* Fixed width sidebar */
    order: -1; /* Place left sidebar first in visual order */
}
.main-content {
    flex: 1; /* Main content takes remaining space */
    padding: 1rem;
    background-color: #fff;
    min-width: 0; /* Allow content to shrink */
}
.right-sidebar {
    background-color: #e0e0e0;
    padding: 1rem;
    flex: 0 0 180px; /* Fixed width right sidebar */
}

/* Responsive adjustments */
@media (max-width: 768px) {
    .main-container {
        flex-direction: column; /* Stack columns vertically */
    }
    .sidebar, .right-sidebar {
        flex: 1 1 100%; /* Make sidebars full width */
        order: unset; /* Reset order to natural flow */
    }
}
How it works: This snippet demonstrates how to build the classic "Holy Grail" layout with a header, footer, and three columns (two sidebars and main content) using CSS Flexbox. The `body` is a flex container for the header, main container, and footer. The `.main-container` itself is also a flex container, allowing the sidebars and main content to distribute space. `flex: 1` on `.main-content` ensures it fills available space, while fixed widths are used for sidebars. Media queries handle responsive stacking on smaller screens.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs