Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 58 ยท Utilities

Spacing Utilities

Master Bootstrap 5.3 spacing utilities: the m-* and p-* scale, sides and axes, auto margins, responsive spacing, gap utilities and why negative margins are off.

White space makes a layout feel organized. In plain CSS you set margin and padding with lengths you pick yourself, and over a project those lengths drift: 14px here, 18px there. Bootstrap replaces that with a scale of six steps and short classes such as mt-3 and px-4, so the whole site uses the same rhythm.

You will learn the naming pattern, the sides and axes, auto margins for centering, responsive spacing and the gap utilities for flex and grid containers. Spacing is used in every later lesson, so it is worth learning well.

The naming pattern

Every spacing class follows one formula:

TEXT
{property}{sides}-{size}            (all breakpoints)
{property}{sides}-{breakpoint}-{size}   (from that breakpoint up)
  • property: m for margin, p for padding.
  • sides: t top, b bottom, s start (left in left-to-right text), e end (right), x left and right, y top and bottom. Leave it out for all four sides.
  • size: 0 to 5, or auto (margins only).

So mt-3 is margin-top step 3, px-2 is padding left and right step 2, p-4 is padding on all sides step 4. Bootstrap uses s and e instead of l and r so layouts mirror automatically for right-to-left languages such as Arabic and Hebrew.

The scale

The sizes are multiples of the root spacer, which is 1rem (normally 16 px):

Size Value Pixels at 16 px root
0 0 0
1 0.25 rem 4
2 0.5 rem 8
3 1 rem 16
4 1.5 rem 24
5 3 rem 48

Because the values are in rem, spacing grows when a visitor increases the browser's default font size, which is good for accessibility.

The spacing scale as padding

The blue area around each white label is the padding. It grows with the step number.

flowchart LR
  A["mt-3"] --> B["m = margin"]
  A --> C["t = top side"]
  A --> D["3 = 1rem step"]

Reading a spacing class: property, side, size.

Sides and axes

Margin on different sides

Compare the shorthand with plain CSS: mx-5 is margin-left and margin-right of 3 rem, while m-5 sets all four. Combining classes lets you override: p-3 pt-0 gives padding on three sides and none on top, because the more specific side class comes later in Bootstrap's CSS.

Auto margins

auto works for margins only (m-auto, mx-auto, ms-auto, me-auto, mt-auto, mb-auto, my-auto). Its two classic jobs:

  • Center a block with a set width: mx-auto splits the leftover horizontal space equally. The element needs a width smaller than its parent, otherwise there is nothing to split.
  • Push items apart in a flex container: ms-auto absorbs all free space on the start side and pushes the item to the end. This is how a navbar pins links to the right (The Navbar).
mx-auto and ms-auto

Negative margins

Margins can be negative in CSS, and Bootstrap 5 can generate classes such as mt-n3. But they are disabled by default (the Sass option $enable-negative-margins is false), so on a stock CDN build those classes do not exist. You would turn them on when compiling Sass (Customizing with Sass). Most layouts never need them: the grid already uses negative margins internally on .row.

Responsive spacing

Add a breakpoint between the side and the size to change spacing from that screen width up: p-2 p-md-4 p-xl-5. As with columns, the rules are mobile-first, so the unprefixed class covers small screens and the prefixed ones take over as the viewport grows (Breakpoints and Responsive Design).

Spacing that grows with the screen

The gap utilities

Margins on children are clumsy when items wrap onto several lines, because the first or last item has unwanted edge space. Modern layouts use gap, which only adds space between items in a flex or grid container. Bootstrap offers:

  • gap-0 to gap-5: both directions.
  • row-gap-* and column-gap-*: one direction (added in 5.3).

They use the same 0 to 5 scale and also have breakpoint versions, such as gap-md-4.

gap in flex wrap

d-grid makes an element a CSS grid container, and the inline grid-template-columns defines three columns (Bootstrap's d-grid does not define columns; a later lesson covers the CSS grid option in Nested Grids and Full Layouts). Use gap whenever you can. The g-*, gx-* and gy-* classes on rows, from Gutters, Alignment and Ordering, are a related but separate system for grid gutters.

Tips and pitfalls

  • Margin collapse. Vertical margins of block siblings merge: mb-4 followed by mt-3 gives 1.5 rem, not 2.5 rem. Padding and gap never collapse.
  • Inline elements ignore vertical margin. A <span> with mt-3 does nothing; make it d-inline-block or d-block.
  • Pick one side to own the space. A common habit is to use only mb-* between stacked elements, so you do not wonder which neighbor created the space.
  • Do not fight the scale. If you keep wanting 20 px, either use step 3 or change the spacer map in Sass.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the class px-4 do?

    p means padding, x means the horizontal axis (start and end), and step 4 is 1.5 rem.
  2. 2. Which class pair centers a 300 px wide block inside its parent?

    Automatic left and right margins split the free space equally, but the element needs a width smaller than its parent.
  3. 3. What is the difference between gap-3 and mb-3 on each child?

    Gap is applied by the container between items. Margins on children also leave space after the last item.
  4. 4. Why does mt-n3 not work in a default Bootstrap CDN build?

    The $enable-negative-margins option is false by default, so the classes are not generated.

Summary

  • Spacing classes follow {m|p}{side}-{breakpoint}-{size} with sizes 0 to 5 (0 to 3 rem) and auto for margins.
  • Sides: t, b, s, e, x, y; no side means all four.
  • mx-auto centers a block with a width; ms-auto pushes a flex item to the end.
  • Add a breakpoint infix such as p-md-4 to change spacing as the screen grows.
  • Prefer gap-*, row-gap-* and column-gap-* for spacing between items.
  • Negative margin classes are off by default.

Next lesson: Display and Visibility.