← Back to CSS Course | Chapter 11: Grid | Lesson 4 of 10

CSS Grid Gaps

Grid gaps put clean, consistent space between tracks without adding a single extra margin to any item -- and unlike margin, that space never appears on the outer edge of the grid.
Syntax
css
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.

Note:
  • 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

css
<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.

Note:
  • 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

css
<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.

Note:
  • 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

css
<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.

Note:
  • 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

css
<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.

Note:
  • 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

css
<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>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using margin on items to make gaps, which can create double spacing or uneven edges.
  2. Using the old grid-gap name only, when gap is the current property.
  3. Expecting gap to add space around the outside of the grid, when it only applies between tracks.
Browser Support

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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.