CSS Grid patterns & layouts
CSS Grid enables complex, responsive layouts with minimal code. From magazine layouts to dashboard grids, grid-based patterns form the foundation of modern web design. These examples show how to build real-world layouts using grid.
Holy Grail layout
CSS
<!-- Classic three-column layout with header and footer -->
<div class="page">
<header>Header</header>
<nav>Navigation</nav>
<main>Main Content</main>
<aside>Sidebar</aside>
<footer>Footer</footer>
</div>
.page {
display: grid;
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
gap: 20px;
min-height: 100vh;
}
header {
grid-column: 1 / -1; /* span full width */
background: #333333;
color: white;
padding: 20px;
}
nav {
grid-row: 2;
grid-column: 1;
background: #f5f5f5;
padding: 20px;
overflow-y: auto;
}
main {
grid-row: 2;
grid-column: 2;
padding: 20px;
}
aside {
grid-row: 2;
grid-column: 3;
background: #f9f9f9;
padding: 20px;
}
footer {
grid-column: 1 / -1;
background: #333333;
color: white;
padding: 20px;
text-align: center;
}
<!-- Responsive: stack on mobile -->
@media (max-width: 768px) {
.page {
grid-template-columns: 1fr;
grid-template-rows: auto auto auto auto auto;
}
nav, aside {
grid-column: 1;
}
nav { grid-row: 2; }
main { grid-row: 3; }
aside { grid-row: 4; }
footer { grid-row: 5; }
}Magazine layout
CSS
<!-- Hero image + featured article + grid of articles -->
<div class="magazine">
<img class="hero" src="hero.jpg">
<article class="featured">Featured Story</article>
<article>Article</article>
<article>Article</article>
<article>Article</article>
</div>
.magazine {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: 300px;
gap: 20px;
padding: 20px;
}
.hero {
grid-column: 1 / -1; /* full width -->
width: 100%;
height: 400px;
object-fit: cover;
}
.featured {
grid-column: 1 / 3; /* spans 2 columns -->
grid-row: 2;
background: white;
padding: 20px;
border: 1px solid #ddd;
}
article {
background: white;
padding: 20px;
border: 1px solid #ddd;
}
<!-- Result: Hero spans width, featured takes 2/3 column 2 -->
[════════ Hero Image ════════════════]
[═════ Featured (2 cols) ═════] [Art 1]
[Art 2] [Art 3] [Art 4]Dashboard grid
CSS
<!-- Dashboard with widgets of varying sizes -->
<div class="dashboard">
<div class="widget">Small</div>
<div class="widget large">Large</div>
<div class="widget">Small</div>
<div class="widget">Small</div>
</div>
.dashboard {
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-auto-rows: 200px;
gap: 15px;
padding: 20px;
}
.widget {
grid-column: span 4; /* 1/3 width -->
background: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.widget.large {
grid-column: span 6; /* 1/2 width -->
grid-row: span 2; /* double height -->
}
<!-- Responsive -->
@media (max-width: 768px) {
.dashboard {
grid-template-columns: repeat(6, 1fr);
}
.widget {
grid-column: span 3; /* 1/2 width on tablet -->
}
.widget.large {
grid-column: span 6; /* full width -->
}
}
@media (max-width: 480px) {
.dashboard {
grid-template-columns: 1fr;
}
.widget, .widget.large {
grid-column: 1;
}
}Asymmetric layout
CSS
<!-- Uneven column widths for different content types -->
<div class="asymmetric">
<aside class="sidebar">Sidebar</aside>
<main class="content">Main</main>
</div>
.asymmetric {
display: grid;
grid-template-columns: 280px 1fr;
gap: 30px;
padding: 40px;
}
.sidebar {
background: #f5f5f5;
padding: 20px;
border-radius: 8px;
height: fit-content; /* sticky-like behavior -->
}
.content {
padding: 20px;
}
<!-- Three-column asymmetric -->
.three-column {
display: grid;
grid-template-columns: 1fr 2fr 1fr; /* 1:2:1 ratio -->
gap: 30px;
}
<!-- Two-column with narrow right -->
.two-column-right {
display: grid;
grid-template-columns: 1fr 300px;
gap: 30px;
}Responsive grid without media queries
CSS
<!-- Automatically adapts columns based on space -->
.responsive {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
padding: 20px;
}
.item {
background: white;
border: 1px solid #eeeeee;
border-radius: 8px;
padding: 20px;
min-height: 200px;
}
<!-- No media queries needed! -->
<!-- 1200px+ → 4 columns
900px → 3 columns
600px → 2 columns
300px → 1 column -->
/* More aggressive: smaller items per column -->
.tight-grid {
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
}
/* More breathing room: larger items -->
.spacious-grid {
grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
}Mixing explicit and auto tracks
CSS
<!-- Fixed sidebar + auto-fitting content columns -->
<div class="mixed">
<aside>Sidebar</aside>
<div class="content-grid">
<article>Article</article>
<!-- many articles -->
</div>
</div>
.mixed {
display: grid;
grid-template-columns: 250px 1fr;
gap: 20px;
}
.content-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
}
<!-- Result: fixed sidebar + responsive content grid -->
/* Header + responsive body -->
.app {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: auto 1fr;
}
.app-header {
grid-column: 1;
background: white;
border-bottom: 1px solid #ddd;
}
.app-body {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
padding: 20px;
}Note
These patterns form the building blocks of modern layouts. Combine them with flexbox for inner component layout. Most modern websites use grid for overall page structure and flexbox for component layout.
Grid complete
You've now learned the fundamentals of CSS Grid. Combined with flexbox, you can build virtually any layout. Next: responsive design techniques that work with these layout systems.