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.

css snippetcss
.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.
css snippetcss
/* 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.

css snippetcss
.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.

© 2026 Suleman Ahmed. All rights reserved.

Built with Next.js, Tailwind CSS & Sanity.