Bootstrap - Spacing Utilities
What Are Spacing Utilities?
Spacing utilities in Bootstrap are special classes that help you add or remove space (margins and padding) around elements — without writing any CSS.
They use simple, short class names that start with:
-
.m-
→ for margin -
.p-
→ for padding
So instead of writing:
margin: 20px;
padding: 10px;
You can just use Bootstrap classes like:
<div class="m-3 p-2">...</div>
1. The Basic Syntax
{property}{sides}-{breakpoint?}-{size}
Let’s break it down:
Part | Meaning |
---|---|
property |
m for margin, p for padding |
sides |
Which side(s) you want to apply it to |
breakpoint |
(Optional) For responsive spacing (like sm , md , lg , etc.) |
size |
How much space to add (from 0 to 5 or auto ) |
2. The “Sides”
You can control spacing on all or specific sides:
Class Part | Applies To |
---|---|
(none) | All sides |
t |
Top |
b |
Bottom |
s |
Start (left in LTR) |
e |
End (right in LTR) |
x |
Left and right (horizontal) |
y |
Top and bottom (vertical) |
Examples:
.m-3 → margin on all sides
.mt-2 → margin-top
.mb-4 → margin-bottom
.ms-5 → margin-left (or start side)
.me-1 → margin-right (or end side)
.mx-2 → margin-left and margin-right
.py-4 → padding-top and padding-bottom
3. The “Size” Values
Bootstrap provides spacing from 0 to 5, plus auto
.
Size | Space (in rem ) |
Description |
---|---|---|
0 |
0 |
Removes space |
1 |
0.25rem (≈4px) |
Small space |
2 |
0.5rem (≈8px) |
Medium-small space |
3 |
1rem (≈16px) |
Medium space |
4 |
1.5rem (≈24px) |
Large space |
5 |
3rem (≈48px) |
Extra-large space |
auto |
— | Automatically adjusts margin (mainly for centering) |
Examples:
.m-0 → no margin
.p-1 → small padding
.mt-3 → medium top margin
.px-4 → large horizontal padding
.m-auto → centers block horizontally
4. Responsive Spacing
You can make spacing responsive using breakpoints:sm
, md
, lg
, xl
, xxl
Syntax:
{property}{sides}-{breakpoint}-{size}
Examples:
.mt-md-4 → Adds top margin of size 4 on medium screens and up
.px-lg-5 → Adds horizontal padding of size 5 on large screens and up
.m-sm-2 → Adds margin of size 2 on small screens and up
5. Common Examples
<!-- Equal margin and padding -->
<div class="m-3 p-3">Box</div>
<!-- Add top and bottom margin -->
<div class="my-4">Box</div>
<!-- Add only left padding -->
<div class="ps-5">Box</div>
<!-- Center element horizontally -->
<div class="mx-auto" style="width: 200px;">Centered Box</div>
<!-- Responsive spacing -->
<div class="p-2 p-md-4 p-lg-5">Responsive Padding</div>
6. Visual Summary
Utility | Purpose | Example |
---|---|---|
.m-0 |
Remove all margins | <div class="m-0"> |
.mt-3 |
Add margin-top | <div class="mt-3"> |
.mx-2 |
Add margin left/right | <div class="mx-2"> |
.p-4 |
Add padding on all sides | <div class="p-4"> |
.py-5 |
Add padding top/bottom | <div class="py-5"> |
.m-auto |
Center element (auto margin) | <div class="m-auto"> |
7. Why It’s Useful
-
No need for custom CSS.
-
Consistent spacing across your project.
-
Responsive control (works on all screen sizes).
-
Fast and easy to use.
In Summary:
-
.m-
controls margin,.p-
controls padding. -
Add direction (
t
,b
,s
,e
,x
,y
) to control specific sides. -
Use numbers
0–5
(orauto
) to set spacing amount. -
Add breakpoints (
sm
,md
,lg
, etc.) for responsive spacing.
Example (All Together):
<div class="p-3 my-4 mx-auto">
<p class="mb-2">This box has padding, margin, and is centered.</p>
</div>
In short:
Bootstrap’s spacing utilities (.m-
and.p-
) make it super easy to add margins and padding anywhere — quickly, consistently, and responsively — without writing CSS manually.