CSS grid-gap Property

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Layout

What You’ll Learn

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.

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.

📝 Syntax

The syntax for the grid-gap property is straightforward and can accept one or two values:

syntax.css
.container {
  grid-gap: row-gap column-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.

Basic Example

grid-gap-basic.css
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 20px;
}
grid-gap: 20px; grid-gap: 12px 24px; gap: 1rem;

Default Value

The default value for the grid-gap property is 0, meaning no gap between grid items.

Syntax Rules

  • Applies to grid containers only, not individual grid items.
  • Shorthand for grid-row-gap and grid-column-gap.
  • Legacy alias of gap in grid layouts.
  • The property is not inherited.
  • Gap appears only between tracks, not on container edges.

⚡ Quick Reference

QuestionAnswer
Initial value0
Applies toGrid containers
InheritedNo
Shorthand forgrid-row-gap + grid-column-gap
Modern equivalentgap

💎 Property Values

ValueDescription
lengthSpecifies the size of the gap using any CSS length unit (e.g., px, em, rem)
percentageSpecifies the size of the gap as a percentage of the grid container’s size

👀 Live Preview

Six items in a three-column grid with grid-gap: 0.75rem:

1
2
3
4
5
6

Examples Gallery

Add 20px gap on all sides, compare with no gap, use two values for asymmetric spacing, and try rem units.

🔢 Uniform Grid Spacing

Start with the reference example — 20px gap between rows and columns.

Example 1 — 20px Grid Gap

Create a grid container with a 20px gap between both rows and columns.

grid-gap-20px.css
.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 20px;
}
Try It Yourself

How It Works

A single 20px value sets both row and column gap, creating even spacing in every direction between items.

Example 2 — No Gap vs Grid Gap

See how gap improves readability compared to items touching each other.

grid-gap-compare.css
.no-gap { grid-gap: 0; }
.with-gap { grid-gap: 16px; }
Try It Yourself

How It Works

Without gap, grid cells sit flush together. Adding grid-gap creates gutters between every row and column.

📈 Flexible Spacing

Use two values or relative units for more control over grid spacing.

Example 3 — Different Row and Column Gap

Set row gap and column gap independently with two values.

grid-gap-two-values.css
.card-grid {
  grid-gap: 12px 24px;
}
Try It Yourself

How It Works

12px 24px means 12px between rows and 24px between columns, useful when horizontal spacing should be wider.

Example 4 — rem for Scalable Gap

Use rem units so grid spacing scales with the root font size.

grid-gap-rem.css
.container {
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 1rem;
}
Try It Yourself

How It Works

1rem gap grows or shrinks when users change their default font size, keeping spacing accessible.

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

🖥 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 Chrome 57+ · Desktop & Mobile
Full support
Mozilla Firefox 52+ · Desktop & Mobile
Full support
Apple Safari 10.1+ · macOS & iOS
Full support
Microsoft Edge 16+ · 79+ Chromium
Full support
Opera 44+
Full support
grid-gap property 97% supported

Bottom line: Safe for modern projects. Prefer gap in new code; both work in grid layouts.

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

💡 Best Practices

✅ Do

  • Set gap on the grid container
  • Use one value when row and column spacing match
  • Use two values for asymmetric spacing
  • Prefer rem for accessible scaling
  • Consider modern gap in new projects

❌ Don’t

  • Apply on grid items instead of containers
  • Use margins when gap solves spacing cleanly
  • Expect gap on container outer edges
  • Use tiny gaps on mobile touch layouts
  • Forget row vs column order in two-value syntax

Key Takeaways

Knowledge Unlocked

Five things to remember about grid-gap

Use these points when spacing grid items.

5
Core concepts
0 02

Default 0

No space.

Default
20px 03

One value

Same both.

Syntax
12 24 04

Two values

Row then col.

Pattern
gap 05

Modern gap

Same effect.

Alias

❓ Frequently Asked Questions

grid-gap is shorthand that sets the space between rows and columns in a CSS grid container.
The default value is 0, which means there is no gap between grid items unless you set one.
grid-gap is the legacy grid-specific name. gap is the modern standard and works on both grid and flex containers.
The first value sets row gap to 10px. The second value sets column gap to 24px.
No. Gap applies only between rows and columns, not between the grid and the container edges.

Practice in the Live Editor

Open the HTML editor, change grid-gap values, and watch row and column spacing update.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful