CSS Reference ยท Flexbox
The gap property
Reference for the CSS gap shorthand: set row and column spacing in grid, flexbox and multi-column layouts, with one or two values, examples and usage notes.
The gap property sets the empty space between items in grid, flex and multi-column layouts. It is a shorthand for row-gap and column-gap. Unlike margins, a gap only appears between neighbours, never on the outer edges of the container, so you don't need hacks to remove the extra space at the end of a row.
With one value, the same gap is used in both directions. With two values, the first is the row gap (space between rows) and the second is the column gap.
.layout { display: grid; gap: 1rem; } .tags { display: flex; flex-wrap: wrap; gap: 0.5rem 1rem; }
Try it
The gray background of the container shows through the gaps, and it does not appear around the outside.
Syntax
gap: <'row-gap'> <'column-gap'>? /* each part: normal | <length-percentage> */
- Initial value:
normal(computes to 0 in grid and flex layout, and to1emin multi-column layout) - Applies to: multi-column containers, flex containers and grid containers
- Inherited: no
- Animatable: yes, as a length or percentage
- Computed value: as specified, with lengths made absolute
Values
| Value | Description |
|---|---|
<length> |
A fixed size such as 16px, 1rem or 2vw. Negative values are not allowed. |
<percentage> |
A percentage of the container's content box size in that axis. |
normal |
The default: no gap in grid and flex layouts, 1em between multi-column columns. |
calc() |
Any math expression that resolves to a length or percentage. |
Usage notes
- Gaps vs margins. Use
gapfor spacing between siblings; reserve margins for space around the whole group. - Wrapping flex. In a flex container the column gap spaces items in a line and the row gap spaces the lines. The row gap only shows once items wrap.
- Track sizes. In a grid, the gap is added to the track sizes;
1frcolumns share the space left after gaps are removed. - Percentages. A percentage row gap can be unpredictable when the container height is
auto; prefer lengths for rows. - Legacy names.
grid-gap,grid-row-gapandgrid-column-gapare old aliases for the same properties; prefer the unprefixed names. - Block layout.
gapdoes nothing in ordinary block flow.
Browser support
Supported in all current browsers for grid, flexbox and multi-column layout.
Related
row-gapโ vertical spacing onlycolumn-gapโ horizontal spacing onlygrid-template-columnsโ defines the tracks the gap separates- Lessons: Grid Tracks, Flex Container