Skip to main content Skip to docs navigation

Margin

Use margin utilities to responsively control the space around elements.

Class Styles
.m-0 margin: 0;
.m-1 margin: 0.25rem;
.m-2 margin: 0.5rem;
.m-3 margin: 1rem;
.m-4 margin: 1.5rem;
.m-5 margin: 3rem;
.m-auto margin: auto;
.mx-0 margin-inline: 0;
.mx-1 margin-inline: 0.25rem;
.mx-2 margin-inline: 0.5rem;
.mx-3 margin-inline: 1rem;
.mx-4 margin-inline: 1.5rem;
.mx-5 margin-inline: 3rem;
.mx-auto margin-inline: auto;
.my-0 margin-block: 0;
.my-1 margin-block: 0.25rem;
.my-2 margin-block: 0.5rem;
.my-3 margin-block: 1rem;
.my-4 margin-block: 1.5rem;
.my-5 margin-block: 3rem;
.my-auto margin-block: auto;
.mt-0 margin-block-start: 0;
.mt-1 margin-block-start: 0.25rem;
.mt-2 margin-block-start: 0.5rem;
.mt-3 margin-block-start: 1rem;
.mt-4 margin-block-start: 1.5rem;
.mt-5 margin-block-start: 3rem;
.mt-auto margin-block-start: auto;
.me-0 margin-inline-end: 0;
.me-1 margin-inline-end: 0.25rem;
.me-2 margin-inline-end: 0.5rem;
.me-3 margin-inline-end: 1rem;
.me-4 margin-inline-end: 1.5rem;
.me-5 margin-inline-end: 3rem;
.me-auto margin-inline-end: auto;
.mb-0 margin-block-end: 0;
.mb-1 margin-block-end: 0.25rem;
.mb-2 margin-block-end: 0.5rem;
.mb-3 margin-block-end: 1rem;
.mb-4 margin-block-end: 1.5rem;
.mb-5 margin-block-end: 3rem;
.mb-auto margin-block-end: auto;
.ms-0 margin-inline-start: 0;
.ms-1 margin-inline-start: 0.25rem;
.ms-2 margin-inline-start: 0.5rem;
.ms-3 margin-inline-start: 1rem;
.ms-4 margin-inline-start: 1.5rem;
.ms-5 margin-inline-start: 3rem;
.ms-auto margin-inline-start: auto;

Overview

Use margin utilities to control the outer space around elements. Margin utilities are built from a default Sass map ranging from .25rem to 3rem. Use utilities like .m-3 and .m-5 to control the margin on all sides of an element:

.m-3
.m-5
HTML
<div class="m-3">.m-3</div>
<div class="m-5">.m-5</div>

Using the CSS Grid layout module? Consider using the gap utility instead.

Notation

Margin utilities that apply to all breakpoints, from xs to 2xl, have no breakpoint abbreviation in them. This is because those classes are applied from min-width: 0 and up, and thus are not bound by a media query. The remaining breakpoints, however, do include a breakpoint abbreviation.

The classes are named using the format {property}{sides}-{size} for xs and {property}{sides}-{breakpoint}-{size} for sm, md, lg, xl, and 2xl.

Where property is:

  • m - for classes that set margin

Where sides is one of:

  • t - for classes that set margin-top
  • b - for classes that set margin-bottom
  • s - (start) for classes that set margin-left in LTR, margin-right in RTL
  • e - (end) for classes that set margin-right in LTR, margin-left in RTL
  • x - for classes that set both *-left and *-right
  • y - for classes that set both *-top and *-bottom
  • blank - for classes that set a margin on all 4 sides of the element

Where size is one of:

  • 0 - for classes that eliminate the margin by setting it to 0
  • 1 - (by default) for classes that set the margin to $spacer * .25
  • 2 - (by default) for classes that set the margin to $spacer * .5
  • 3 - (by default) for classes that set the margin to $spacer
  • 4 - (by default) for classes that set the margin to $spacer * 1.5
  • 5 - (by default) for classes that set the margin to $spacer * 3
  • auto - for classes that set the margin to auto

(You can add more sizes by adding entries to the $spacers Sass map variable.)

Examples

Single side

Use utilities like .mt-3, .me-2, .mb-4, and .ms-1 to control the margin on one side of an element:

.mt-4
.me-4
.mb-4
.ms-4
HTML
<div class="mt-4">.mt-4</div>
<div class="me-4">.me-4</div>
<div class="mb-4">.mb-4</div>
<div class="ms-4">.ms-4</div>

Horizontal margin

Use utilities like .mx-3 and .mx-5 to control the horizontal margin of an element:

.mx-3
.mx-5
HTML
<div class="mx-3">.mx-3</div>
<div class="mx-5">.mx-5</div>

Vertical margin

Use utilities like .my-3 and .my-5 to control the vertical margin of an element:

.my-3
.my-5
HTML
<div class="my-3">.my-3</div>
<div class="my-5">.my-5</div>

Horizontal centering

Bootstrap includes an .mx-auto class for horizontally centering fixed-width block level content—that is, content that has display: block and a width set—by setting the horizontal margins to auto.

.mx-auto
HTML
<div class="mx-auto" style="width: 200px;">
  Centered element
</div>

Negative margin

In CSS, margin properties can utilize negative values (padding cannot). These negative margins are disabled by default, but can be enabled in Sass by setting $enable-negative-margins: true.

The syntax is nearly the same as the default, positive margin utilities, but with the addition of n before the requested size. Here’s an example class that’s the opposite of .mt-1:

SCSS
.mt-n1 {
  margin-top: -0.25rem !important;
}

Responsive

All margin utilities are responsive and include all breakpoints.

  • .m-0 through .m-5 and .m-auto
  • .m-sm-0 through .m-sm-5 and .m-sm-auto
  • .m-md-0 through .m-md-5 and .m-md-auto
  • .m-lg-0 through .m-lg-5 and .m-lg-auto
  • .m-xl-0 through .m-xl-5 and .m-xl-auto
  • .m-2xl-0 through .m-2xl-5 and .m-2xl-auto

CSS

Sass utilities API

Margin utilities are declared in our utilities API in scss/_utilities.scss. Learn how to use the utilities API.

SCSS
"margin": (
  responsive: true,
  property: margin,
  class: m,
  values: map.merge($spacers, (auto: auto))
),
"margin-x": (
  responsive: true,
  property: margin-right margin-left,
  class: mx,
  values: map.merge($spacers, (auto: auto))
),
"margin-y": (
  responsive: true,
  property: margin-top margin-bottom,
  class: my,
  values: map.merge($spacers, (auto: auto))
),
"margin-top": (
  responsive: true,
  property: margin-top,
  class: mt,
  values: map.merge($spacers, (auto: auto))
),
"margin-end": (
  responsive: true,
  property: margin-right,
  class: me,
  values: map.merge($spacers, (auto: auto))
),
"margin-bottom": (
  responsive: true,
  property: margin-bottom,
  class: mb,
  values: map.merge($spacers, (auto: auto))
),
"margin-start": (
  responsive: true,
  property: margin-left,
  class: ms,
  values: map.merge($spacers, (auto: auto))
),