Providing final CSS Grid and Container Queries layout
The detailed plan led to a pure CSS Holy Grail layout using container queries — no media queries — with a sticky footer and a collapsible sidebar toggled by a hidden checkbox, adapting based on container width. Next, I'll deliver the complete HTML and CSS snippet reflecting this approach.
Below is a single‐page example that uses only CSS Grid + Container Queries (no media queries) to create a classic “Holy Grail” layout:
- Header / Main (with left nav, content, right aside) / Footer
- Sticky footer (layout stretches to full viewport height)
- Sidebar that collapses into a slide‑out drawer on narrow containers
- Right aside drops out when the container gets narrower
- All breakpoints are container‑based (using
@container), not viewport media queries
You can drop this into an .html file and open it directly.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Holy Grail (CSS Grid + Container Queries)</title>
<style>
:root {
color-scheme: light dark;
--bg: #f7f8fa;
--panel: #ffffff;
--line: #e2e5ea;
--ink: #1f2933;
--brand: #334155;
}
* { box-sizing: border-box; }
body {
margin: 0;
font-family: system-ui, sans-serif;
background: var(--bg);
color: var(--ink);
}
/* Outer page grid (3 rows: header, main, footer) */
.page {
min-height: 100vh; /* sticky footer via 1fr in grid rows */
display: grid;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header"
"main"
"footer";
container-type: inline-size; /* enable container queries on this element */
}
header {
grid-area: header;
display: flex;
align-items: center;
gap: 1rem;
padding: 1rem 1.5rem;
background: var(--brand);
color: white;
position: sticky;
top: 0;
z-index: 10;
}
/* Toggle button for small containers */
#nav-toggle { display: none; }
.toggle {
display: none;
cursor: pointer;
font-size: 1.35rem;
user-select: none;
padding: 0.35rem 0.6rem;
border: 1px solid rgba(255,255,255,0.35);
border-radius: 0.25rem;
}
footer {
grid-area: footer;
padding: 1rem 1.5rem;
background: var(--brand);
color: white;
}
main {
grid-area: main;
display: grid;
gap: 1rem;
padding: 1rem 1.5rem;
/* Default (wide) 3-column Holy Grail */
grid-template-columns: 16rem 1fr 14rem;
grid-template-areas: "nav content aside";
align-items: start;
}
nav, article, aside {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 0.4rem;
padding: 1rem;
}
nav { grid-area: nav; }
article { grid-area: content; min-height: 20rem; }
aside { grid-area: aside; }
/* Container Query #1: drop the right aside when narrower */
@container (max-width: 1100px) {
main {
grid-template-columns: 14rem 1fr;
grid-template-areas: "nav content";
}
aside { display: none; }
}
/* Container Query #2: collapse the nav into a slide-out */
@container (max-width: 700px) {
.toggle { display: inline-flex; }
main {
grid-template-columns: 1fr;
grid-template-areas: "content";
}
nav {
position: fixed;
inset: calc(1rem + 3.25rem) auto 1rem 1rem; /* below header */
width: min(16rem, 85cqi);
max-width: 18rem;
transform: translateX(-115%);
transition: transform 0.25s ease, box-shadow 0.25s ease;
z-index: 20;
box-shadow: none;
}
/* Slide nav in when toggle is checked */
#nav-toggle:checked ~ main nav {
transform: translateX(0);
box-shadow: 0 12px 30px rgba(0,0,0,0.18);
}
/* Prevent body scroll jump when nav is open (optional) */
#nav-toggle:checked ~ main::after {
content: "";
position: fixed;
inset: 0;
background: rgba(0,0,0,0.25);
z-index: 15;
}
}
</style>
</head>
<body>
<div class="page">
<header>
<label class="toggle" for="nav-toggle">☰</label>
<h1 style="margin:0;font-size:1.15rem;">Holy Grail Layout</h1>
</header>
<!-- Hidden checkbox drives the collapsible nav on narrow containers -->
<input type="checkbox" id="nav-toggle" aria-label="Toggle navigation" />
<main>
<nav>
<strong>Nav</strong><br/>
<a href="#">Link 1</a><br/>
<a href="#">Link 2</a><br/>
<a href="#">Link 3</a><br/>
<a href="#">Link 4</a><br/>
<a href="#">Link 5</a>
</nav>
<article>
<h2>Main Content</h2>
<p>
This is the main content area. Resize the container to see:
</p>
<ul>
<li>Right aside disappears under ~1100px container width.</li>
<li>Sidebar collapses to a slide-out below ~700px container width.</li>
<li>Footer stays pinned to the bottom when content is short.</li>
</ul>
<p>
Everything is driven by <strong>container queries</strong> on <code>.page</code>, not viewport media queries.
</p>
</article>
<aside>
<strong>Aside</strong>
<p>Supplementary info or ads.</p>
</aside>
</main>
<footer>Sticky footer area</footer>
</div>
</body>
</html>
How it works:
.page is the container (container-type: inline-size), so all @container (…) rules respond to its width.
grid-template-rows: auto 1fr auto gives you a sticky footer (the middle row grows).
- Wide: 3 columns (
nav content aside).
- Medium: 2 columns (
nav content, aside hidden).
- Narrow: 1 column; nav slides out from the left when the toggle is checked. No media queries are used—only container queries.