Spacings

Apply margin, padding, gap and sibling-spacing utility classes to elements.

Syntax

Use .m-{size} for margins, .p-{size} for padding, .gap-{size} for flex/grid gaps and .space-x-{size} or .space-y-{size} for direct-child margins. Insert a breakpoint before the size, as in .p-md-3.

HTML
<div class="d-flex flex-column gap-2 p-2 p-md-3">
	<div>Project overview</div>
	<div>Recent activity</div>
</div>

Class and token reference

PatternEffect

.m-{size}, .p-{size}

Margin or padding on all sides.

.m{direction}-{size}, .p{direction}-{size}

Margin or padding on t (top), b (bottom), s (logical start) or e (logical end).

.m{axis}-{size}, .p{axis}-{size}

Margin or padding on the horizontal (x) or vertical (y) axis.

.gap-{size}, .column-gap-{size}, .row-gap-{size}

Both, column or row gaps in flex/grid layouts.

.space-x-{size}, .space-y-{size}

Logical start margins between direct children.

.m-auto, .mx-auto, .ms-auto

Automatic margin alignment, including direction and axis variants.

Use spacing utilities to apply margin or padding to elements.

The default scale provides whole and half steps from 0 through 5. Decimal class suffixes keep their dot, while the corresponding CSS variable uses a hyphen so it remains a valid custom property name.

Class suffixCSS variableValue
0--spacer-00
0.5--spacer-0-50.125rem
1--spacer-10.25rem
1.5--spacer-1-50.375rem
2--spacer-20.5rem
2.5--spacer-2-50.75rem
3--spacer-31rem
3.5--spacer-3-51.25rem
4--spacer-41.5rem
4.5--spacer-4-52.25rem
5--spacer-53rem
p-1
p-1.5
p-2
p-2.5
p-3
p-3.5
HTML
<div class="p-1 bg-primary rounded">p-1</div>
<div class="p-1.5 bg-primary rounded">p-1.5</div>
<div class="p-2 bg-primary rounded">p-2</div>
<div class="p-2.5 bg-primary rounded">p-2.5</div>
<div class="p-3 bg-primary rounded">p-3</div>
<div class="p-3.5 bg-primary rounded">p-3.5</div>

Margin

Apply margin to all sides of an element.

m-1
m-2
m-3
HTML
<div class="m-1 p-2 bg-primary rounded">m-1</div>
<div class="m-2 p-2 bg-primary rounded">m-2</div>
<div class="m-3 p-2 bg-primary rounded">m-3</div>

Padding

Apply padding to all sides of an element.

p-1
p-2
p-3
HTML
<div class="p-1 bg-primary rounded">p-1</div>
<div class="p-2 bg-primary rounded">p-2</div>
<div class="p-3 bg-primary rounded">p-3</div>

Directions

Apply margin or padding to a specific direction.

mt-3
me-3
mb-3
ms-3
HTML
<div class="mt-3 p-2 bg-primary rounded">mt-3</div>
<div class="me-3 p-2 bg-primary rounded">me-3</div>
<div class="mb-3 p-2 bg-primary rounded">mb-3</div>
<div class="ms-3 p-2 bg-primary rounded">ms-3</div>

Axis

Apply spacing to the horizontal or vertical axis.

mx-3
my-3
px-3
py-3
HTML
<div class="mx-3 p-2 bg-primary rounded">mx-3</div>
<div class="my-3 p-2 bg-primary rounded">my-3</div>
<div class="px-3 bg-primary rounded">px-3</div>
<div class="py-3 bg-primary rounded">py-3</div>

Auto margins

Use auto margin utilities to align elements.

mx-auto w-50
HTML
<div class="mx-auto p-2 bg-primary rounded w-50">mx-auto w-50</div>

Child spacing

Use space-x-* or space-y-* when direct siblings need spacing without changing the container’s layout mode. The utilities apply logical start margins, so horizontal spacing follows the writing direction. A margin utility on an individual child can override the generated sibling margin.

FirstSecondThird
First
Second
Third
HTML
<div class="d-flex space-x-1.5">
	<span>First</span>
	<span>Second</span>
	<span>Third</span>
</div>

<div class="space-y-2.5">
	<div>First</div>
	<div>Second</div>
	<div>Third</div>
</div>

Customize the spacing scale

Spacing utilities and components use the same --spacer-* runtime tokens. Override a token on :root to change that spacing level globally, or scope the override to an ancestor to customize only one part of the page. Base and responsive margin, padding and gap utilities all follow the nearest matching token, as do space-x-* and space-y-*.

CSS
:root {
	--spacer-3: 1.25rem;
	--spacer-2-5: 0.875rem;
}

.compact-panel {
	--spacer-3: 0.75rem;
}
HTML
<div class="d-flex p-3 gap-3">Uses the global spacer</div>
<div class="compact-panel">
	<div class="d-flex p-2 p-md-3 gap-2 gap-md-3">Uses the locally scoped spacer from md</div>
</div>

Custom entries added to the Sass spacings theme map generate matching --spacer-* tokens and utility classes. Each utility retains its Sass value as a fallback when its partial is imported without variables.scss. A decimal map key such as 2.5 generates classes such as .p-2.5 and the valid custom property --spacer-2-5.

Responsive behavior

Use responsive margin, padding, gap and child-spacing utilities to change spacing at different breakpoints.

p-2 p-md-3 p-lg-4
HTML
<div class="p-2 p-md-3 p-lg-4 bg-primary rounded">p-2 p-md-3 p-lg-4</div>
<div class="d-flex gap-1.5 gap-md-2.5">Responsive gap</div>
<div class="space-y-1.5 space-y-md-2.5">Responsive child spacing</div>

Variants use minimum widths and keep applying at larger breakpoints. The later matching breakpoint wins. Gap requires a flex or grid container; use direct-child spacing when the layout should remain unchanged.

Recipes

Combine spacing utilities with layout utilities.

Item 1
Item 2
Item 3
HTML
<div class="d-flex flex-column gap-2">
	<div class="p-2 bg-primary rounded">Item 1</div>
	<div class="p-2 bg-primary rounded">Item 2</div>
	<div class="p-2 bg-primary rounded">Item 3</div>
</div>

Keep actions apart

Use an automatic logical margin to push the secondary action to the opposite end.

HTML
<div class="d-flex flex-wrap align-items-center gap-2">
	<a href="/components/button">Button reference</a>
	<a class="ms-auto" href="/components/form">Form reference</a>
</div>