Course contentsShow
← Web Development
Lesson 338 of 2,9826. CSS GridFree lesson

Grid Gaps: gap, row-gap, and column-gap

Add spacing between grid tracks using gap properties without affecting outer margins.

Grid Gaps: gap, row-gap, and column-gap

You'll learn how to add clean spacing between grid tracks without messing with margins or padding.


When you create a grid layout, items naturally sit flush against each other. To add breathing room between them, CSS Grid provides dedicated gap properties that create spacing between tracks (rows and columns) — think of it like the grout lines between tiles.

Why Gap Properties Matter

Before grid gaps, developers used margins on grid items to create spacing. The problem? Margins add space on all sides, including the outer edges, making layouts uneven. Gap properties solve this by adding space only between grid tracks, leaving outer edges clean.

The Three Gap Properties

gap — Sets spacing between both rows and columns in one declaration:

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 20px; /* 20px between all tracks */
}

row-gap — Controls spacing only between rows:

.container {
  row-gap: 30px; /* vertical spacing */
}

column-gap — Controls spacing only between columns:

.container {
  column-gap: 15px; /* horizontal spacing */
}

Using Them Together

You can set different row and column gaps using the gap shorthand:

.container {
  gap: 30px 15px; /* 30px rows, 15px columns */
}

This is equivalent to setting row-gap: 30px and column-gap: 15px separately.

The Analogy

Think of gap like the spacing between words on this page — it creates separation between elements without adding extra space at the beginning or end of the line.

Key Takeaway: Use gap, row-gap, and column-gap to add consistent spacing between grid tracks without affecting outer margins or needing complex calculations.