Spacing utilities allow you to modify an element's margin and padding on the fly without writing custom CSS rules. Bootstrap 5 uses an intuitive shorthand naming syntax based on a consistent 6-level rem scale.
{property}{sides}-{size}Every spacing class follows a standard 3-part formula:
Property: m for margin • p for padding
Sides: t (top) • b (bottom) • s (start/left) • e (end/right) • x (left & right) • y (top & bottom) • blank (all 4 sides)
Size: 0 (0px) • 1 (0.25rem / 4px) • 2 (0.5rem / 8px) • 3 (1rem / 16px) • 4 (1.5rem / 24px) • 5 (3rem / 48px) • auto (sets margin: auto)
| Class Level | Multiplier | Computed Pixel Size (Default 16px Base) |
|---|---|---|
m-0 / p-0 | 0 | 0px (Resets margin / padding) |
m-1 / p-1 | $spacer * 0.25 | 4px |
m-2 / p-2 | $spacer * 0.5 | 8px |
m-3 / p-3 | $spacer * 1.0 | 16px (Standard Base Spacer) |
m-4 / p-4 | $spacer * 1.5 | 24px |
m-5 / p-5 | $spacer * 3.0 | 48px (Generous Section Padding) |
m-auto / ms-auto | auto | Auto Margins (Centering & Pushing) |
gap-* Utility for Flexbox & GridWhen using d-flex or .grid, use the gap-* utility to set gutters between children without applying individual margins:
<div class="d-flex gap-3 bg-light p-3 border rounded">
<button class="btn btn-primary">Action 1</button>
<button class="btn btn-secondary">Action 2</button>
<button class="btn btn-outline-danger">Cancel</button>
</div>
Strengthen your responsive web design mastery by studying these connected classes:
Combine margins and paddings with d-flex and justify-content classes.
Adjust row and column gutter spacing using Bootstrap spacing scale.
Control element visibility, borders, sizing, and fixed positioning.
Use vstack and hstack with gap-* for instant consistent item spacing.
s stands for start (left in LTR languages, right in RTL languages like Arabic/Hebrew) and e stands for end. This provides native Right-to-Left (RTL) internationalization support.
.mx-auto (sets margin-left: auto; margin-right: auto;).
class='p-2 p-md-5'.
Advance your practical frontend engineering skills with the next topic in our synchronized curriculum.
Continue to Class 35: Flexbox Utilities → Previous: ← Class 33: Toasts Notifications | Full Syllabus