Bootstrap Margin and Padding Cheat Sheet (mt-3, px-4, gap)
In Bootstrap 5, m-* classes set margin and p-* classes set padding: add a side (t, b, s, e, x, or y) and a size from 0 to 5, so mt-3 is a 1rem top margin and px-4 is 1.5rem of left and right padding. Bootstrap’s spacing utilities let you add margin and padding with short, predictable class names instead of custom CSS. Once you learn the notation, you can space any element in a few keystrokes and make it responsive. This guide covers the full system, how the scale is built from one variable, how to customize it, and the patterns you will use most. It pairs with the flexbox and grid guides.
The notation
A spacing class is built from three parts: a property, an optional side, and a size.
{property}{side}-{size}
- Property:
mfor margin,pfor padding. - Side:
t(top),b(bottom),s(start / left),e(end / right),x(left and right),y(top and bottom), or omitted for all four sides. - Size:
0through5, orauto.
So mt-3 is margin-top size 3, px-4 is horizontal padding size 4, and m-0 removes all margins.
<div class="mt-3 mb-5 px-4 py-2">Spaced content</div>
Every class at a glance
Replace * with a size from 0 to 5 (or auto for margins):
| You want | Margin | Padding |
|---|---|---|
| Top | mt-* |
pt-* |
| Bottom | mb-* |
pb-* |
| Left (start) | ms-* |
ps-* |
| Right (end) | me-* |
pe-* |
| Left and right | mx-* |
px-* |
| Top and bottom | my-* |
py-* |
| All four sides | m-* |
p-* |
So margin-top is mt-3, padding-bottom is pb-3, margin-left is ms-3, and padding left and right is px-3.
The sides at a glance
| Suffix | Affects |
|---|---|
t / b |
top / bottom |
s / e |
start (left) / end (right) |
x |
left and right |
y |
top and bottom |
| (none) | all four sides |
The s/e (start/end) naming is logical, so it flips automatically in right-to-left layouts, which is why Bootstrap 5 uses ms-/me- instead of the old ml-/mr-.
The spacing scale
The sizes map to multiples of $spacer, which defaults to 1rem (16px):
| Class size | Value |
|---|---|
| 0 | 0 |
| 1 | 0.25rem (4px) |
| 2 | 0.5rem (8px) |
| 3 | 1rem (16px) |
| 4 | 1.5rem (24px) |
| 5 | 3rem (48px) |
Because everything scales from $spacer, changing that one variable rescales every spacing utility, every gap, and the grid gutters at once. That single point of control is what makes the scale feel consistent.
Responsive spacing
Insert a breakpoint to change spacing per screen size:
{property}{side}-{breakpoint}-{size}
For example, small padding on mobile and larger padding from medium screens up:
<div class="p-2 p-md-5">Responsive padding</div>
The breakpoints are sm, md, lg, xl, and xxl. Like all Bootstrap responsive utilities, a value applies from that breakpoint up, so p-md-5 means “size 5 from medium screens and wider.”
Center with auto margins
mx-auto sets the left and right margins to auto, which horizontally centers a block element that has a width:
<div class="mx-auto" style="width: 300px;">Centered block</div>
Inside a flex container, ms-auto, me-auto, and my-auto push items by absorbing free space. For the full set of centering techniques, see how to center a div in Bootstrap.
Negative margins
Negative margins pull an element closer to its neighbor or out of its container, for example a card that overlaps the hero above it. Bootstrap 5 has them, but they are turned off by default, so mt-n3 does nothing in the standard CSS from a CDN. Turn them on in Sass:
$enable-negative-margins: true;
@import "bootstrap/scss/bootstrap";
That generates classes with an n before the size, for sizes 1 to 5: mt-n1 to mt-n5, mx-n3, ms-n2, and so on. Without Sass, write the one rule you need instead, such as .overlap { margin-top: -3rem; }.
Space between elements: gap, not margins
For evenly spacing the children of a flex or grid container, prefer gap-* over margins. It avoids the awkward “last item still has a margin” problem and uses the same 0 to 5 scale:
<div class="d-flex flex-wrap gap-3">...</div>
Bootstrap 5.3 also has row-gap-* and column-gap-* to set the two directions separately, for example column-gap-4 row-gap-2 on a wrapping row of tags. gap works on d-flex and d-grid containers.
To push items to opposite ends instead of spacing them evenly, use justify-content-between on the flex container, or ms-auto on the item that should move to the right; the flexbox guide covers both. In the grid, the gutter utilities g-*, gx-*, and gy-* control the space between columns and rows; see the grid guide.
Customize the scale
The default scale fits most designs, but you can change $spacer or define the $spacers map in Sass to add your own steps. Set it before importing Bootstrap, repeating the default steps you want to keep:
$spacer: 1rem;
$spacers: (
0: 0,
1: $spacer * .25,
2: $spacer * .5,
3: $spacer,
4: $spacer * 1.5,
5: $spacer * 3,
6: $spacer * 4, // 4rem, new
7: $spacer * 6, // 6rem, new
);
@import "bootstrap/scss/bootstrap";
Now p-6, mt-7, and the rest exist alongside the built-in sizes. Because the variable system drives all of this, the quickest way to feel the effect is the visual builder: adjust $spacer, watch a full UI kit reflow live, and export clean Bootstrap Sass or CSS. The mechanics of overriding variables are in Bootstrap Sass variables explained.
Common patterns
- Vertical rhythm between sections:
mb-5(or larger) on each block. - Card or button inner padding:
p-3/p-4. - Center a fixed-width block:
mx-autowith a width. - Even spacing in a row of items:
d-flex gap-3rather than margins on each child.
Bootstrap 3 and 4 spacing classes
If you are reading older code or tutorials:
- Bootstrap 3 had no spacing utilities at all. Margins and padding came from custom CSS, so a class like
mb-3does nothing there. - Bootstrap 4 introduced the same
m/pnotation with physical sides:ml-3andmr-3for left and right,pl-*andpr-*for padding, andml-autoto push an item right. - Bootstrap 5 renamed those to logical sides:
ms-*,me-*,ps-*,pe-*, andms-auto. The oldml-autosilently does nothing in Bootstrap 5, which is the most common reason a “push right” layout breaks after an upgrade.
Next steps
With spacing under control, the natural companions are flexbox utilities and the grid for layout, plus the color and typography guides for the rest of your theme.
FAQ
What do the m and p classes mean in Bootstrap?
m is margin and p is padding. You add an optional side (t, b, s, e, x, y) and a size (0 to 5, or auto), so mt-3 is margin-top size 3 and px-4 is horizontal padding size 4.
What are the Bootstrap spacing sizes?
Sizes 0 to 5 map to multiples of $spacer (1rem by default): 0 = 0, 1 = 0.25rem, 2 = 0.5rem, 3 = 1rem, 4 = 1.5rem, 5 = 3rem. Changing $spacer rescales them all.
How do I add a custom spacing size in Bootstrap?
Extend the $spacers Sass map before importing Bootstrap, for example adding 6: $spacer * 4. Bootstrap then generates the matching utilities like p-6 and mt-6 for you.
How do I add space between elements in Bootstrap?
Put the elements in a flex container and add a gap: <div class="d-flex gap-3">. For stacked blocks, add a bottom margin to each, such as mb-3. To push two items to opposite ends of a row, use d-flex justify-content-between.
Why don’t negative margins like mt-n3 work in Bootstrap 5?
Negative margin utilities are disabled by default. Set $enable-negative-margins: true in Sass before importing Bootstrap to generate mt-n1 to mt-n5 and the other sides, or write a custom CSS rule with a negative margin.
What is the difference between ms-/me- and ml-/mr- in Bootstrap?
Bootstrap 5 renamed the directional margin/padding utilities to logical ones: ms- (start) and me- (end) instead of ml- (left) and mr- (right). The logical versions flip automatically in right-to-left layouts.
Try it in the builder
Make these changes visually with a live preview, then export clean Bootstrap Sass or CSS.
Open the Builder