CSS Grid gaps: grid-gap, row-gap, and column-gap

In CSS Grid, use row-gap for the space between rows and column-gap for the space between columns. With gap: 16px 24px, the first value is the row gap and the second is the column gap. One value applies to both directions.

Map the old Grid gap names to current properties

Legacy name Current name Role in Grid
grid-row-gap row-gap Space between row tracks
grid-column-gap column-gap Space between column tracks
grid-gap gap Sets both gaps together

grid-row-gap, grid-column-gap, and grid-gap are compatibility aliases. You can replace grid-gap: 16px 24px with gap: 16px 24px and keep the same spacing. See CSS Gaps · Legacy gap properties.

.grid {
  row-gap: 16px;
  column-gap: 24px;
}

/* Same spacing as the two declarations above */
.grid {
  gap: 16px 24px;
}

/* Set both gaps to 20px */
.grid {
  gap: 20px;
}

Choose one of the following rule sets. If you paste them all with the same selector, the final gap: 20px replaces both earlier values. Use 0 to remove a gap. Negative lengths are invalid. See CSS Gaps · gap shorthand.

Compare row and column gaps in a complete example

Save and open the full example as grid-gap.html. It uses no external files or JavaScript. Each grid has six items in three columns. Their only difference is which gaps are set: none, 16px between rows, 24px between columns, or both. The yellow area is the parent’s background.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare Grid row and column gaps</title>
  <style>
    body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
    * { box-sizing: border-box; }
    h1 { font-size: 24px; } h2 { margin: 16px 0 4px; font-size: 18px; }
    .grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr));
      max-width: 420px; padding: 12px; border: 1px solid #9d8455; background: #fff2cf; }
    .item { min-width: 0; min-height: 40px; display: grid; place-items: center;
      background: #176f80; color: white; overflow-wrap: anywhere; }
    .row-only { row-gap: 16px; }
    .column-only { column-gap: 24px; }
    .both { gap: 16px 24px; }
  </style>
</head>
<body>
<h1>Compare Grid gaps</h1>
<h2>No gap</h2>
<div class="grid none">
  <div class="item">A</div>
  <div class="item">B</div>
  <div class="item">C</div>
  <div class="item">D</div>
  <div class="item">E</div>
  <div class="item">F</div>
</div>
<h2>16px row gap</h2>
<div class="grid row-only">
  <div class="item">A</div>
  <div class="item">B</div>
  <div class="item">C</div>
  <div class="item">D</div>
  <div class="item">E</div>
  <div class="item">F</div>
</div>
<h2>24px column gap</h2>
<div class="grid column-only">
  <div class="item">A</div>
  <div class="item">B</div>
  <div class="item">C</div>
  <div class="item">D</div>
  <div class="item">E</div>
  <div class="item">F</div>
</div>
<h2>16px row gap and 24px column gap</h2>
<div class="grid both">
  <div class="item">A</div>
  <div class="item">B</div>
  <div class="item">C</div>
  <div class="item">D</div>
  <div class="item">E</div>
  <div class="item">F</div>
</div>
</body>
</html>

In the second grid, only the two rows are 16px apart. In the third grid there are only column gaps. Each grid has padding: 12px, so even the first grid has space around the edges. When both rows are 40px tall, row-gap: 16px takes the content height from 80px to 96px.

Distinguish gap, padding, and margin and calculate column widths

gap creates space between neighboring tracks. padding creates space between the border and the content. An item’s margin can add to the visible distance between neighbors. A single column has no column gap, and a single row has no row gap. See CSS Grid · Gutters.

At a grid width of 420px, subtract 2px of border and 24px of horizontal padding to get 394px of content width. Two 24px column gaps consume 48px, so three columns divide 346px, or about 115.33px each. Three 33.333% columns plus gaps can overflow. The example’s repeat(3, minmax(0, 1fr)) shares the space left after the gaps.

Change the values and explain wider visible gaps

Change .both to gap: 20px to set both gaps to 20px. Restore the original, then replace it with grid-gap: 16px 24px to compare the same layout. Finally, use gap: 16px 24px followed by column-gap: 8px. The row gap remains 16px, and only the column gap changes to 8px.

Restore the 24px column gap and append the rule below with three 60px columns. The 394px content area has 166px left after the columns and basic gaps use 180px and 48px. space-between adds 83px to each of the two gaps, so the visible distance is 107px. The computed column-gap remains 24px. See CSS Box Alignment · Distributed alignment.

/* Append this to the example’s style element to test only .both */
.both {
  grid-template-columns: repeat(3, 60px);
  justify-content: space-between;
}

Remove the trial rules afterward. If you see no gap, check that it is on the grid container, that the axis has at least two tracks, and that a later gap declaration has not reset that value. If the distance is larger, check item margin and the container’s justify-content or align-content.