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

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

Adding space between grid tracks without affecting item sizing or requiring margin hacks.

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

You'll learn how to add clean, consistent spacing between grid rows and columns without messy workarounds.

Why Grid Gaps Matter

Remember when you had to use margins on grid items to create space between them? That approach caused headaches—margins would add up on container edges, requiring negative margins or extra calculations. Grid gaps solve this elegantly by adding space between tracks only, leaving the container edges clean.

Think of grid gaps like the grout lines between tiles: they create visual separation without affecting the size of the tiles themselves.

The gap Properties

gap is the shorthand property that sets spacing between both rows and columns simultaneously:

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

row-gap controls vertical spacing between rows only:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  row-gap: 30px; /* space between rows */
}

column-gap controls horizontal spacing between columns only:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  column-gap: 15px; /* space between columns */
}

Combining Row and Column Gaps

Use the gap shorthand with two values: gap: [row-gap] [column-gap]:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 40px 20px; /* 40px between rows, 20px between columns */
}

The gaps don't affect the fr unit calculations—they're added after the available space is distributed.

Key Takeaway: Use gap, row-gap, and column-gap to add clean spacing between grid tracks without affecting item sizes or needing margin hacks on individual items.