sodcalculator

10 Tech How-To

CSS Gap Property: How to Add Space Between Elements

Tag 10-21348 · Updated · Standard formula, shown below

Volume 9.26 yd³
Volume
250.0 ft³
Result
Lawn length
40 ft
You entered
Lawn width
25 ft
You entered
You enter
Lawn length, Lawn width, Depth
You get
Volume, Volume
Rounding
2 dp · 1 dp

The CSS gap property (also known as grid-gap in older browsers) is the simplest way to add space between elements in a flexbox or grid layout. You set it on the container, and it automatically adds consistent spacing between all direct children, eliminating the need for margins on each item.

Formula

The gap property doesn’t use a mathematical formula—it’s a CSS layout property that defines the size of the gutter between rows and columns in a grid or flex container. The syntax is:

gap: <row-gap> <column-gap>;

  • row-gap: the space between rows (vertical spacing in a row-direction flex or grid).
  • column-gap: the space between columns (horizontal spacing in a row-direction flex or grid).

If you specify only one value, it applies to both rows and columns. You can also use the longhand properties row-gap and column-gap individually.

Worked Example

Suppose you have a flex container with three items and you want 20 pixels of space between them. The CSS would be:

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

This adds 20px between item 1 and item 2, and 20px between item 2 and item 3. No margins are needed on the items themselves. The total space between the first and last item is 40px (two gaps), but the space between each pair is exactly 20px.

How the Gap Property Works

The gap property is a shorthand for row-gap and column-gap. In a grid container, it creates space between grid tracks (both rows and columns). In a flex container, it creates space between flex items along the main axis and, if wrapping is enabled, also along the cross axis.

Key points:

  • gap only applies to direct children of the container. Nested elements are unaffected.
  • The property does not add space before the first item or after the last item—only between items.
  • It works with any length unit (px, em, rem, %, vw, etc.) and also accepts calc() expressions.
  • In grid, you can use gap alongside grid-template-columns and grid-template-rows to control both track sizes and spacing.

How to Choose the Right Gap Value

Choosing a gap size depends on your design and the visual rhythm you want. There’s no one-size-fits-all, but here are practical guidelines:

  • Small gaps (4–8px): for dense UI components, button groups, inline icon-text pairs.
  • Medium gaps (12–24px): for card layouts, form fields, and most content sections.
  • Large gaps (32px+): for page-level sections, hero areas, or when you want clear separation.
  • Use relative units (em, rem) for responsive design—they scale with the base font size.
  • Consider using clamp() for fluid gaps: gap: clamp(12px, 2vw, 24px);

Common Problems and Misconceptions

One frequent mistake is expecting gap to work on a container that isn’t flex or grid. It won’t—it has no effect on block or inline layouts. Another is confusing gap with margin: margins collapse in certain situations, but gap never collapses and never adds space outside the container.

Another misconception is that gap adds space around each item equally. In reality, it adds space between items, so the outer edges have no extra space. If you need space on the outer edges, use padding on the container.

Browser support is excellent—all modern browsers support gap in flexbox and grid since 2021. For older browsers, you can use grid-gap as a fallback, or use margins with a negative margin on the container (a common hack).

Comparison with Margins and Other Techniques

Before gap, developers used margins or padding to create spacing. Here’s how they compare:

  • Margins: add space around each element. They can collapse (vertical margins in block layout) and require careful handling of the first/last child (e.g., margin-left: 0 on the first item).
  • Padding on container: adds space inside the container, but doesn’t create space between items unless combined with negative margins or percentages.
  • Justify-content / align-content: distribute space around items, but don’t give you exact control over the gap size.
  • Gap: gives exact, consistent spacing with no collapsing and no extra outer space. It’s the cleanest solution.

Practical Examples and Use Cases

Here are a few real-world scenarios where gap shines:

  • Button groups: display: flex; gap: 10px; on a container of buttons gives even spacing without margin overrides.
  • Card grids: display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 20px; creates a responsive grid with equal spacing.
  • Form layouts: display: grid; gap: 15px; spaces out input fields and labels vertically.
  • Navigation menus: display: flex; gap: 1.5rem; on a nav list gives clean spacing between links.

Frequently Asked Questions

Does gap work with inline elements?

No. The gap property only works on flex or grid containers. For inline elements, you’d need to use margins or padding. If you want to space inline-block elements, you can set the parent to display: flex and then use gap.

Can I use gap with CSS columns?

Yes, but it’s a different property. For CSS multi-column layouts, you use column-gap (which is actually part of the same gap shorthand). The column-gap property works on elements with column-count or column-width set.

What’s the difference between gap and grid-gap?

They are the same thing. grid-gap was the original name for grid layouts, but it was renamed to gap to work with flexbox too. Use gap for new code, and keep grid-gap only if you need to support very old browsers.

How do I center items with gap?

Use justify-content: center and align-items: center on the flex container. The gap property only controls the space between items, not their alignment. The two work together: display: flex; gap: 20px; justify-content: center; centers the group and adds 20px between items.

Can gap be negative?

No. Negative values are not allowed for gap. If you need to overlap items, use negative margins or transform properties instead.

Does gap work with absolutely positioned elements?

No. Absolutely positioned elements are removed from the normal flow, so they are not affected by the parent’s gap. You’d need to position them manually or use margins.

Related

Formula used
in l | Lawn length | ft | 40 | 0 | 100000
in w | Lawn width | ft | 25 | 0 | 100000
in d | Depth | in | 3 | 0 | 120
out yd | Volume | yd³ | l * w * d / 324 | 2
out ft3 | Volume | ft³ | l * w * d / 12 | 1

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More in this bed

The whole Tech How-To bed →