The Ultimate CSS Grid Layout Cheatsheet: A Complete Reference Guide

Introduction: What is CSS Grid Layout?

CSS Grid Layout is a two-dimensional layout system designed for the web that allows developers to create complex responsive web layouts with ease. Unlike earlier layout methods, Grid gives you precise control over both rows and columns simultaneously, enabling sophisticated designs that adapt to different screen sizes.

Why CSS Grid matters:

  • Simplifies complex layouts that were previously difficult to achieve
  • Reduces reliance on nested divs and float-based hacks
  • Enables truly responsive designs with minimal media queries
  • Works seamlessly with Flexbox for comprehensive layout solutions

Core Concepts of CSS Grid

Terminology

  • Grid Container: The element with display: grid applied
  • Grid Items: Direct children of the grid container
  • Grid Lines: The horizontal and vertical dividing lines that form the grid structure
  • Grid Tracks: Rows or columns created between grid lines
  • Grid Cell: The intersection of a row and column
  • Grid Area: Multiple grid cells that form a rectangle
  • Gutters: Spacing between grid tracks (using gap property)

Basic Grid Structure

.container {
  display: grid;
  grid-template-columns: 200px 1fr 1fr;
  grid-template-rows: auto 300px auto;
  gap: 20px;
}

Getting Started with CSS Grid: Step-by-Step

  1. Create a grid container

    .container {
      display: grid;
    }
    
  2. Define your grid structure

    .container {
      grid-template-columns: 1fr 2fr 1fr;
      grid-template-rows: auto 300px auto;
    }
    
  3. Add spacing between tracks

    .container {
      gap: 20px; /* Same as: row-gap: 20px; column-gap: 20px; */
    }
    
  4. Position items on the grid

    .item {
      grid-column: 1 / 3; /* Start at line 1, end at line 3 */
      grid-row: 2 / 3; /* Start at line 2, end at line 3 */
    }
    
  5. Create responsive layouts with media queries

    @media (max-width: 768px) {
      .container {
        grid-template-columns: 1fr;
      }
    }
    

Key Properties and Values

Container Properties

PropertyDescriptionExample Values
displayCreates a grid contextgrid, inline-grid
grid-template-columnsDefines column tracks100px 1fr 2fr, repeat(3, 1fr)
grid-template-rowsDefines row tracksauto 200px auto, repeat(3, minmax(100px, auto))
grid-template-areasDefines named grid areas"header header" "sidebar content" "footer footer"
gapSets spacing between tracks20px, 10px 20px (row-gap column-gap)
justify-contentAligns grid within container (horizontal)start, end, center, space-between, space-around
align-contentAligns grid within container (vertical)start, end, center, space-between, space-around
justify-itemsAligns items within cells (horizontal)start, end, center, stretch
align-itemsAligns items within cells (vertical)start, end, center, stretch

Item Properties

PropertyDescriptionExample Values
grid-columnSets column start/end positions1 / 3, 1 / span 2, 2 / -1
grid-rowSets row start/end positions1 / 3, 1 / span 2, 2 / -1
grid-areaPlaces an item in a named grid areaheader, content, sidebar
justify-selfAligns item within its cell (horizontal)start, end, center, stretch
align-selfAligns item within its cell (vertical)start, end, center, stretch
orderChanges visual order of grid items0 (default), -1, 1, 2, etc.

Special Units and Functions

Unit/FunctionDescriptionExample
frFraction unit, shares available space1fr 2fr 1fr
minmax()Sets minimum and maximum sizeminmax(100px, 1fr)
repeat()Repeats track definitionsrepeat(3, 1fr), repeat(auto-fill, minmax(200px, 1fr))
auto-fillCreates as many tracks as will fitrepeat(auto-fill, minmax(200px, 1fr))
auto-fitLike auto-fill but collapses empty tracksrepeat(auto-fit, minmax(200px, 1fr))

CSS Grid vs. Flexbox: When to Use Each

FeatureCSS GridFlexbox
DimensionTwo-dimensional (rows and columns)One-dimensional (row OR column)
Use CaseComplex page layoutsComponent alignment, navigation bars
DirectionWorks in both directions simultaneouslyWorks in one direction at a time
Item PlacementPrecise placement with line numbers/namesSequential flow with limited control
GapsBuilt-in gap propertiesUses margins (older) or gap (newer)
Auto LayoutAuto-placement algorithm with extensive optionsSimple auto layout along main axis
Browser SupportIE11 needs prefixes, partial supportBetter for older browsers

Common Challenges and Solutions

Challenge: Items Overflowing Grid

Solution:

.container {
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr));
}

Challenge: Creating Responsive Layouts Without Media Queries

Solution:

.container {
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  /* Items will wrap automatically when container width < 300px */
}

Challenge: Grid Lines Shifting Due to Content Size

Solution:

.container {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  /* The minmax(0, 1fr) prevents content from pushing tracks wider */
}

Challenge: Maintaining Aspect Ratio for Grid Items

Solution:

.item {
  aspect-ratio: 16 / 9; /* Modern approach */
}

/* Or for older browsers */
.item::before {
  content: "";
  display: block;
  padding-top: 56.25%; /* 9/16 = 0.5625 */
}

Best Practices and Practical Tips

  • Start mobile-first: Design for small screens first, then enhance for larger ones
  • Combine Grid with Flexbox: Use Grid for page layout, Flexbox for component alignment
  • Name grid lines and areas for more maintainable code:
    .container {  grid-template-columns: [sidebar-start] 1fr [sidebar-end content-start] 3fr [content-end];}
    
  • Use modern CSS features like clamp() with Grid for better responsiveness:
    .container {  grid-template-columns: repeat(auto-fit, minmax(clamp(200px, 25%, 300px), 1fr));}
    
  • Avoid fixed track sizes when possible to improve responsiveness
  • Consider accessibility: Ensure your grid layout maintains a logical order when linearized
  • Use DevTools: Browser DevTools have excellent Grid inspectors to debug layouts

Advanced Grid Techniques

Overlapping Elements

.item1 {
  grid-column: 1 / 3;
  grid-row: 1 / 3;
  z-index: 1;
}

.item2 {
  grid-column: 2 / 4;
  grid-row: 2 / 4;
}

Masonry-like Layouts

/* Simplified masonry with grid (without true masonry behavior) */
.container {
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  grid-auto-rows: 20px;
}

.item {
  grid-row-end: span calc(var(--height) / 20);
}

/* Use JavaScript to calculate and set --height based on content */

Subgrid (Newer Browsers)

.container {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: auto auto;
}

.nested {
  grid-column: 2 / 7;
  display: grid;
  grid-template-columns: subgrid;
  /* Adopts the parent's column tracks */
}

Resources for Further Learning

Browser Support

As of 2025, CSS Grid is supported in all modern browsers. For legacy browsers (IE11), consider using feature detection and fallbacks:

@supports (display: grid) {
  .container {
    display: grid;
    /* Grid properties */
  }
}

@supports not (display: grid) {
  .container {
    display: flex;
    flex-wrap: wrap;
    /* Flexbox fallback */
  }
}
Scroll to Top