Margin vs Padding
These two words confuse many beginners, so remember this picture:
- Margin is the space outside an element, between it and its neighbors
- Padding is the space inside an element, between its border and its content
The Naming Pattern
The class name follows this pattern: {property}{side}-{size}
| Part | Options |
|---|---|
| Property | m = margin, p = padding |
| Side | t top, b bottom, s start (left), e end (right), x left and right, y top and bottom, (nothing) all four sides |
| Size | 0, 1, 2, 3, 4, 5, and auto for margin |
Sizes
| Number | Space |
|---|---|
| 0 | 0 |
| 1 | 0.25rem (4px) |
| 2 | 0.5rem (8px) |
| 3 | 1rem (16px) |
| 4 | 1.5rem (24px) |
| 5 | 3rem (48px) |
Examples
Centering with Auto Margin
mx-auto sets both left and right margins to auto, which centers a box that has a fixed or limited width. ms-auto pushes an element to the right.
Responsive Spacing
Gap
Use gap-* on flex or grid containers to add space between children without margins.
Negative Margins
Negative margin classes use an n before the size, such as mt-n1. They are not enabled by default and need the Sass option to be turned on, so beginners rarely use them.
Common Mistakes
- Mixing up margin and padding
- Using left and right (
ml,mr), which were Bootstrap 4 names. In Bootstrap 5 usemsandme - Using
mx-autoon an element that takes the full width, where nothing visibly changes
bg-light border and apply p-1, p-3, p-5 on them. Then try m-1, m-3, m-5 and see the difference.
Conclusion
Spacing classes are used in nearly every Bootstrap project. Learn the {property}{side}-{size} pattern, and you can space anything quickly.

