CSS Grid Layout: Building Complex Layouts Made Simple

CSS Grid Layout: Building Complex Layouts Made Simple
CSS Grid is a powerful two-dimensional layout system that makes creating complex web layouts easier than ever before. Let’s explore its capabilities with practical examples.
Basic Grid Setup
Start with a simple grid container:
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
gap: 20px;
padding: 20px;
}
.item {
background: #3498db;
padding: 20px;
color: white;
border-radius: 8px;
}<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
<div class="item">Item 6</div>
</div>Dashboard Layout
Create a typical dashboard layout with named grid areas:
.dashboard {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: 60px 1fr 50px;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
gap: 0;
}
.header {
grid-area: header;
background: #2c3e50;
color: white;
padding: 0 20px;
display: flex;
align-items: center;
}
.sidebar {
grid-area: sidebar;
background: #34495e;
color: white;
padding: 20px;
}
.main {
grid-area: main;
background: #ecf0f1;
padding: 20px;
}
.footer {
grid-area: footer;
background: #2c3e50;
color: white;
display: flex;
align-items: center;
justify-content: center;
}
/* Responsive adjustments */
@media (max-width: 768px) {
.dashboard {
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}
}<div class="dashboard">
<header class="header">
<h1>Dashboard</h1>
</header>
<aside class="sidebar">
<nav>Navigation</nav>
</aside>
<main class="main">
<h2>Main Content</h2>
</main>
<footer class="footer">
<p>© 2025 My App</p>
</footer>
</div>Card Grid with Spanning
Create a magazine-style layout with spanning items:
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
grid-auto-rows: 200px;
gap: 15px;
padding: 20px;
}
.card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 10px;
padding: 20px;
color: white;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.2rem;
font-weight: bold;
}
.card-large {
grid-column: span 2;
grid-row: span 2;
}
.card-wide {
grid-column: span 2;
}
.card-tall {
grid-row: span 2;
}<div class="gallery">
<div class="card card-large">Featured</div>
<div class="card">Card 1</div>
<div class="card card-tall">Card 2</div>
<div class="card">Card 3</div>
<div class="card card-wide">Card 4</div>
<div class="card">Card 5</div>
<div class="card">Card 6</div>
</div>Responsive Grid with minmax()
Create fluid grids that adapt to screen size:
.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 20px;
padding: 20px;
}
.product-card {
background: white;
border: 1px solid #ddd;
border-radius: 8px;
overflow: hidden;
transition: transform 0.3s ease;
}
.product-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}
.product-image {
width: 100%;
height: 200px;
background: #f5f5f5;
display: flex;
align-items: center;
justify-content: center;
}
.product-info {
padding: 15px;
}Grid with Explicit Placement
Precisely control item placement:
.chess-board {
display: grid;
grid-template-columns: repeat(8, 60px);
grid-template-rows: repeat(8, 60px);
gap: 0;
border: 2px solid #333;
}
.square {
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
}
.square:nth-child(odd) {
background: #f0d9b5;
}
.square:nth-child(even) {
background: #b58863;
}
/* Place specific pieces */
.piece-king {
grid-column: 5;
grid-row: 1;
}Advanced: Nested Grids
Grids can be nested for complex layouts:
.page-layout {
display: grid;
grid-template-columns: 1fr 3fr;
gap: 20px;
}
.content-area {
display: grid;
grid-template-rows: auto 1fr;
gap: 20px;
}
.article-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 15px;
}Grid Alignment Properties
Fine-tune item positioning:
.aligned-grid {
display: grid;
grid-template-columns: repeat(3, 150px);
grid-template-rows: repeat(2, 150px);
gap: 20px;
/* Align all items */
justify-items: center; /* horizontal */
align-items: center; /* vertical */
/* Align the grid itself */
justify-content: center;
align-content: center;
height: 500px;
}
.aligned-grid .item:first-child {
/* Override for individual items */
justify-self: start;
align-self: start;
}Conclusion
CSS Grid revolutionizes web layouts by providing intuitive two-dimensional control. Whether you’re building dashboards, galleries, or complex page layouts, Grid makes it simpler and more maintainable than traditional methods.