How to Add Space Between Grid Items with gap in CSS
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
<grid container> {
gap: <row gap> <column gap>;
}
| Part | What 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:

<!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 screenshot comes from Chromium 159, the open-source browser that Chrome is built on. Without the gap line, the same grid looks like this:

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
| Mistake | What happens | Do this instead |
|---|---|---|
Putting gap on the items | gap belongs to the container: on the items it did nothing, and the cells still touched | Put it in the rule with display: grid |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| The cells still touch | gap is on the items, not the container | Move it to the container |
| Rows and columns have the same gap, but you wanted different ones | gap has one value | Give 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
- gap, on MDN.
