CSS gap Property

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

What You’ll Learn

The gap property adds consistent space between items in grid and flex layouts without margin on every child.

01

row-gap

Vertical space.

02

column-gap

Horizontal space.

03

Grid

Grid layouts.

04

Flexbox

Flex layouts.

05

px / rem

Length units.

06

Shorthand

One property.

Introduction

The gap property in CSS is a shorthand for setting the row-gap and column-gap properties in grid and flexbox layouts. It defines the space between rows and columns, making it easier to control the layout of your content with consistent spacing.

Definition and Usage

Before gap, developers often used margins on each child to create space between grid or flex items. That approach is harder to maintain and can cause unwanted outer spacing. gap applies spacing only between items, keeping the container edges clean.

💡
Beginner Tip

Put gap on the parent container (display: grid or display: flex), not on the individual items. One value like gap: 1rem sets the same space in both directions.

📝 Syntax

The syntax for the gap property allows you to specify the space for rows and columns in a grid or flex container:

syntax.css
container {
  gap: row-gap column-gap;
}
  • row-gap is the space between rows.
  • column-gap is the space between columns.

If only one value is provided, it applies to both rows and columns.

Basic Example

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

Default Value

The default value of the gap property is normal, which typically results in no space between rows and columns unless otherwise defined.

Syntax Rules

  • Shorthand for row-gap and column-gap.
  • One value sets both axes; two values set row then column.
  • Works on grid containers and flex containers in modern browsers.
  • The property is not inherited by child elements.
  • Gap creates space only between tracks or items, not on the outer edges of the container.

⚡ Quick Reference

QuestionAnswer
Initial valuenormal (0)
Applies toGrid and flex containers
InheritedNo
Shorthand forrow-gap + column-gap
Common patterngap: 1rem;

💎 Property Values

ValueDescription
lengthSpecifies the gap with a specific length (e.g., 10px, 1em, 2rem)
percentageSpecifies the gap as a percentage of the containing element's size (e.g., 5%)
normalThe default value, which usually means no gap

👀 Live Preview

Compare a grid with no gap versus a grid with gap: 0.75rem:

gap: 0;
1
2
3
4
5
6
gap: 0.75rem;
1
2
3
4
5
6

Examples Gallery

Create space between items in a grid container, then explore flexbox gap, separate row/column gaps, and rem-based spacing.

🔢 Grid & Flex

Start with the reference example — a 3-column grid with gap: 20px.

Example 1 — Grid Container with Gap

Use gap: 20px on a grid container with six items.

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

How It Works

The grid container distributes six items into three columns. gap: 20px adds 20 pixels of space between every row and column.

Example 2 — Flexbox with Gap

Space flex items evenly using gap instead of margins on each child.

gap-flex.css
.toolbar {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}
Try It Yourself

How It Works

Flex containers support gap in modern browsers, so toolbar buttons get even spacing without margin-right on each button.

📈 Row & Column Control

Set different vertical and horizontal gaps, or use rem for responsive spacing.

Example 3 — Different Row and Column Gaps

Use two values: first for rows, second for columns.

gap-two-values.css
.card-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 24px 12px;
}
Try It Yourself

How It Works

gap: 24px 12px sets 24px between rows and 12px between columns, useful when you want tighter horizontal spacing.

Example 4 — rem-Based Responsive Gap

Use rem so gap scales with the root font size.

gap-rem.css
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr));
  gap: 1rem;
}
Try It Yourself

How It Works

1rem gap grows proportionally if the user increases their browser default font size, keeping layout spacing accessible.

♿ Accessibility

  • Use sufficient gap between interactive elements so touch targets are easy to tap on mobile.
  • Prefer rem for gap so spacing scales when users change root font size.
  • Do not rely on gap alone for semantic grouping; use headings and landmarks for structure.
  • Test wrapped flex layouts to ensure gap spacing remains readable at narrow widths.
  • Keep contrast on items inside gapped layouts; spacing does not replace color contrast requirements.

🧠 How gap Works

1

You set gap on the container

Apply gap to a grid or flex parent with one or two length values.

Declaration
2

Browser reserves track space

The layout engine inserts fixed gutters between rows and columns or between flex items.

Layout
3

Items sit in remaining space

Children fill the tracks or flex lines without needing individual margins for spacing.

Placement
=

Even, maintainable spacing

Items are separated cleanly with no extra outer margin on the first or last child.

🖥 Browser Compatibility

The gap property is widely supported in modern browsers, including recent versions of Chrome, Firefox, Safari, Edge, and Opera. Grid gap has longer support; flexbox gap was added in later browser versions.

Baseline · Modern browsers

gap in grid and flex

Grid gap is universal in modern engines. Flex gap is supported in current Chrome, Firefox, Safari, and Edge.

96% Modern browser support
Google Chrome 57+ grid · 84+ flex
Supported
Mozilla Firefox 52+ grid · 63+ flex
Supported
Apple Safari 10.1+ grid · 14.1+ flex
Supported
Microsoft Edge 16+ grid · 84+ flex
Supported
Opera 44+ grid · 70+ flex
Supported
gap property 96% supported

Bottom line: Safe for modern projects. Grid gap is nearly universal; flex gap works in all current major browsers.

🎉 Conclusion

The gap property is an essential tool for web developers working with grid and flexbox layouts. By using gap, you can easily control the spacing between rows and columns, creating a clean and well-organized design.

Experiment with different values to see how this property can improve the layout of your web projects. Start with a single value like gap: 1rem, then try separate row and column gaps when you need finer control.

💡 Best Practices

✅ Do

  • Use gap on the container, not children
  • Prefer gap over margin for item spacing
  • Use rem for scalable spacing
  • Try two values for row vs column control
  • Combine with grid and flex layouts

❌ Don’t

  • Add gap on non-grid/flex elements
  • Mix gap with conflicting child margins
  • Assume flex gap in very old browsers
  • Use gap instead of padding inside items
  • Forget to test wrapped flex rows

Key Takeaways

Knowledge Unlocked

Five things to remember about gap

Use these points when spacing layout items.

5
Core concepts
normal 02

Default none

No extra gap.

Default
↕↔ 03

Row + column

Shorthand.

Syntax
grid 04

Grid & flex

Both layouts.

Use case
1rem 05

Length units

px, rem, %.

Values

❓ Frequently Asked Questions

The gap property sets the space between rows and columns in a grid or flex container. It is a shorthand for row-gap and column-gap.
The default value is normal, which means no extra gap is added between items (0 in practice).
gap adds space only between items inside a grid or flex container. margin adds space around an individual element and can affect layout outside the container.
Yes. Modern browsers support gap in flex containers as well as grid containers, making it a convenient way to space flex items without margin hacks.
gap is shorthand that can set both axes at once. row-gap controls vertical spacing between rows; column-gap controls horizontal spacing between columns.

Practice in the Live Editor

Open the HTML editor, change gap values on grid and flex containers, and see how spacing between items updates instantly.

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