Bootstrap provides spacing utility classes that make it easy to add margin and padding to HTML elements. These utilities help developers create clean layouts without writing custom CSS for every spacing requirement.
m-* for
margin and p-* for padding. They can also be applied to
specific sides such as top, bottom, start, and end.
Spacing means the empty space around or inside an HTML element. Bootstrap provides utility classes to control this space quickly.
Bootstrap spacing utilities mainly control:
Margin creates space outside an element, while padding creates space inside an element.
<div class="m-3">
Margin Example
</div>
<div class="p-3">
Padding Example
</div>
Bootstrap uses m for margin and p for padding.
Margin classes begin with the letter m.
<div class="m-0">
No margin
</div>
<div class="m-3">
Medium margin
</div>
<div class="m-5">
Large margin
</div>
The number determines the spacing size.
Padding classes begin with the letter p.
<div class="p-0">
No padding
</div>
<div class="p-3">
Medium padding
</div>
<div class="p-5">
Large padding
</div>
Padding adds space between the content and the element's border.
Bootstrap provides a predefined spacing scale commonly used with spacing utilities.
| Class Value | Typical Spacing |
|---|---|
| 0 | 0 |
| 1 | 0.25rem |
| 2 | 0.5rem |
| 3 | 1rem |
| 4 | 1.5rem |
| 5 | 3rem |
For example, p-3 applies the spacing value associated
with 3.
The mt-* classes add margin to the top of an element.
<div class="mt-1">
Small top margin
</div>
<div class="mt-3">
Medium top margin
</div>
<div class="mt-5">
Large top margin
</div>
Here, t represents top.
The mb-* classes add margin to the bottom of an element.
<h2 class="mb-3">
Bootstrap
</h2>
<p>
Learn Bootstrap spacing.
</p>
Here, b represents bottom.
The ms-* classes add margin to the start side of an
element.
<div class="ms-4">
Start Margin
</div>
The use of start and end utilities also works better with right-to-left layouts than fixed left and right terminology.
The me-* classes add margin to the end side.
<button class="btn btn-primary me-2">
Save
</button>
<button class="btn btn-secondary">
Cancel
</button>
Here, e represents end.
The mx-* classes apply margin to both the start and end
sides.
<div class="mx-3">
Horizontal Margin
</div>
This is useful when you want equal horizontal spacing.
The my-* classes apply margin to both the top and bottom.
<div class="my-4">
Vertical Margin
</div>
This is useful for creating equal vertical spacing.
The pt-* classes add padding to the top of an element.
<div class="pt-4">
Top Padding
</div>
Here, p means padding and t means top.
The pb-* classes add padding to the bottom.
<div class="pb-4">
Bottom Padding
</div>
Here, b represents bottom.
The ps-* classes add padding to the start side.
<div class="ps-4">
Start Padding
</div>
Start-based utilities are useful for layouts that support different text directions.
The pe-* classes add padding to the end side.
<div class="pe-4">
End Padding
</div>
Here, e represents end.
The px-* classes add padding to both the start and end
sides.
<div class="px-4">
Horizontal Padding
</div>
This is useful for adding equal left and right spacing in a left-to-right layout.
The py-* classes add padding to both the top and bottom.
<div class="py-4">
Vertical Padding
</div>
This is commonly used for buttons, sections, cards, and banners.
Bootstrap provides the m-auto utility for automatic
margins.
<div
class="mx-auto"
style="width: 200px;">
Centered Element
</div>
mx-auto is commonly used to horizontally center a
fixed-width block element.
Bootstrap also provides negative margin utilities using an n
prefix in supported spacing utilities.
<div class="mt-n2">
Negative Top Margin
</div>
Bootstrap spacing utilities can be combined with responsive breakpoints.
<div
class="p-2 p-md-4 p-lg-5">
Responsive Padding
</div>
In this example, the padding changes at different viewport breakpoints.
Margin can also change according to the viewport size.
<div
class="mt-2 mt-md-4 mt-lg-5">
Responsive Margin
</div>
The breakpoint-prefixed utility applies from that breakpoint upward, unless another class overrides it.
Spacing utilities are useful for separating buttons.
<button class="btn btn-primary me-2">
Save
</button>
<button class="btn btn-success me-2">
Submit
</button>
<button class="btn btn-danger">
Delete
</button>
The me-2 class adds spacing after the first two buttons.
Spacing utilities can be used to create comfortable spacing inside cards.
<div class="card p-4">
<h3 class="mb-3">
Course
</h3>
<p class="mb-4">
Learn Bootstrap from basics.
</p>
<button class="btn btn-primary">
Start
</button>
</div>
Spacing utilities can make forms easier to read and use.
<div class="mb-3">
<label class="form-label">
Name
</label>
<input
type="text"
class="form-control">
</div>
<div class="mb-3">
<label class="form-label">
Email
</label>
<input
type="email"
class="form-control">
</div>
The mb-3 class creates space between form fields.
Large padding values can be used to create breathing room around sections.
<section class="py-5">
<h2>
About Us
</h2>
<p>
Learn more about our organization.
</p>
</section>
The py-5 class adds vertical padding.
Bootstrap also provides gap utilities that work with flex and grid layouts.
<div
class="d-flex gap-3">
<button class="btn btn-primary">
One
</button>
<button class="btn btn-success">
Two
</button>
<button class="btn btn-danger">
Three
</button>
</div>
The gap-3 class adds spacing between flex or grid items.
Bootstrap provides row-gap-* and
column-gap-* utilities for controlling gaps separately.
<div
class="d-grid
row-gap-3
column-gap-2">
<div>
Item One
</div>
<div>
Item Two
</div>
</div>
These utilities are useful when vertical and horizontal gaps need different values.
Avoid these common mistakes when using Bootstrap spacing utilities:
The following example demonstrates margin, padding, and gap utilities together.
Learn Bootstrap components, utilities, responsive layouts, and practical projects.
<div class="p-4 bg-light rounded">
<h3 class="mb-3">
Bootstrap Course
</h3>
<p class="mb-4">
Learn Bootstrap components,
utilities and responsive layouts.
</p>
<div class="d-flex gap-2">
<button class="btn btn-primary">
Start Course
</button>
<button class="btn btn-success">
View Lessons
</button>
</div>
</div>
This example combines Bootstrap margin, padding, responsive spacing, and gap utilities to create a simple responsive course section.
<section
class="py-5">
<div
class="container">
<div
class="p-4 p-md-5 bg-light rounded">
<h2 class="mb-3">
Learn Bootstrap
</h2>
<p class="mb-4">
Build responsive websites
using Bootstrap utilities
and components.
</p>
<div
class="d-flex
flex-column
flex-md-row
gap-2">
<button
class="btn btn-primary">
Start Learning
</button>
<button
class="btn btn-success">
View Course
</button>
</div>
</div>
</div>
</section>
Build responsive websites using Bootstrap utilities and components.
m-* controls margin.p-* controls padding.mt-* controls margin-top.mb-* controls margin-bottom.ms-* controls margin-start.me-* controls margin-end.mx-* controls horizontal margin.my-* controls vertical margin.pt-* controls padding-top.pb-* controls padding-bottom.ps-* controls padding-start.pe-* controls padding-end.px-* controls horizontal padding.py-* controls vertical padding.mx-auto is commonly used for horizontal centering.p-md-4.gap-* controls spacing between flex or grid items.row-gap-* and column-gap-* can control gaps
separately.Question: Which Bootstrap class adds margin to all four sides of an element?