Suleman Ahmed - BlogAdvanced CSS Grid Layouts: Beyond the Basics

CSS Grid has revolutionized two-dimensional layouts on the web. While most developers know how to define grid columns and rows, Grid offers advanced features that make complex responsive layouts much easier to manage.
Grid Template Areas
Instead of explicitly setting grid column and row lines, grid template areas let you describe the page layout visually inside your CSS.
.dashboard-container {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: 60px 1fr 40px;
grid-template-areas:
"sidebar header"
"sidebar main"
"sidebar footer";
height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }Auto-Fit vs Auto-Fill
When creating dynamic grids, `auto-fit` and `auto-fill` behave differently when there is extra space in the container:
- Auto-Fill: Creates as many columns as will fit in the container, even if some of them are empty.
- Auto-Fit: Fits the existing items into the grid, stretching them to fill all the available space.
/* Responsive layout without media queries */
.grid-responsive {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}Introducing Subgrid
CSS subgrid allows nested grid items to inherit the grid rows and columns of their parent container. This makes aligning elements across different cards (such as headers and buttons) incredibly easy.
.card-list {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.card {
display: grid;
grid-template-rows: subgrid;
grid-row: span 3; /* Spans 3 parent grid tracks */
}Using these advanced techniques enables cleaner, more readable layouts without nesting extra wrapper divs.
More Articles

A Guide to Effective Unit Testing with Vitest and React Testing Library
Learn how to write reliable tests for your React components. Covers API mocking, event simulations, and clean assertions.

Tailwind CSS v4: The Future of Utility-First CSS
Explore the key improvements in Tailwind CSS v4, featuring native CSS variables, a highly optimized CSS compiler, and simplified config files.

React 19 Server Components: What You Need to Know
Get ready for React 19. Understand how Server Components differ from Client Components, and how they improve page load performance.