How do I use Bootstrap’s spacing utilities (mt-, mb-, px-, py-) effectively? Pending Review
Asked on Sep 16, 2025
1 Answer
Bootstrap's spacing utilities provide a convenient way to control the margin and padding of elements using classes. These utilities are prefixed with "m" for margin and "p" for padding, followed by a direction (t, b, l, r, x, y) and a size (0-5 or auto).
<!-- BEGIN COPY / PASTE -->
<div class="mt-3 mb-3 px-4 py-2 bg-primary text-white">
Spacing Example
</div>
<!-- END COPY / PASTE -->
- "mt-3" adds a top margin of 1rem (16px if the root font size is 16px).
- "mb-3" adds a bottom margin of 1rem.
- "px-4" adds horizontal padding (left and right) of 1.5rem.
- "py-2" adds vertical padding (top and bottom) of 0.5rem.
- Spacing utilities are responsive; you can use them with breakpoints like "mt-md-4" for medium screens and up.