The gap property

gap is set on a container and puts equal space between its items — only between them, with no spacing at the edges.

Syntax

gap: <gap>;
gap: <row gap> <column gap>;
gap: 16px;
gap: 8px 24px;
gap: normal;
Initial value
normal
Inherited
no
Applies to
flex, grid and multi-column containers

Values

ValueWhat it does
<length>a fixed gap: px, rem, em
<percentage>a percentage of the container's size
normalthe initial value: 0 in flex and grid, 1em in multi-column text
<row-gap> <column-gap>two values: the gap between rows first, then between columns

Example

.flex {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
}
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px 24px;
}
.flex span,
.grid span {
  padding: 12px;
  background: #e0f2fe;
  border: 1px solid #0284c7;
}
<div class="flex">
  <span>flex</span>
  <span>gap: 16px</span>
  <span>3</span>
</div>
<div class="grid">
  <span>grid</span>
  <span>gap: 8px 24px</span>
  <span>3</span>
  <span>4</span>
  <span>5</span>
  <span>6</span>
</div>

Result

▶ Open in HTML Editor

Good to know

  • Unlike margin, gap adds no space before the first or after the last item — there is no need to reset margins on the outer items.
  • gap is a shorthand for row-gap and column-gap. Browsers understand the older grid-gap as an alias.
  • In a wrapping flex container (flex-wrap: wrap) gap also sets the space between lines.

Sources and specification

See also