Skip to main content

How to Add Space Between Grid Items with gap in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

gap puts space between the rows and the columns of a grid. It goes on the grid container, next to display: grid, and the browser shrinks the columns to make room.

Syntax​

Syntax
<grid container> {
gap: <row gap> <column gap>;
}
PartWhat it does
<grid container>The element with display: grid.
<row gap>The space between rows, such as 8px.
<column gap>The space between columns. Leave it out to use the same value for both: gap: 16px.

MDN's gap page explains that the space goes only between the rows and columns: there's no gap before the first one or after the last.

The Example​

The example is a menu of six drinks in a grid of three columns. Without a gap, the bordered cells touch. One declaration, gap: 16px, puts 16 pixels between them.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding a rule for the class drinks with display grid, three columns of 1fr and a gap of 16px, a rule giving the paragraphs inside a light grey border and no margin, and a div of class drinks with six drinks.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Gap</title>
<style>
.drinks {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}

.drinks p {
border: 1px solid #d1d1d1;
margin: 0;
}
</style>
</head>
<body>
<div class="drinks">
<p>Coffee</p>
<p>Tea</p>
<p>Juice</p>
<p>Water</p>
<p>Milk</p>
<p>Lemonade</p>
</div>
</body>
</html>

gap: 16px is in the same rule as display: grid. The second rule gives each item a border and removes its own top and bottom space, so the gaps are easy to see.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: six bordered cells in two rows of three, Coffee, Tea and Juice, then Water, Milk and Lemonade, with white space between the cells.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on. Without the gap line, the same grid looks like this:

The same grid in Chromium without gap: the six bordered cells touch each other.

What Happened​

  • There are 16 pixels between the columns and between the rows. In a window 480 pixels wide, each cell was 144 pixels wide, where it was 155 without the gap: the columns got narrower to make room.
  • The grid didn't get wider, and there's no gap before the first column or after the last.

Common Mistakes​

MistakeWhat happensDo this instead
Putting gap on the itemsgap belongs to the container: on the items it did nothing, and the cells still touchedPut it in the rule with display: grid

Troubleshooting​

SymptomLikely causeFix
The cells still touchgap is on the items, not the containerMove it to the container
Rows and columns have the same gap, but you wanted different onesgap has one valueGive two values: rows first, then columns

FAQ​

What about grid-gap?​

It's the older name, and it still works: grid-gap: 16px gave 16-pixel gaps too. MDN's page says browsers accept grid-gap as an alias for gap.

Can rows and columns have different gaps?​

Yes. gap: 8px 24px gave 8 pixels between the rows and 24 pixels between the columns.

Conclusion​

Add gap to the grid container, with one value for the same space everywhere or two values for rows and columns. The columns shrink to make room, and the grid stays the same width.

Additional Resources​