CSS Grid Gaps
In this page:
selector {
gap: row-gap column-gap;
row-gap: length;
column-gap: length;
}
Why Gaps Beat Margins in a Grid
Adding margin to grid items pushes the whole grid wider or taller and creates uneven spacing at the container edges; row-gap and column-gap instead insert space only between tracks, leaving the container edges flush.
- Accepted values:
- gap — one or two values, row then column
- row-gap — length or percentage
- column-gap — length or percentage
- normal — default (behaves as 0 in grid and flex)
- note: grid-gap, grid-row-gap and grid-column-gap are legacy aliases
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Why Gaps Beat Margins in a Grid
<style>
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
background: #eef;
padding: 10px;
}
.container div {
background: seagreen;
color: white;
padding: 10px;
}
</style>
<div class="container">
<div>Flush edges</div>
<div>Even gaps</div>
<div>No margin needed</div>
</div>
column-gap for Horizontal Spacing
column-gap sets the space between columns only, leaving vertical spacing between rows untouched -- useful when you want tight vertical rhythm but breathing room between side-by-side columns.
- Accepted values:
- column-gap: <length> — e.g. 20px
- column-gap: <percentage> — relative to the container
- column-gap: normal — default, 0
- grid-column-gap — legacy alias
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: column-gap for Horizontal Spacing
<style>
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
column-gap: 30px;
background: #eef;
padding: 10px;
}
.container div {
background: seagreen;
color: white;
padding: 10px;
}
</style>
<div class="container">
<div>1</div><div>2</div><div>3</div>
</div>
row-gap for Vertical Spacing
row-gap is the vertical counterpart to column-gap, controlling only the space between rows and leaving horizontal spacing between columns completely untouched. Set it independently when your layout needs generous vertical breathing room but tight horizontal grouping, or vice versa.
- Accepted values:
- row-gap: <length> — e.g. 20px
- row-gap: <percentage> — relative to the container
- row-gap: normal — default, 0
- grid-row-gap — legacy alias
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: row-gap for Vertical Spacing
<style>
.container {
display: grid;
grid-template-columns: repeat(2, 1fr);
row-gap: 30px;
background: #eef;
padding: 10px;
}
.container div {
background: seagreen;
color: white;
padding: 10px;
}
</style>
<div class="container">
<div>1</div><div>2</div><div>3</div><div>4</div>
</div>
The gap Shorthand
gap: 20px 10px sets row-gap to 20px and column-gap to 10px in one declaration, with the row value always listed first; gap: 16px alone applies 16px to both axes at once.
The shorthand is generally preferred over the two longhand properties unless you need to change one axis independently later in your stylesheet.
- Accepted values:
- gap: 20px — same for both
- gap: 10px 30px — row then column
- gap: 2% — percentage
- gap: normal — default
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: The gap Shorthand
<style>
.container {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px 10px;
background: #eef;
padding: 10px;
}
.container div {
background: seagreen;
color: white;
padding: 10px;
}
</style>
<div class="container">
<div>1</div><div>2</div><div>3</div><div>4</div>
</div>
Gaps Interact with minmax and fr Sizing
Gap space is subtracted from the space fr and auto-fit/auto-fill calculations have to work with, so a large gap value on a narrow container can make tracks with minmax() wrap sooner than you might expect -- always test gap and track sizing together.
- Accepted values:
- gap: <length> — subtracted before fr is calculated
- 1fr — shares the space left after gaps
- minmax(<min>, 1fr) — tracks still respect the minimum
- auto-fit — fewer tracks fit when gaps are large
Example: Gaps Interact with minmax and fr Sizing
<style>
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 40px;
background: #eef;
padding: 10px;
}
.container div {
background: seagreen;
color: white;
padding: 16px;
}
</style>
<div class="container">
<div>Large gap can push tracks to wrap sooner</div>
<div>Card</div>
</div>
- Using
marginon items to make gaps, which can create double spacing or uneven edges. - Using the old
grid-gapname only, whengapis the current property. - Expecting
gapto add space around the outside of the grid, when it only applies between tracks.
CSS Grid is supported in Chrome 57+, Firefox 52+, Safari 10.1+ and Edge 16+.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: