🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Grid-gap

AI & DATA SCIENCE // grid-gap

The gap property (historically grid-gap) sets consistent spacing between a grid's rows and columns simultaneously, without adding any extra space around the grid's own outer edges.

Syntax

gap: row-gap column-gap;
gap: all-gaps;

Deep Dive Course

A single gap value applies identically to both row and column gaps, while two values set row-gap first, then column-gap separately — critically, gap only inserts space between grid tracks, it never adds any space at the grid container's own outer edges, meaning a grid with gap: 20px still has its first and last items sitting flush against the container's boundary, with padding on the container itself needed for outer spacing instead. gap is also now supported for flexbox containers in modern browsers, not just grid, providing the same clean, consistent inter-item spacing without needing margin-based workarounds.

1Understanding Grid-gap

A single gap value applies identically to both row and column gaps, while two values set row-gap first, then column-gap separately — critically, gap only inserts space between grid tracks, it never adds any space at the grid container's own outer edges, meaning a grid with gap: 20px still has its first and last items sitting flush against the container's boundary, with padding on the container itself needed for outer spacing instead. gap is also now supported for flexbox containers in modern browsers, not just grid, providing the same clean, consistent inter-item spacing without needing margin-based workarounds.

💡

Use gap instead of adding margin to individual grid items for spacing — gap only affects the space between tracks, never adding unwanted extra space at the grid's own outer edges the way margins on edge items typically would.

editor.html
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}
localhost:3000

2Practical Example

Here is a real-world application of Grid-gap showing how it is used in production CSS code.

editor.html
.grid {
  display: grid;
  gap: 10px 30px;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Grid-gap:

1. Use gap for spacing between grid rows/columns, rather than margins on individual items, avoiding the extra outer-edge spacing margins would introduce on edge items

2. Add padding to the grid container itself, separately from gap, when outer edge spacing around the whole grid is also needed

3. Use gap on flexbox containers too in modern browsers, for the same clean inter-item spacing without needing older margin-based spacing workarounds

⚠️

Tip: Use gap instead of adding margin to individual grid items for spacing — gap only affects the space between tracks, never adding unwanted extra space at the grid's own outer edges the way margins on edge items typically would.

editor.html
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}
localhost:3000

Examples

Example 01Basic Usage
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}
Example 02Advanced Example
.grid {
  display: grid;
  gap: 10px 30px;
}

Best Practices

  • Use gap for spacing between grid rows/columns, rather than margins on individual items, avoiding the extra outer-edge spacing margins would introduce on edge items
  • Add padding to the grid container itself, separately from gap, when outer edge spacing around the whole grid is also needed
  • Use gap on flexbox containers too in modern browsers, for the same clean inter-item spacing without needing older margin-based spacing workarounds

Interview Question

Why does a grid with gap: 20px still have its edge items sitting flush against the grid container's own boundary, with no visible 20px space at the outer edges?

Hint: Think about specifically where gap inserts its spacing — only between adjacent tracks, or also around the outside of the entire grid.

gap is specifically defined to insert its spacing value only in the gutters between adjacent grid tracks, the space that exists between one row and the next, or one column and the next, it has no defined effect at all on the space between the grid's outermost tracks and the container's own boundary edges. This means an item sitting in the very first column, or the very first row, has no preceding track on that particular side for gap to insert space against, so it naturally ends up flush against the container's edge on that side, with gap contributing nothing there since there's no adjacent gap to create in the first place. Achieving actual visible spacing around the entire grid's outer perimeter, not just between its internal tracks, requires separately adding padding to the grid container itself, which is a distinct, independent property from gap and specifically controls that outer boundary spacing instead.

Exercises

MediumPractice using Grid-gap in a real scenario.
View Solution
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

Frequently Asked Questions

Why does a grid with gap: 20px still have its edge items sitting flush against the grid container's own boundary, with no visible 20px space at the outer edges?

gap is specifically defined to insert its spacing value only in the gutters between adjacent grid tracks, the space that exists between one row and the next, or one column and the next, it has no defined effect at all on the space between the grid's outermost tracks and the container's own boundary edges. This means an item sitting in the very first column, or the very first row, has no preceding track on that particular side for gap to insert space against, so it naturally ends up flush against the container's edge on that side, with gap contributing nothing there since there's no adjacent gap to create in the first place. Achieving actual visible spacing around the entire grid's outer perimeter, not just between its internal tracks, requires separately adding padding to the grid container itself, which is a distinct, independent property from gap and specifically controls that outer boundary spacing instead.

Related Functions

Display-gridGrid-template-columnsMargin