October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

CSS gap Property: How to Add Space Between Elements

Use CSS gap on a layout container to set gutters between Grid tracks, Flexbox items or lines, and multi-column boxes. Learn the syntax and debug extra spacing.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use gap on a Grid, Flexbox, or multi-column container to set space between its tracks or items. For example, this gives a three-column grid an even 1rem gutter horizontally and vertically:

.card-list {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 1rem;
}

gap affects the space between siblings, not the space around the container. Its two-value form is row-gap first, then column-gap; in Flexbox, those names map to lines and items according to flex-direction.

What does CSS gap do?

gap is a container-level shorthand for row-gap and column-gap. It sets gutters between Grid tracks, Flexbox items or lines, and multi-column boxes. Put it on the parent layout container, not on the children. See the MDN property reference.

A gap is only between participating tracks or items: it does not create an inset before the first or after the last one, and it does not add padding between a container’s border and its content.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

How do one-value and two-value syntax work?

One value applies equally to both dimensions. Two values set row spacing first and column spacing second:

.card-list {
  gap: 0.75rem 1.25rem; /* row gap, then column gap */
}

That order stays the same regardless of layout mode. The visual direction of those gaps depends on the layout, especially in Flexbox.

How do you use gap in Flexbox?

For a row-direction flex container, column-gap separates items along a line and row-gap separates wrapped lines. Enable wrapping if you want multiple lines:

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1rem; /* line spacing, then item spacing */
}

For a column-direction container, row-gap separates items down the column. A one-value declaration is usually clearest:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
.stack {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

The mapping follows flex-direction, not a permanently horizontal-versus-vertical rule: row and row-reverse use the first value between lines and the second between items; column and column-reverse use the first between items and the second between lines. Flexbox is single-line by default, so line gaps matter only when wrapping creates multiple lines. The MDN gaps guide covers the layout-specific behavior.

How does gap behave in Grid and multi-column layouts?

Grid gutters

In Grid, row and column gaps are gutters between tracks. A spanning item spans the gutter, and the gutter behaves like an empty fixed-size track when sizing the grid. For instance, three 100px columns with a 10px gap have a 320px total track-plus-gutter width when an item spans all three columns. The gaps do not add width outside the first and last tracks. justify-content or align-content can distribute additional free space between tracks, making the visible separation larger than the declared gap.

Multi-column gutters

In multi-column layout, column-gap sets the gutter between adjacent columns. row-gap applies when columns form multiple rows under the relevant multi-column behavior. The documented default column gutter is 1em; outside multi-column layout, normal computes to zero. Set an explicit value when consistent spacing matters. See MDN’s layout gap guide.

Which value should you choose?

  • Length: Use a length such as 0.75rem or 12px when you want predictable spacing.
  • Percentage: A percentage is based on the container’s content box in the relevant dimension. Its behavior during intrinsic sizing varies by layout: cyclic Grid percentages can differ between intrinsic contributions and final layout, and percentage gaps in Flexbox may be zero or ignored during intrinsic sizing before applying after sizing. If the container dimension is indefinite, do not assume percentages behave identically across Grid and Flexbox. Consult the property reference and layout guide for the specific case.
  • normal: This is the default. It computes to zero outside multi-column layout, where the default column gutter is 1em.

The formal syntax also permits non-negative lengths, percentages, and line-width keywords. If you rely on newer syntax or support a defined browser floor, check the reference’s compatibility table rather than assuming every evolving syntax feature is implemented everywhere. MDN marks the core property Baseline and widely available, with browser availability since October 2017.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Why does the visible spacing look larger than gap?

gap sets inter-item or inter-track spacing, but it may not be the only source of visible space. Check these together:

  • The parent’s declared gap; in a two-value declaration, confirm the first value is row gap and the second is column gap.
  • For Flexbox, flex-direction and flex-wrap, which determine what the two dimensions separate.
  • Margins on the children and padding on the container.
  • justify-content and align-content, which can distribute extra free space between items, lines, or tracks. The MDN box alignment overview explains that interaction.
  • Percentage gaps when container dimensions are indefinite.

For new code, use gap, row-gap, and column-gap. Older Grid-era declarations such as grid-gap, grid-row-gap, and grid-column-gap are legacy aliases.

Or skip the browser setup

If you need a screenshot of a page to check how a layout renders, ScreenshotNeo provides a website screenshot API. One GET request returns an image or PDF; the following cURL example saves a WebP screenshot of Stripe. See the ScreenshotNeo documentation for request options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo accepts cookie and consent banners like a visitor and removes 60+ known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be disabled. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, with the result reported in response headers. Its MCP server lets AI agents use screenshot tools. The free plan includes 1,000 shots per month with no card; paid plans start at $5 for 3,000 shots.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Learn about ScreenshotNeo, or sign up for 1,000 free screenshots a month with no card.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.