CSS propertyFlexbox

gap

Sets the spacing between items in a flex, grid, or multi-column container.

Initial value
normal
Inherited
No
Animatable
yes, as a length or percentage
Applies to
flex, grid, and multi-column containers
JavaScript
element.style.gap = "12px"
On this page

Example

#
CSS
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
Preview

Definition and usage

#

The gap property adds space only between items, never at the edges of the container. It’s a shorthand for row-gap (the gap between rows) and column-gap (between columns). One value sets both, two values set rows and columns separately.

Before gap worked in Flexbox, spacing was done with margins, and you had to remove the margin on the last item. Today one line handles it, including when items wrap onto several lines.

The value can be a length or a percentage. Gaps don’t take a color; for dividing lines, use borders on the items or the container’s background.

Syntax

#
Syntax
gap: <row-gap> <column-gap>?;

Values

#
  • 0

    Items touch each other.
  • 12px

    An equal gap vertically and horizontally.
  • 24px 6px

    rows columns

    A big gap between lines, a small one between items within a line.

Values in practice

#

0

Items touch each other.
.demo { gap: 0; }

12px

An equal gap vertically and horizontally.
.demo { gap: 12px; }

24px 6px

A big gap between lines, a small one between items within a line.
.demo { gap: 24px 6px; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Instead of margins on list items, set gap on the container. You won’t have to worry about the first and last item.
  • Set the spacing from the container’s edge with the container’s padding; gap doesn’t add it.

Related entries

#

Spotted a mistake or missing an example? Write to us.