CSS Grid Mastery with Claude Code
Learn about CSS Grid using Claude Code. Practical tips and code examples included.
Build Complex Layouts Concisely With CSS Grid
CSS Grid is the core of modern layout design. With Claude Code, you can generate accurate CSS for complex grid layouts in a short time.
Basic Grid Layout
> Build a dashboard layout with CSS Grid.
> Use four regions: header, sidebar, main content, footer.
/* Dashboard layout */
.dashboard {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: 60px 1fr 40px;
grid-template-areas:
"header header"
"sidebar main"
"sidebar footer";
min-height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
/* Responsive: single column on mobile */
@media (width < 768px) {
.dashboard {
grid-template-columns: 1fr;
grid-template-rows: 60px auto 1fr 40px;
grid-template-areas:
"header"
"sidebar"
"main"
"footer";
}
}
Automatic Layout With auto-fill / auto-fit
/* Card grid: automatically adjusts the column count */
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 1.5rem;
}
/* auto-fit: items stretch when there are few of them */
.stretch-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
}
/* Key difference:
auto-fill -> keeps empty tracks (item widths are fixed)
auto-fit -> collapses empty tracks (items expand) */
Magazine Layout
/* Magazine-style irregular grid */
.magazine {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-auto-rows: 200px;
gap: 1rem;
}
.magazine .featured {
grid-column: span 2;
grid-row: span 2;
}
.magazine .wide {
grid-column: span 2;
}
.magazine .tall {
grid-row: span 2;
}
Using Subgrid
/* Align elements inside cards using subgrid */
.card-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 1.5rem;
}
.card {
display: grid;
grid-template-rows: subgrid;
grid-row: span 3; /* title, body, footer -> 3 rows */
}
.card__title {
align-self: start;
font-size: 1.25rem;
font-weight: bold;
}
.card__body {
align-self: start;
}
.card__footer {
align-self: end;
border-top: 1px solid #eee;
padding-top: 0.5rem;
}
Advanced Layouts With Named Lines
.page {
display: grid;
grid-template-columns:
[full-start] minmax(1rem, 1fr)
[content-start] min(65ch, 100%)
[content-end] minmax(1rem, 1fr)
[full-end];
}
.page > * {
grid-column: content;
}
.page .full-bleed {
grid-column: full;
}
.page .wide {
grid-column: full;
max-width: 90rem;
margin-inline: auto;
padding-inline: 1rem;
}
CSS Grid Animation
/* Grid animation transitions */
.animated-grid {
display: grid;
grid-template-columns: 1fr 1fr;
transition: grid-template-columns 0.3s ease;
}
.animated-grid.collapsed {
grid-template-columns: 0fr 1fr;
}
.animated-grid .sidebar {
overflow: hidden;
min-width: 0;
}
Practical Utility Classes
/* Grid utilities */
.grid-1 { grid-template-columns: 1fr; }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.gap-sm { gap: 0.5rem; }
.gap-md { gap: 1rem; }
.gap-lg { gap: 1.5rem; }
.gap-xl { gap: 2rem; }
.span-2 { grid-column: span 2; }
.span-3 { grid-column: span 3; }
.span-full { grid-column: 1 / -1; }
Summary
Using CSS Grid together with Flexbox lets you handle any layout. With Claude Code, you can accurately implement advanced features like subgrid and named lines. Don’t forget to combine it with media queries for responsive design. For the full spec, see CSS Grid Layout Module Level 2.
Level up your Claude Code workflow
50 battle-tested prompt templates you can copy-paste into Claude Code right now.
Free PDF: Claude Code Cheatsheet in 5 Minutes
Key commands, shortcuts, and prompt examples on a single printable page.
About the Author
Masa
Engineer obsessed with Claude Code. Runs claudecode-lab.com, a 10-language tech media with 2,000+ pages.
Related Posts
Creating Custom Slash Commands in Claude Code — Tailor Your Workflow
Learn how to create custom slash commands in Claude Code. Covers file placement, arguments, and automating frequent tasks with practical code examples.
10 Tips to Triple Your Productivity with Claude Code
Learn about 10 tips to triple your productivity using Claude Code. Practical tips and code examples included.
Canvas/WebGL Optimization with Claude Code
Learn about Canvas/WebGL optimization using Claude Code. Practical tips and code examples included.