Home/Utilities/Spacing

Spacing

Margin, padding, and gap utilities - generated from the spacing design token. All values are multiples of 0.5rem (8px) for a consistent, rhythmic layout system.

Spacing scale

ArchCSS uses a 7-step scale. The base unit is $spacer: 1rem (16px). Steps below and above multiply or divide that base.

Token Value Pixels
000px
10.5rem8px
21rem16px
31.5rem24px
42rem32px
52.5rem40px
63rem48px

Margin

Apply margin to any side or axis with a shorthand class. Replace {n} with a token from the scale (0–6).

Class Property Example
.m-{n}margin.m-2 -> margin: 1rem
.mt-{n}margin-top.mt-3 -> margin-top: 1.5rem
.mr-{n}margin-right.mr-1 -> margin-right: 0.5rem
.mb-{n}margin-bottom.mb-4 -> margin-bottom: 2rem
.ml-{n}margin-left.ml-2 -> margin-left: 1rem
.mx-{n}margin-left + margin-right.mx-3 -> margin: 0 1.5rem
.my-{n}margin-top + margin-bottom.my-2 -> margin: 1rem 0
<!-- 1rem margin on all sides -->
<div class="m-2">...</div>

<!-- 1.5rem top margin, 0.5rem bottom margin -->
<p class="mt-3 mb-1">...</p>

<!-- 2rem left and right margin -->
<section class="mx-4">...</section>

Margin auto

Use auto margin utilities to horizontally or vertically center elements within their container.

Preview - centered box

.mx-auto
<!-- Center a block element horizontally -->
<div class="mx-auto" style="width:200px">Centered</div>

<!-- Push all margin to the left (right-align) -->
<div class="ml-auto">Right-aligned</div>

<!-- Push all margin to the right (left-align inside flex) -->
<div class="mr-auto">Left-aligned</div>
Class Property
.m-automargin: auto
.mx-automargin-left: auto; margin-right: auto
.my-automargin-top: auto; margin-bottom: auto
.mt-automargin-top: auto
.mr-automargin-right: auto
.mb-automargin-bottom: auto
.ml-automargin-left: auto

Padding

Apply internal spacing with padding utilities. Same scale and directional shorthand as margin.

Preview - padding scale

p-0
0
p-1
8px
p-2
16px
p-3
24px
p-4
32px
p-5
40px
p-6
48px
<!-- 1.5rem padding on all sides -->
<div class="p-3">...</div>

<!-- 2rem horizontal, 1rem vertical -->
<button class="px-4 py-2">Click me</button>

<!-- 0.5rem top, 1.5rem bottom -->
<header class="pt-1 pb-3">...</header>
Class Property Example
.p-{n}padding.p-2 -> padding: 1rem
.pt-{n}padding-top.pt-3 -> padding-top: 1.5rem
.pr-{n}padding-right.pr-1 -> padding-right: 0.5rem
.pb-{n}padding-bottom.pb-4 -> padding-bottom: 2rem
.pl-{n}padding-left.pl-2 -> padding-left: 1rem
.px-{n}padding-left + padding-right.px-3 -> padding: 0 1.5rem
.py-{n}padding-top + padding-bottom.py-2 -> padding: 1rem 0

Gap

Control the spacing between flex or grid children using gap utilities. Same scale as margin and padding.

Preview - flex gap

.g-1 (0.5rem)

Box A
Box B
Box C

.g-3 (1.5rem)

Box A
Box B
Box C

.g-5 (2.5rem)

Box A
Box B
Box C
<!-- 1rem gap between flex children -->
<div class="flex g-2">
  <div>Box A</div>
  <div>Box B</div>
  <div>Box C</div>
</div>

<!-- 2rem column gap, 1rem row gap in a grid -->
<div class="grid gx-4 gy-2">
  <div>Item</div>
  <div>Item</div>
</div>
Class Property Example
.g-{n}gap.g-2 -> gap: 1rem
.gx-{n}column-gap.gx-3 -> column-gap: 1.5rem
.gy-{n}row-gap.gy-2 -> row-gap: 1rem

Named gap presets

Descriptive gap sizes that reference the configured grid gutter instead of the spacing scale.

<!-- Tight, gutter-size gap -->
<div class="flex normal-gap">
  <div>Box A</div>
  <div>Box B</div>
</div>

<!-- Preset scoped to one axis -->
<div class="grid large-gap-y">
  <div>Item</div>
</div>

<!-- Remove the gap entirely -->
<div class="flex no-gap">
  <div>Box A</div>
  <div>Box B</div>
</div>
Class Value
.narrow-gapgap: 2px
.small-gapgap: gutter / 2
.normal-gapgap: gutter
.large-gapgap: gutter / 1.5
.wide-gapgap: gutter / 2
.no-gapgap: 0

Each preset also ships -x and -y variants for one-axis gaps, e.g. .large-gap-y. Presets use the final $grid-gutter-width value, so they stay in sync when the gutter token changes.

Responsive spacing

All spacing utilities support responsive prefixes. Stack breakpoint variants to adjust spacing at different viewport widths - ArchCSS is mobile-first, so unprefixed classes apply at all sizes.

Example - responsive padding card

px-2 on mobile  |  md:px-4 on tablet  |  lg:py-6 on desktop
<!-- Small padding on mobile, larger on tablet and desktop -->
<section class="px-2 md:px-4 lg:px-6 py-3 lg:py-6">
  <h1>Responsive section</h1>
</section>

<!-- Tight margin on mobile, auto-centered from md up -->
<div class="mx-2 md:mx-auto" style="max-width:720px">
  <p>Centered content</p>
</div>

<!-- Responsive gap in a flex row -->
<div class="flex g-1 md:g-3 lg:g-5">
  <div>Card</div>
  <div>Card</div>
  <div>Card</div>
</div>
Prefix Breakpoint Example
xs:≥ 375pxxs:p-2
sm:≥ 544pxsm:px-3
md:≥ 768pxmd:px-4
lg:≥ 992pxlg:py-6
xl:≥ 1200pxxl:m-5
xxl:≥ 1440pxxxl:g-4
Mobile-first. A class like md:px-4 applies from the md breakpoint (768px) and wider. Write the smallest spacing first, then layer larger values with breakpoint prefixes.