Skip to main contentSkip to docs navigation

Responsive utilities for controlling gap, row-gap, and column-gap on flex and grid containers using the Chassis spacing scale.

@layer utilities
MDN Reference
Scoped tokens

Overview

Gap utilities apply the CSS gap, row-gap, and column-gap properties to flex and grid containers, distributing space between items without adding margin to individual children. All three utilities are responsive and share the same $spaces spacing scale used by margin and padding.

Notation

Gap classes that apply at all breakpoints have no breakpoint prefix — they start from min-width: 0 and are not media-query-bound. Breakpoint-prefixed classes apply at that breakpoint and above.

The classes are named using the format {property}-{size} for xs and {breakpoint}:{property}-{size} for sm, md, lg, xl, and 2xl.

Where property is one of:

  • gap — sets gap (both row and column gap simultaneously)
  • row-gap — sets row-gap
  • column-gap — sets column-gap

Where size is one of:

  • zero or 0 — eliminates the gap by setting it to 0
  • 4xs — default token value is 0.0625rem (1px).
  • 3xs — default token value is 0.125rem (2px).
  • 2xs — default token value is 0.25rem (4px).
  • xs — default token value is 0.5rem (8px).
  • md — default token value is 1rem (16px).
  • lg — default token value is 1.25rem (20px).
  • xl — default token value is 1.5rem (24px).
  • 2xl — default token value is 1.75rem (28px).
  • 3xl — default token value is 2rem (32px).
  • 4xl — default token value is 2.25rem (36px).
  • 5xl — default token value is 2.5rem (40px).
  • 6xl — default token value is 3rem (48px).

There is no .gap-auto class — auto has no defined behavior for gap in the CSS specification.

gap

.gap-{size} sets gap on a flex or grid container, controlling spacing between all items in both directions at once.

Item 1
Item 2
Item 3
Item 4
HTML
<div class="grid gap-md">
  <div class="g-col-6 p-xs">Item 1</div>
  <div class="g-col-6 p-xs">Item 2</div>
  <div class="g-col-6 p-xs">Item 3</div>
  <div class="g-col-6 p-xs">Item 4</div>
</div>

row-gap

.row-gap-{size} controls vertical spacing between rows in a grid or flex container without affecting column spacing.

Item 1
Item 2
Item 3
Item 4
HTML
<div class="grid gap-0 row-gap-md">
  <div class="g-col-6 p-xs">Item 1</div>
  <div class="g-col-6 p-xs">Item 2</div>
  <div class="g-col-6 p-xs">Item 3</div>
  <div class="g-col-6 p-xs">Item 4</div>
</div>

column-gap

.column-gap-{size} controls horizontal spacing between columns in a grid or flex container without affecting row spacing.

Item 1
Item 2
Item 3
Item 4
HTML
<div class="grid gap-0 column-gap-md">
  <div class="g-col-6 p-xs">Item 1</div>
  <div class="g-col-6 p-xs">Item 2</div>
  <div class="g-col-6 p-xs">Item 3</div>
  <div class="g-col-6 p-xs">Item 4</div>
</div>

CSS

Gap utilities are generated via the utilities API in scss/utilities/_gap.scss.

Reference

Each class name, its CSS property, and the generated values appear in the reference table.

ClassProperties
.gap-zerogap: var(--cx-space-zero);
.gap-4xsgap: var(--cx-space-4xs);
.gap-3xsgap: var(--cx-space-3xs);
.gap-2xsgap: var(--cx-space-2xs);
.gap-xsgap: var(--cx-space-xs);
.gap-smgap: var(--cx-space-sm);
.gap-mdgap: var(--cx-space-md);
.gap-lggap: var(--cx-space-lg);
.gap-xlgap: var(--cx-space-xl);
.gap-2xlgap: var(--cx-space-2xl);
.gap-3xlgap: var(--cx-space-3xl);
.gap-4xlgap: var(--cx-space-4xl);
.gap-5xlgap: var(--cx-space-5xl);
.gap-6xlgap: var(--cx-space-6xl);
.gap-0gap: 0;
.row-gap-zerorow-gap: var(--cx-space-zero);
.row-gap-4xsrow-gap: var(--cx-space-4xs);
.row-gap-3xsrow-gap: var(--cx-space-3xs);
.row-gap-2xsrow-gap: var(--cx-space-2xs);
.row-gap-xsrow-gap: var(--cx-space-xs);
.row-gap-smrow-gap: var(--cx-space-sm);
.row-gap-mdrow-gap: var(--cx-space-md);
.row-gap-lgrow-gap: var(--cx-space-lg);
.row-gap-xlrow-gap: var(--cx-space-xl);
.row-gap-2xlrow-gap: var(--cx-space-2xl);
.row-gap-3xlrow-gap: var(--cx-space-3xl);
.row-gap-4xlrow-gap: var(--cx-space-4xl);
.row-gap-5xlrow-gap: var(--cx-space-5xl);
.row-gap-6xlrow-gap: var(--cx-space-6xl);
.row-gap-0row-gap: 0;
.column-gap-zerocolumn-gap: var(--cx-space-zero);
.column-gap-4xscolumn-gap: var(--cx-space-4xs);
.column-gap-3xscolumn-gap: var(--cx-space-3xs);
.column-gap-2xscolumn-gap: var(--cx-space-2xs);
.column-gap-xscolumn-gap: var(--cx-space-xs);
.column-gap-smcolumn-gap: var(--cx-space-sm);
.column-gap-mdcolumn-gap: var(--cx-space-md);
.column-gap-lgcolumn-gap: var(--cx-space-lg);
.column-gap-xlcolumn-gap: var(--cx-space-xl);
.column-gap-2xlcolumn-gap: var(--cx-space-2xl);
.column-gap-3xlcolumn-gap: var(--cx-space-3xl);
.column-gap-4xlcolumn-gap: var(--cx-space-4xl);
.column-gap-5xlcolumn-gap: var(--cx-space-5xl);
.column-gap-6xlcolumn-gap: var(--cx-space-6xl);
.column-gap-0column-gap: 0;

Sass maps

Gap utilities are generated from Sass maps in scss/maps/_spacing.scss.

$spaces: (
  zero:           $space-zero,
  4xs:            $space-4xsmall,
  3xs:            $space-3xsmall,
  2xs:            $space-2xsmall,
  xs:             $space-xsmall,
  sm:             $space-small,
  md:             $space-medium,
  lg:             $space-large,
  xl:             $space-xlarge,
  2xl:            $space-2xlarge,
  3xl:            $space-3xlarge,
  4xl:            $space-4xlarge,
  5xl:            $space-5xlarge,
  6xl:            $space-6xlarge,
);

$-gap-values is derived from $spaces in scss/utilities/_gap.scss, resolving each spacing key to its :root custom property via var(--space-*), plus 0.

// Build a gap values map from $spaces. Each key maps to its :root spacing variable, plus 0.
$-gap-values: map.merge(map-loop($spaces, varify, "$key", "space"), (0: 0));

Utilities API

Gap utilities are declared in scss/utilities/_gap.scss using the utilities API.

"gap": (
  responsive: true,
  property: gap,
  class: gap,
  values: $-gap-values
),
"row-gap": (
  responsive: true,
  property: row-gap,
  class: row-gap,
  values: $-gap-values
),
"column-gap": (
  responsive: true,
  property: column-gap,
  class: column-gap,
  values: $-gap-values
),