Skip to content

CSS Grid Layout: A Practical Mastery Guide

CSS Grid enables two-dimensional layouts that once needed hacks: the grid model, placement rules and the responsive patterns that replace float layouts.

3 min read
CSS Grid layout visualization showing rows, columns, and grid areas

Before CSS Grid, two-dimensional layouts required floats, positioning hacks, or flexbox workarounds. Grid was designed specifically for this: define rows and columns, place items into cells, and let the browser handle the math. It's the first CSS layout system that works in both dimensions simultaneously.

Defining a Grid

A grid container creates a two-dimensional layout context. You define columns and rows on the container; children become grid items.

csscss
/* ❌ Float-based layout — fragile, requires clearfix hacks */
.container::after {
  content: "";
  display: table;
  clear: both;
}
.sidebar {
  float: left;
  width: 25%;
}
.main {
  float: left;
  width: 75%;
}
 
/* ✅ CSS Grid — explicit, maintainable */
.container {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: auto 1fr auto;
  gap: 1rem;
  min-height: 100vh;
}

The 1fr unit distributes available space proportionally. 250px 1fr means a fixed 250px sidebar and a flexible main area that takes the rest.

Grid Template Areas

Named grid areas make complex layouts readable by mapping a visual ASCII representation to the actual layout.

csscss
.dashboard {
  display: grid;
  grid-template-columns: 240px 1fr 1fr;
  grid-template-rows: 60px 1fr auto;
  grid-template-areas:
    "header  header  header"
    "sidebar main    aside"
    "sidebar footer  footer";
  gap: 1rem;
  min-height: 100vh;
}
 
.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.aside   { grid-area: aside; }
.footer  { grid-area: footer; }
htmlhtml
<div class="dashboard">
  <header class="header">Navigation</header>
  <nav class="sidebar">Menu</nav>
  <main class="main">Content</main>
  <aside class="aside">Details</aside>
  <footer class="footer">Status</footer>
</div>

Responsive Grid with minmax and auto-fit

auto-fit with minmax() creates responsive grids without media queries. Items wrap automatically based on available space.

csscss
/* ❌ Fixed column count — breaks on narrow screens */
.card-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 1rem;
}
 
/* ✅ Auto-responsive — cards wrap based on available width */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 1.5rem;
}
/* 
  auto-fit: create as many columns as fit
  minmax(280px, 1fr): each column is at least 280px, at most equal share
  Result: 4 columns on wide screen, 2 on tablet, 1 on mobile — no media queries
*/

Explicit Placement

Grid items can be placed into specific cells using line numbers or span directives.

csscss
/* Place items by grid line numbers */
.featured-article {
  grid-column: 1 / 3;   /* Span from column line 1 to line 3 (2 columns) */
  grid-row: 1 / 2;      /* First row */
}
 
/* Span syntax — more readable for common cases */
.wide-card {
  grid-column: span 2;  /* Span 2 columns from wherever it's placed */
}
 
.tall-card {
  grid-row: span 2;     /* Span 2 rows */
}
csscss
/* Full magazine-style layout */
.magazine-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 200px);
  gap: 1rem;
}
 
.hero-story {
  grid-column: 1 / 3;
  grid-row: 1 / 3;
}
 
.secondary-story {
  grid-column: 3 / 5;
  grid-row: 1 / 2;
}
 
.sidebar-story {
  grid-column: 3 / 4;
  grid-row: 2 / 4;
}

Alignment and Centering

Grid provides alignment properties for both the grid items within their cells and the grid tracks within the container.

csscss
/* Center a single item both horizontally and vertically */
.centered-container {
  display: grid;
  place-items: center;
  min-height: 100vh;
}
 
/* Align items within their grid cells */
.grid {
  display: grid;
  grid-template-columns: repeat(3, 200px);
  justify-items: center;   /* Horizontal alignment of items within cells */
  align-items: start;       /* Vertical alignment of items within cells */
}
 
/* Distribute grid tracks within the container */
.grid-centered {
  display: grid;
  grid-template-columns: repeat(3, 200px);
  justify-content: center;  /* Center the entire grid horizontally */
  align-content: start;     /* Align the grid to the top */
  gap: 2rem;
}

Grid with Media Queries for Complex Layouts

While auto-fit handles many responsive cases, complex layouts sometimes need explicit grid changes at breakpoints.

csscss
.app-layout {
  display: grid;
  gap: 1rem;
 
  /* Mobile: single column stack */
  grid-template-columns: 1fr;
  grid-template-areas:
    "header"
    "main"
    "sidebar"
    "footer";
}
 
@media (min-width: 768px) {
  .app-layout {
    /* Tablet: sidebar becomes a column */
    grid-template-columns: 200px 1fr;
    grid-template-areas:
      "header  header"
      "sidebar main"
      "footer  footer";
  }
}
 
@media (min-width: 1200px) {
  .app-layout {
    /* Desktop: three-column layout */
    grid-template-columns: 240px 1fr 300px;
    grid-template-areas:
      "header  header  header"
      "sidebar main    aside"
      "footer  footer  footer";
  }
}

Key Takeaways

  1. Grid works in two dimensions — define rows and columns simultaneously, place items anywhere in the grid
  2. Named areas create readable layouts — the ASCII art in grid-template-areas maps directly to the visual result
  3. auto-fit with minmax() creates responsive grids without media queries — items wrap automatically based on container width
  4. Use fr units for proportional sizing — 1fr 2fr gives one part and two parts of available space
  5. place-items: center is the simplest centering — one line replaces the flexbox centering pattern
  6. Combine auto-fit for card grids, explicit areas for app layouts — use the right tool for each layout type
Wilfredo Rujel

Wilfredo Rujel

Full Stack Software Engineer

Share this postX