Skip to main content

Spacing Utilities

Add space around and inside elements! Spacing is one of the most important parts of good design - it makes everything easier to read.

Padding vs Margin

There are two types of spacing:

TypeDescription
PaddingSpace inside the element, between the border and content
MarginSpace outside the element, pushing other elements away
<!-- Padding (inside) -->
<div class="pa-4" style="background: lightblue;">I have padding inside me!</div>

<!-- Margin (outside) -->
<div class="ma-4" style="background: lightgreen;">
I have margin pushing others away!
</div>

The Spacing Scale

Spacing uses a simple number system. Each number = 4 pixels:

ClassSize
*-00px
*-14px
*-28px
*-312px
*-416px
*-624px
*-832px
*-1248px
*-1664px
*-2496px
Easy math

Multiply the number by 4 to get pixels! pa-4 = 4 x 4 = 16px

Padding Classes

Add space inside elements:

ClassDescription
pa-*Padding All sides
pt-*Padding Top
pb-*Padding Bottom
pl-*Padding Left
pr-*Padding Right
px-*Padding X-axis (left + right)
py-*Padding Y-axis (top + bottom)
<!-- All sides -->
<div class="pa-4">Padding everywhere!</div>

<!-- Just top and bottom -->
<div class="py-4">Padding top & bottom only</div>

<!-- Just left and right -->
<div class="px-4">Padding left & right only</div>

Margin Classes

Add space outside elements:

ClassDescription
ma-*Margin All sides
mt-*Margin Top
mb-*Margin Bottom
ml-*Margin Left
mr-*Margin Right
mx-*Margin X-axis (left + right)
my-*Margin Y-axis (top + bottom)
<div>
<div class="mb-4">I have margin bottom</div>
<div class="mt-4">I have margin top</div>
</div>

Center with Auto Margin

Use mx-auto to center block elements:

<div class="mx-auto" style="width: 200px;">I'm centered!</div>

Gap (for Flexbox/Grid)

The easiest way to add space between items in a flex or grid container:

ClassDescription
gx-0No column gap (0px)
gx-14px column gap
gx-28px column gap
gx-416px column gap
gy-*Row gap (for wrapped rows)
<div class="d-flex gx-4">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
Why use gap?

It's cleaner than margin because you don't have to worry about extra space on the first/last items!

Common Patterns

Card with Good Spacing

<div class="pa-4 br-2 sb-2">
<h3 class="mb-2">Card Title</h3>
<p class="mb-4">Card description with some text.</p>
<button class="px-4 py-2">Click Me</button>
</div>

Button Group

<div class="d-flex gx-2">
<button class="px-4 py-2">Save</button>
<button class="px-4 py-2">Cancel</button>
</div>

Stacked List

<div>
<div class="pa-3 mb-2">Item 1</div>
<div class="pa-3 mb-2">Item 2</div>
<div class="pa-3">Item 3</div>
</div>

Cheat Sheet

Sides

CodeMeaning
aAll
tTop
bBottom
lLeft
rRight
xLeft + Right
yTop + Bottom

Values (x 4px)

ValueSize
00px
14px
28px
416px
832px
autoauto