Margin

Utilities for controlling an element’s margin.

Quick reference

ClassProperties
.m-0margin: 0;
.m-1margin: 0.25rem; /* 4px */
.m-2margin: 0.5rem; /* 8px */
.m-3margin: 0.75rem; /* 12px */
.m-4margin: 1rem; /* 16px */
.m-5margin: 1.25rem; /* 20px */
.m-6margin: 1.5rem; /* 24px */
.m-7margin: 1.75rem; /* 28px */
.m-8margin: 2rem; /* 32px */
.m-9margin: 2.25rem; /* 36px */
.m-10margin: 2.5rem; /* 40px */
.m-11margin: 2.75rem; /* 44px */
.m-12margin: 3rem; /* 48px */
.m-14margin: 3.5rem; /* 56px */
.m-16margin: 4rem; /* 64px */
.m-20margin: 5rem; /* 80px */
.m-automargin: auto;
.mx-0margin-left: 0;
margin-right: 0;
.mx-1margin-left: 0.25rem; /* 4px */
margin-right: 0.25rem; /* 4px */
.mx-2margin-left: 0.5rem; /* 8px */
margin-right: 0.5rem; /* 8px */
.mx-3margin-left: 0.75rem; /* 12px */
margin-right: 0.75rem; /* 12px */
.mx-4margin-left: 1rem; /* 16px */
margin-right: 1rem; /* 16px */
.mx-5margin-left: 1.25rem; /* 20px */
margin-right: 1.25rem; /* 20px */
.mx-6margin-left: 1.5rem; /* 24px */
margin-right: 1.5rem; /* 24px */
.mx-7margin-left: 1.75rem; /* 28px */
margin-right: 1.75rem; /* 28px */
.mx-8margin-left: 2rem; /* 32px */
margin-right: 2rem; /* 32px */
.mx-9margin-left: 2.25rem; /* 36px */
margin-right: 2.25rem; /* 36px */
.mx-10margin-left: 2.5rem; /* 40px */
margin-right: 2.5rem; /* 40px */
.mx-11margin-left: 2.75rem; /* 44px */
margin-right: 2.75rem; /* 44px */
.mx-12margin-left: 3rem; /* 48px */
margin-right: 3rem; /* 48px */
.mx-14margin-left: 3.5rem; /* 56px */
margin-right: 3.5rem; /* 56px */
.mx-16margin-left: 4rem; /* 64px */
margin-right: 4rem; /* 64px */
.mx-20margin-left: 5rem; /* 80px */
margin-right: 5rem; /* 80px */
.mx-automargin-left: auto;
margin-right: auto;
.my-0margin-top: 0;
margin-bottom: 0;
.my-1margin-top: 0.25rem; /* 4px */
margin-bottom: 0.25rem; /* 4px */
.my-2margin-top: 0.5rem; /* 8px */
margin-bottom: 0.5rem; /* 8px */
.my-3margin-top: 0.75rem; /* 12px */
margin-bottom: 0.75rem; /* 12px */
.my-4margin-top: 1rem; /* 16px */
margin-bottom: 1rem; /* 16px */
.my-5margin-top: 1.25rem; /* 20px */
margin-bottom: 1.25rem; /* 20px */
.my-6margin-top: 1.5rem; /* 24px */
margin-bottom: 1.5rem; /* 24px */
.my-7margin-top: 1.75rem; /* 28px */
margin-bottom: 1.75rem; /* 28px */
.my-8margin-top: 2rem; /* 32px */
margin-bottom: 2rem; /* 32px */
.my-9margin-top: 2.25rem; /* 36px */
margin-bottom: 2.25rem; /* 36px */
.my-10margin-top: 2.5rem; /* 40px */
margin-bottom: 2.5rem; /* 40px */
.my-11margin-top: 2.75rem; /* 44px */
margin-bottom: 2.75rem; /* 44px */
.my-12margin-top: 3rem; /* 48px */
margin-bottom: 3rem; /* 48px */
.my-14margin-top: 3.5rem; /* 56px */
margin-bottom: 3.5rem; /* 56px */
.my-16margin-top: 4rem; /* 64px */
margin-bottom: 4rem; /* 64px */
.my-20margin-top: 5rem; /* 80px */
margin-bottom: 5rem; /* 80px */
.my-automargin-top: auto;
margin-bottom: auto;
.mt-0margin-top: 0;
.mt-1margin-top: 0.25rem; /* 4px */
.mt-2margin-top: 0.5rem; /* 8px */
.mt-3margin-top: 0.75rem; /* 12px */
.mt-4margin-top: 1rem; /* 16px */
.mt-5margin-top: 1.25rem; /* 20px */
.mt-6margin-top: 1.5rem; /* 24px */
.mt-7margin-top: 1.75rem; /* 28px */
.mt-8margin-top: 2rem; /* 32px */
.mt-9margin-top: 2.25rem; /* 36px */
.mt-10margin-top: 2.5rem; /* 40px */
.mt-11margin-top: 2.75rem; /* 44px */
.mt-12margin-top: 3rem; /* 48px */
.mt-14margin-top: 3.5rem; /* 56px */
.mt-16margin-top: 4rem; /* 64px */
.mt-20margin-top: 5rem; /* 80px */
.mt-automargin-top: auto;
.mb-0margin-bottom: 0;
.mb-1margin-bottom: 0.25rem; /* 4px */
.mb-2margin-bottom: 0.5rem; /* 8px */
.mb-3margin-bottom: 0.75rem; /* 12px */
.mb-4margin-bottom: 1rem; /* 16px */
.mb-5margin-bottom: 1.25rem; /* 20px */
.mb-6margin-bottom: 1.5rem; /* 24px */
.mb-7margin-bottom: 1.75rem; /* 28px */
.mb-8margin-bottom: 2rem; /* 32px */
.mb-9margin-bottom: 2.25rem; /* 36px */
.mb-10margin-bottom: 2.5rem; /* 40px */
.mb-11margin-bottom: 2.75rem; /* 44px */
.mb-12margin-bottom: 3rem; /* 48px */
.mb-14margin-bottom: 3.5rem; /* 56px */
.mb-16margin-bottom: 4rem; /* 64px */
.mb-20margin-bottom: 5rem; /* 80px */
.mb-automargin-bottom: auto;
.ms-0margin-left: 0;
.ms-1margin-left: 0.25rem; /* 4px */
.ms-2margin-left: 0.5rem; /* 8px */
.ms-3margin-left: 0.75rem; /* 12px */
.ms-4margin-left: 1rem; /* 16px */
.ms-5margin-left: 1.25rem; /* 20px */
.ms-6margin-left: 1.5rem; /* 24px */
.ms-7margin-left: 1.75rem; /* 28px */
.ms-8margin-left: 2rem; /* 32px */
.ms-9margin-left: 2.25rem; /* 36px */
.ms-10margin-left: 2.5rem; /* 40px */
.ms-11margin-left: 2.75rem; /* 44px */
.ms-12margin-left: 3rem; /* 48px */
.ms-14margin-left: 3.5rem; /* 56px */
.ms-16margin-left: 4rem; /* 64px */
.ms-20margin-left: 5rem; /* 80px */
.ms-automargin-left: auto;
.me-0margin-right: 0;
.me-1margin-right: 0.25rem; /* 4px */
.me-2margin-right: 0.5rem; /* 8px */
.me-3margin-right: 0.75rem; /* 12px */
.me-4margin-right: 1rem; /* 16px */
.me-5margin-right: 1.25rem; /* 20px */
.me-6margin-right: 1.5rem; /* 24px */
.me-7margin-right: 1.75rem; /* 28px */
.me-8margin-right: 2rem; /* 32px */
.me-9margin-right: 2.25rem; /* 36px */
.me-10margin-right: 2.5rem; /* 40px */
.me-11margin-right: 2.75rem; /* 44px */
.me-12margin-right: 3rem; /* 48px */
.me-14margin-right: 3.5rem; /* 56px */
.me-16margin-right: 4rem; /* 64px */
.me-20margin-right: 5rem; /* 80px */
.me-automargin-right: auto;

Assign responsive-friendly margin values to an element or a subset of its sides with shorthand classes. Includes support for individual properties, all properties, and vertical and horizontal properties.

Overview

The margin utilities that apply to all breakpoints, from xs to xxl, 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 xxl.

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
  • 6 - (by extended) for classes that set the margin to $spacer * 4
  • 7 - (by extended) for classes that set the margin to $spacer * 5
  • 8 - (by extended) for classes that set the margin to $spacer * 6
  • 9 - (by extended) for classes that set the margin to $spacer * 7
  • 10 - (by extended) for classes that set the margin to $spacer * 8
  • auto - for classes that set the margin to auto

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

Basic usage

Add margin to a single side

Control the margin on one side of an element using the m{t|e|b|s}-{size} utilities.

For example, mt-4 would add 1.5rem of margin to the top of an element, me-3 would add 1rem of margin to the right of an element, mb-5 would add 3rem of margin to the bottom of an element, and ms-2 would add 0.5rem of margin to the left of an element.

mt-4
me-3
mb-5
ms-2
<div class="mt-4 ...">mt-4</div>
<div class="me-3 ...">me-3</div>
<div class="mb-5 ...">mb-5</div>
<div class="ms-2 ...">ms-2</div>

Add horizontal margin

Control the horizontal margin of an element using the mx-{size} utilities.

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

Add vertical margin

Control the vertical margin of an element using the my-{size} utilities.

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

Margin to all sides

Control the margin on all sides of an element using the m-{size} utilities.

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

Horizontal centering margin

Additionally, Bootstrap also 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.

Centered element
<div class="mx-auto ...">Centered element</div>

Right align margin

Control the righ-align margin of an element using the ms-auto utilities.

01
02
03
04
05
<div class="d-flex gap-4">
  <div>01</div>
  <div>02</div>
  <div class="ms-auto">03</div>
  <div>04</div>
  <div>05</div>
</div>

Using negative values

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

The syntax is nearly the same as the default, positive margin utilities, but with the addition of n before the requested size by the format {property}{sides}-n{size}.

mt-n5
<div class="bd-w-36 bd-h-16 ..."></div>
<div class="mt-n5">mt-n5</div>

Breakpoints and media queries

All margin utilities that supports responsive at specific breakpoints, using the following format {property}{sides}-{breakpoint}-{size} for xs, sm, md, lg, xl, and xxl.

For example, use my-md-4 to apply the my-4 utility at only medium screen sizes and above, my-3 for xs by default.

my-3 my-md-4 my-lg-6
<div class="my-3 my-md-4 my-lg-6 ...">my-3 my-md-4 my-lg-6</div>