CSS gap Property

The gap property controls the spacing between rows and columns in CSS Grid, Flexbox, and multi-column layouts.

It is a shorthand property for row-gap and column-gap. Unlike margins, gaps create spacing between layout tracks or items without adding extra space around the outside edges of the container. The property accepts length values, percentages, and the normal keyword.

gap Syntax

The gap property accepts one or two values. With one value, the same spacing is applied to both rows and columns. With two values, the first sets row-gap and the second sets column-gap.

Basic Syntax

selector {
  gap: 20px;
}

Two-Value Syntax

selector {
  gap: 20px 30px;
}

Percentage Values

selector {
  gap: 5%;
}

Mixed Values

selector {
  gap: 1rem 24px;
}

The normal Keyword

selector {
  gap: normal;
}

CSS-Wide Keywords

.example {
  gap: inherit;
}
.example {
  gap: initial;
}
.example {
  gap: unset;
}
.example {
  gap: revert;
}
.example {
  gap: revert-layer;
}

The property does not accept negative lengths or negative percentages.

Property Values

The gap property accepts the following value types.

Value Description
normal Uses the normal spacing behavior for the layout mode.
<length> Specifies a fixed or relative gap using units such as px, rem, em, or vw.
<percentage> Specifies a gap as a percentage of the container's relevant content-box dimension, subject to layout-specific percentage resolution rules.
inherit Uses the computed gap values of the parent element.
initial Resets both gap properties to their initial value, normal.
unset Resets both non-inherited gap properties to their initial value.
revert Reverts to the value established by an earlier cascade origin.
revert-layer Reverts according to the previous cascade layer.

Property Characteristics

Characteristic Value
Initial value normal for both longhands
Inherited No
Applies to Multi-column, flex, and grid containers
Computed value Each longhand computes to its specified length or percentage, or normal
Animation type By computed value type of each longhand
Negative values Not allowed

The gap shorthand sets both row-gap and column-gap, even when only one value is provided.

How It Works

The browser inserts gutters between rows, columns, or flex items according to the active layout system.

Create a Layout Container

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

Add Spacing

.container {
  gap: 20px;
}

The browser creates 20-pixel gaps between adjacent grid tracks.

Container Edges

Gaps appear between tracks or items, not between the outermost items and the container edges.

Available Space

Gaps participate in layout calculations. They occupy space that would otherwise be available for grid tracks, flex items, or multi-column content.

No Effect on Ordinary Block Layout

.container {
  display: block;
  gap: 20px;
}

The declaration is valid, but it does not create spacing between ordinary block children because the element is not using a layout mode that supports gaps.

Important Consideration

The direction represented by row-gap and column-gap depends on the layout system. Flexbox requires particular attention because the main axis can change.

Single-Value Syntax

A single gap value sets both row-gap and column-gap to the same value.

Example

.grid {
  display: grid;
  gap: 20px;
}

Equivalent Longhand Properties

.grid {
  display: grid;
  row-gap: 20px;
  column-gap: 20px;
}

Using rem Units

.grid {
  display: grid;
  gap: 1.5rem;
}

Using Zero

.grid {
  display: grid;
  gap: 0;
}

Setting gap: 0 removes the explicit gutters between tracks or items.

Important Consideration

Use one value when both directions should have equal spacing. This keeps the declaration concise and easier to maintain.

Two-Value Syntax

Two values allow different spacing for rows and columns.

Basic Example

.grid {
  display: grid;
  gap: 16px 24px;
}

The first value sets row-gap to 16 pixels. The second sets column-gap to 24 pixels.

Equivalent Longhand Properties

.grid {
  display: grid;
  row-gap: 16px;
  column-gap: 24px;
}

Different Units

.grid {
  display: grid;
  gap: 1rem 30px;
}

No Row Gap

.grid {
  display: grid;
  gap: 0 20px;
}

No Column Gap

.grid {
  display: grid;
  gap: 20px 0;
}

Important Consideration

The value order is always row-gap followed by column-gap. In Flexbox, their physical directions depend on the flex direction.

Length Values

Length values provide predictable spacing using absolute or relative CSS units.

Pixels

.grid {
  gap: 20px;
}

Root-Relative Units

.grid {
  gap: 1.25rem;
}

Font-Relative Units

.grid {
  gap: 1em;
}

Viewport Units

.grid {
  gap: 2vw;
}

Calculated Values

.grid {
  gap: calc(1rem + 8px);
}

Clamped Values

.grid {
  gap: clamp(12px, 2vw, 32px);
}

Important Consideration

Relative units such as rem and em are useful for spacing that scales with typography. The clamp() function is useful when gaps should adapt to viewport size without becoming too small or too large.

Percentage Values

Percentage gaps are resolved against the relevant content-box dimension of the container, but the details vary between layout systems.

Basic Example

.grid {
  display: grid;
  gap: 5%;
}

Different Percentages

.grid {
  display: grid;
  gap: 3% 5%;
}

Grid Percentage Gaps

In Grid, percentage gaps are based on the corresponding content-box dimension. When the container's size is indefinite, cyclic percentage gaps resolve to zero for intrinsic size calculations but may resolve against the content box during layout.

Flexbox Percentage Gaps

In Flexbox, percentage gaps are based on the container's corresponding dimension. If that dimension is indefinite, the percentage gap may resolve to zero.

Percentage Column Gaps

.columns {
  column-count: 3;
  gap: 5%;
}

In a multi-column layout, the relevant value is the column gap. Its percentage is based on the container's width.

Important Consideration

Percentage gaps can behave differently in Grid and Flexbox when container dimensions are not definite. Use length values when predictable spacing is more important than proportional spacing.

The normal Value

The normal keyword is the initial value of both row-gap and column-gap.

Basic Example

.container {
  gap: normal;
}

Grid Layout

.grid {
  display: grid;
  gap: normal;
}

In Grid, normal produces a used gap of zero.

Flexbox Layout

.flex {
  display: flex;
  gap: normal;
}

In Flexbox, normal also produces a used gap of zero.

Multi-Column Layout

.columns {
  column-count: 3;
  gap: normal;
}

In multi-column layouts, the normal column gap is generally 1em.

Important Consideration

The normal keyword does not always mean zero. Its used value depends on the layout mode.

Gap in CSS Grid

In CSS Grid, gap creates gutters between adjacent rows and columns.

Basic Grid

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

Different Row and Column Gaps

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px 24px;
}

Responsive Grid

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
  gap: 20px;
}

Grid Gutter Behavior

Grid gaps separate tracks. They do not add spacing between a grid item's outer edge and the container's outer edge.

Spanning Grid Items

A grid item that spans multiple tracks also spans the gaps between those tracks.

Subgrid

A subgrid normally inherits the parent grid's gap values for its subgridded dimensions, but its own gap values can be explicitly overridden.

Important Consideration

Grid gaps participate in track sizing. Increasing the gap reduces the space available for flexible tracks within a fixed-size container.

Gap in Flexbox

In Flexbox, gap creates spacing between flex items and, when wrapping occurs, between flex lines.

Horizontal Flex Layout

.flex {
  display: flex;
  gap: 20px;
}

Wrapping Flex Layout

.flex {
  display: flex;
  flex-wrap: wrap;
  gap: 16px 24px;
}

Row Direction

.flex {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 16px 24px;
}

With flex-direction: row, the first value controls spacing between flex lines, and the second controls spacing between items along each line.

Column Direction

.flex {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  gap: 16px 24px;
}

With flex-direction: column, the first value controls spacing between items along the main axis, and the second controls spacing between flex lines.

Navigation Example

.navigation {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
}

Important Consideration

For a single-line flex container, only the gap between items along the main axis is visible. A gap between flex lines requires multiple lines.

Gap in Multi-Column Layouts

In multi-column layouts, gap controls the spacing between columns.

Basic Multi-Column Layout

.article-columns {
  column-count: 3;
  gap: 30px;
}

Equivalent Longhand

.article-columns {
  column-count: 3;
  column-gap: 30px;
}

Column Width

.article-columns {
  column-width: 240px;
  gap: 24px;
}

Column Rule

.article-columns {
  column-count: 3;
  gap: 30px;
  column-rule: 1px solid #cccccc;
}

The column rule is drawn within the gap between columns.

Two-Value Gap

.article-columns {
  column-count: 3;
  gap: 20px 30px;
}

For an ordinary multi-column container, the second value controls spacing between columns. The first value sets row-gap, which does not create vertical gaps between normal content fragments.

Important Consideration

Unlike Grid and Flexbox, multi-column layout primarily uses the column gap. The normal column gap is generally 1em.

row-gap and column-gap

The gap shorthand combines the row-gap and column-gap properties.

row-gap

.grid {
  row-gap: 20px;
}

The row-gap property controls the gutter between rows in Grid, or the corresponding spacing between flex items or lines according to the flex direction.

column-gap

.grid {
  column-gap: 30px;
}

The column-gap property controls the gutter between columns in Grid and multi-column layout, or the corresponding spacing in Flexbox.

Shorthand Example

.grid {
  gap: 20px 30px;
}

Equivalent Longhand Example

.grid {
  row-gap: 20px;
  column-gap: 30px;
}

Overriding One Direction

.grid {
  gap: 20px;
  column-gap: 40px;
}

The final row gap remains 20 pixels, while the column gap becomes 40 pixels.

Shorthand Order Matters

.grid {
  column-gap: 40px;
  gap: 20px;
}

Here, the later shorthand resets both directions to 20 pixels.

Important Consideration

Use the shorthand when both values belong together. Use individual longhand properties when only one direction needs to change.

Gap vs. Margin

Both gap and margin create spacing, but they affect layout differently.

Using Gap

.items {
  display: flex;
  gap: 20px;
}

Gap creates spacing between adjacent flex items without adding space around the container's outside edges.

Using Margin

.items > * {
  margin-right: 20px;
}

This adds a right margin to every item, including the final item unless additional rules remove it.

Comparison

Feature gap margin
Purpose Spacing between layout items or tracks. Spacing around individual elements.
Outer edges Does not add outer gutters. Can add space at outer edges.
Negative values Not allowed. Allowed.
Ordinary block layout Does not space ordinary block children. Can space block elements.
Flexbox and Grid Supported. Supported.
Individual item control Applies consistently across the layout. Can be set independently per element.

Combining Gap and Margin

.items {
  display: flex;
  gap: 16px;
}
.items .last-item {
  margin-left: auto;
}

Here, gap controls spacing between items while the automatic margin pushes the final item toward the opposite side of the flex container.

Important Consideration

Use gap for consistent spacing between layout items. Use margins when an individual element needs additional or directional spacing.

Responsive Layouts

Gap values can change with viewport size to maintain comfortable spacing across different screen widths.

Media Query Example

.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 24px;
}
@media (max-width: 768px) {
  .grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
  }
}
@media (max-width: 480px) {
  .grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
}

Fluid Gap

.grid {
  gap: clamp(12px, 2vw, 32px);
}

Responsive Flexbox

.navigation {
  display: flex;
  flex-wrap: wrap;
  gap: 12px 20px;
}
@media (max-width: 600px) {
  .navigation {
    gap: 8px 12px;
  }
}

Container Query Example

.component-wrapper {
  container-type: inline-size;
}
.component-grid {
  display: grid;
  gap: 12px;
}
@container (min-width: 600px) {
  .component-grid {
    gap: 24px;
  }
}

Important Consideration

Responsive gaps should support readability and usable spacing without causing unnecessary overflow. Test layouts with different content lengths and viewport sizes.

Alignment and Distribution

The gap property creates a minimum gutter between items or tracks. Alignment and distribution properties can add further spacing.

Gap with justify-content

.flex {
  display: flex;
  gap: 20px;
  justify-content: space-between;
}

The flex items retain the specified gap, while space-between can distribute additional free space between them.

Gap with align-content

.grid {
  display: grid;
  gap: 20px;
  align-content: space-around;
}

Additional available space may be distributed around the grid tracks, making the visible separation larger than the specified gap.

Gap with justify-items

.grid {
  display: grid;
  gap: 20px;
  justify-items: center;
}

The gap separates grid tracks, while justify-items controls the inline-axis alignment of items inside their grid areas.

Important Consideration

If items appear farther apart than the declared gap, inspect alignment and distribution properties before increasing or decreasing the gap.

Animating Gap

Length and percentage gap values can be animated to change the spacing between layout items.

Basic Transition

.grid {
  display: grid;
  gap: 12px;
  transition: gap 300ms ease;
}
.grid:hover {
  gap: 28px;
}

Animating Row and Column Gaps

.grid {
  display: grid;
  gap: 12px 16px;
  transition: row-gap 300ms ease, column-gap 300ms ease;
}
.grid:hover {
  gap: 24px 32px;
}

Keyframe Animation

@keyframes gap-change {
  from {
    gap: 8px;
  }
  to {
    gap: 32px;
  }
}
.animated-grid {
  display: grid;
  animation: gap-change 2s ease-in-out infinite alternate;
}

Respect Reduced Motion

@media (prefers-reduced-motion: reduce) {
  .grid {
    transition: none;
  }
  .animated-grid {
    animation: none;
  }
}

Important Consideration

Animating gaps can cause repeated layout calculations and change the available space for items. Avoid large or continuous gap animations in complex layouts.

gap Example

This example uses a three-column grid with different row and column gaps.

HTML Example

<div class="gap-demo">
  <div class="gap-item">Item 1</div>
  <div class="gap-item">Item 2</div>
  <div class="gap-item">Item 3</div>
  <div class="gap-item">Item 4</div>
  <div class="gap-item">Item 5</div>
  <div class="gap-item">Item 6</div>
</div>

CSS Example

.gap-demo {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 16px 24px;
}
.gap-demo .gap-item {
  padding: 16px 8px;
  color: #ffffff;
  background-color: #2456a6;
  text-align: center;
  border-radius: 4px;
}
@media (max-width: 480px) {
  .gap-demo {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
  }
}
Browser Result
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

The grid uses 16-pixel spacing between rows and 24-pixel spacing between columns. At narrower viewport widths, the layout changes to two columns with a 12-pixel gap in both directions.

Notice that the gaps appear only between grid items. No additional gap is added around the outer edges of the grid container.

Common Mistakes

Common mistakes involve using gap in unsupported layout contexts or misunderstanding the shorthand values.

Using Gap Without Grid or Flexbox

.container {
  gap: 20px;
}

Without a supporting layout mode, the declaration does not create spacing between ordinary block children.

Using Negative Values

.grid {
  gap: -10px;
}

Negative gap values are invalid.

Using Three Values

.grid {
  gap: 10px 20px 30px;
}

The shorthand accepts one or two values, not three.

Reversing the Value Order

.grid {
  gap: 10px 30px;
}

The first value always sets row-gap. The second sets column-gap.

Expecting Outer Spacing

Gap does not add padding around the container. Use padding when spacing is needed between the container's content and its edges.

Confusing Flex Direction

In Flexbox, the physical directions of the two gap values depend on flex-direction.

Assuming Percentage Gaps Always Behave the Same

Percentage resolution differs between Grid, Flexbox, and multi-column layout, especially when container dimensions are indefinite.

Using Gap Instead of Margin for Individual Items

Gap creates consistent gutters. It cannot assign a unique gap to only one particular item.

Ignoring Alignment Properties

Properties such as justify-content: space-between can add spacing beyond the specified gap.

Forgetting Shorthand Overrides

A later gap declaration can override previously declared row-gap and column-gap values.

Important Considerations

Keep the following points in mind when using the gap property:

  • Purpose: Creates gutters between layout rows, columns, or flex items.
  • Shorthand: Combines row-gap and column-gap.
  • Initial value: normal.
  • Inheritance: The property is not inherited.
  • Applies to: Grid, Flexbox, and multi-column containers.
  • One value: Sets both row and column gaps equally.
  • Two values: Sets row gap first and column gap second.
  • Lengths: Supports units such as px, rem, em, and vw.
  • Percentages: Supported, with layout-specific resolution behavior.
  • Negative values: Not allowed.
  • Zero: Removes explicit gutters.
  • normal in Grid: Produces a used gap of zero.
  • normal in Flexbox: Produces a used gap of zero.
  • normal in multi-column layout: Generally produces a column gap of 1em.
  • Grid: Gaps separate tracks and participate in track sizing.
  • Flexbox: Gap direction depends on the main and cross axes.
  • Multi-column: Column gaps separate text columns.
  • Outer edges: Gaps do not create container padding.
  • Margins: Can be used alongside gaps for individual item spacing.
  • Alignment: Distribution properties may add space beyond the declared gap.
  • Responsive design: Media queries, container queries, and clamp() can adjust gaps.
  • Animations: Length and percentage gaps can interpolate.
  • Browser support: Widely supported in modern browsers.

Use gap for consistent spacing between items in Grid and Flexbox, or between columns in multi-column layouts. It is generally simpler and easier to maintain than adding individual margins to every child element.

Browser Support

The CSS gap property has Widely available support in modern browsers, including Chrome, Edge, Firefox, and Safari.

Grid gap support was introduced before Flexbox gap support. Older browser versions may support gap in Grid but not in Flexbox.

Basic Example

.grid {
  display: grid;
  gap: 20px;
}

Flexbox Example

.flex {
  display: flex;
  gap: 16px;
}

Legacy Grid Syntax

Older CSS Grid implementations used grid-gap, grid-row-gap, and grid-column-gap. The modern properties are gap, row-gap, and column-gap.

Browser Compatibility

For current compatibility information, visit Can I Use? and search for gap.

Specifications

The gap property is defined in the CSS Box Alignment Module, which specifies spacing and alignment behavior across layout systems.

CSS Box Alignment Module Level 3: gap

The specification also defines row-gap, column-gap, and their behavior in Grid, Flexbox, and multi-column layouts.

CSS Box Alignment Module Level 3

For additional explanations and examples, see MDN Web Docs.

MDN Web Docs: gap