Skip to main contentSkip to docs navigation

Add spacing between the direct children of an element using margins, with responsive variants.

@layer utilities
MDN Reference

Overview

Space utilities control the spacing between direct children of an element by applying margin-inline-end or margin-block-end to all children except the last. Unlike gap utilities which require a flex or grid formatting context, space utilities work on any parent element.

Item 1
Item 2
Item 3
HTML
<div class="space-x-medium p-medium bg-evident">
  <div class="d-inline-block p-small bg-main">Item 1</div>
  <div class="d-inline-block p-small bg-main">Item 2</div>
  <div class="d-inline-block p-small bg-main">Item 3</div>
</div>

The selectors generated for space utilities are wrapped in :where() for zero specificity, making them easy to override with additional utilities or custom CSS.

Notation

Space utilities that apply to all breakpoints, from xsmall to 2xlarge, have no breakpoint abbreviation in them. This is because those classes are applied from min-width: 0 and up, and thus are not bound by a media query. The remaining breakpoints, however, do include a breakpoint abbreviation.

The classes are named using the format {property}-{size} for xsmall and .{breakpoint}:{property}-{size} for small, medium, large, xlarge, and 2xlarge.

Where property is one of:

  • space-x — for classes that set horizontal spacing via margin-inline-end
  • space-y — for classes that set vertical spacing via margin-block-end

Where size is one of:

  • zero or 0 — for classes that eliminate spacing by setting it to 0
  • 4xsmall - default token value is 0.0625rem (1px).
  • 3xsmall - default token value is 0.125rem (2px).
  • 2xsmall - default token value is 0.25rem (4px).
  • xsmall - default token value is 0.5rem (8px).
  • medium - default token value is 1rem (16px).
  • large - default token value is 1.25rem (20px).
  • xlarge - default token value is 1.5rem (24px).
  • 2xlarge - default token value is 1.75rem (28px).
  • 3xlarge - default token value is 2rem (32px).
  • 4xlarge - default token value is 2.25rem (36px).
  • 5xlarge - default token value is 2.5rem (40px).
  • 6xlarge - default token value is 3rem (48px).

Horizontal

space-x-* utilities control the horizontal space between children by applying margin-inline-end to all children except the last.

Item 1
Item 2
Item 3
HTML

<div class="space-x-medium p-medium bg-evident">
  <div class="d-inline-block p-small bg-main">Item 1</div>
  <div class="d-inline-block p-small bg-main">Item 2</div>
  <div class="d-inline-block p-small bg-main">Item 3</div>
</div>

Vertical

space-y-* utilities control the vertical space between children by applying margin-block-end to all children except the last.

Item 1
Item 2
Item 3
HTML

<div class="space-y-medium p-medium bg-evident">
  <div class="p-small bg-main">Item 1</div>
  <div class="p-small bg-main">Item 2</div>
  <div class="p-small bg-main">Item 3</div>
</div>

How it works

Space utilities apply margin-inline-end or margin-block-end to every direct child except the last via a :not(:last-child) child selector. The selector is wrapped in :where() for zero specificity, so individual children can be overridden with standard margin utilities.

CSS
:where(.space-x-medium > :not(:last-child)) {
  margin-inline-end: 1rem;
}
:where(.space-y-medium > :not(:last-child)) {
  margin-block-end: 1rem;
}

CSS

Space utilities are generated via the utilities API.

Reference

The table lists all space utilities and their CSS properties.

ClassProperties
.space-x-zeromargin-inline-end: var(--cx-space-zero);
.space-x-4xsmallmargin-inline-end: var(--cx-space-4xsmall);
.space-x-3xsmallmargin-inline-end: var(--cx-space-3xsmall);
.space-x-2xsmallmargin-inline-end: var(--cx-space-2xsmall);
.space-x-xsmallmargin-inline-end: var(--cx-space-xsmall);
.space-x-smallmargin-inline-end: var(--cx-space-small);
.space-x-mediummargin-inline-end: var(--cx-space-medium);
.space-x-largemargin-inline-end: var(--cx-space-large);
.space-x-xlargemargin-inline-end: var(--cx-space-xlarge);
.space-x-2xlargemargin-inline-end: var(--cx-space-2xlarge);
.space-x-3xlargemargin-inline-end: var(--cx-space-3xlarge);
.space-x-4xlargemargin-inline-end: var(--cx-space-4xlarge);
.space-x-5xlargemargin-inline-end: var(--cx-space-5xlarge);
.space-x-6xlargemargin-inline-end: var(--cx-space-6xlarge);
.space-x-0margin-inline-end: 0;
.space-y-zeromargin-block-end: var(--cx-space-zero);
.space-y-4xsmallmargin-block-end: var(--cx-space-4xsmall);
.space-y-3xsmallmargin-block-end: var(--cx-space-3xsmall);
.space-y-2xsmallmargin-block-end: var(--cx-space-2xsmall);
.space-y-xsmallmargin-block-end: var(--cx-space-xsmall);
.space-y-smallmargin-block-end: var(--cx-space-small);
.space-y-mediummargin-block-end: var(--cx-space-medium);
.space-y-largemargin-block-end: var(--cx-space-large);
.space-y-xlargemargin-block-end: var(--cx-space-xlarge);
.space-y-2xlargemargin-block-end: var(--cx-space-2xlarge);
.space-y-3xlargemargin-block-end: var(--cx-space-3xlarge);
.space-y-4xlargemargin-block-end: var(--cx-space-4xlarge);
.space-y-5xlargemargin-block-end: var(--cx-space-5xlarge);
.space-y-6xlargemargin-block-end: var(--cx-space-6xlarge);
.space-y-0margin-block-end: 0;

Utilities API

Space utilities are declared in scss/utilities/_index.scss using the utilities API.

"space-x": (
  responsive: true,
  property: margin-inline-end,
  class: space-x,
  child-selector: "> :not(:last-child)",
  values: map.merge(map-loop($spaces, varify, "$key", "space"), (0: 0))
),
"space-y": (
  responsive: true,
  property: margin-block-end,
  class: space-y,
  child-selector: "> :not(:last-child)",
  values: map.merge(map-loop($spaces, varify, "$key", "space"), (0: 0))
),