The grid-gap property sets the space between rows and columns in a CSS grid layout using one shorthand declaration.
01
Row + col
Both gaps.
02
0 default
No space.
03
One value
Same both.
04
Two values
Row then col.
05
gap alias
Modern name.
06
Container
Grid parent.
Fundamentals
Introduction
The grid-gap property in CSS is used to define the space between grid items in a CSS Grid layout. This property is a shorthand for the grid-row-gap and grid-column-gap properties, allowing you to set the gaps between rows and columns simultaneously. It helps in creating well-spaced and visually appealing grid layouts.
Definition and Usage
Set grid-gap on a grid container with display: grid. One value applies the same spacing to rows and columns. Two values let you set row gap first, then column gap. For new projects, the modern gap property is preferred, but grid-gap behaves the same in grid layouts.
💡
Beginner Tip
Gap adds space only between items, not on the outer edges of the grid. You do not need margins on each child to create gutters.
Foundation
📝 Syntax
The syntax for the grid-gap property is straightforward and can accept one or two values:
syntax.css
.container{grid-gap:row-gapcolumn-gap;}
If one value is specified, it applies to both rows and columns.
If two values are specified, the first value applies to the row gap, and the second value applies to the column gap.
1rem gap grows or shrinks when users change their default font size, keeping spacing accessible.
A11y
♿ Accessibility
Use adequate gap so touch targets are not too close together on mobile.
Prefer rem units so spacing scales with user font preferences.
Do not rely on gap alone for visual grouping; use headings and landmarks.
Test zoom levels to ensure gaps remain readable at 200% zoom.
Keep contrast clear between items and background even with spacing.
🧠 How grid-gap Works
1
Grid tracks are defined
grid-template-columns and rows create the layout tracks.
Template
2
grid-gap is set
You choose row and column spacing with one or two values.
Gap
3
Browser inserts gutters
Empty space appears between tracks, not on the outer container edges.
Spacing
=
▦
Clean grid spacing
Items stay evenly separated without margin hacks on each child.
Compatibility
🖥 Browser Compatibility
The grid-gap property is supported in most modern browsers, including recent versions of Chrome, Firefox, Safari, Edge, and Opera.
✓ Baseline · Modern browsers
grid-gap everywhere
Grid gap spacing works in all major modern grid implementations.
97%Modern browser support
Google Chrome57+ · Desktop & Mobile
Full support
Mozilla Firefox52+ · Desktop & Mobile
Full support
Apple Safari10.1+ · macOS & iOS
Full support
Microsoft Edge16+ · 79+ Chromium
Full support
Opera44+
Full support
grid-gap property97% supported
Bottom line: Safe for modern projects. Prefer gap in new code; both work in grid layouts.
Wrap Up
🎉 Conclusion
The grid-gap property is an essential tool for web developers working with CSS Grid layouts. It simplifies the process of spacing grid items, enhancing the overall design and readability of your grid layouts.
Experiment with different gap values to achieve the desired look and feel for your web projects. For new code, consider using the modern gap property, which works the same way in grid layouts.