Bootstrap 6 Margin

Margin

Assign responsive-friendly margin values to an element or a subset of its sides with shorthand classes.

ClassStyles
.m-0margin: var(--cui-spacer-0);
.m-1margin: var(--cui-spacer-1);
.m-2margin: var(--cui-spacer-2);
.m-3margin: var(--cui-spacer-3);
.m-4margin: var(--cui-spacer-4);
.m-5margin: var(--cui-spacer-5);
.m-6margin: var(--cui-spacer-6);
.m-7margin: var(--cui-spacer-7);
.m-8margin: var(--cui-spacer-8);
.m-9margin: var(--cui-spacer-9);
.m-10margin: var(--cui-spacer-10);
.m-11margin: var(--cui-spacer-11);
.m-12margin: var(--cui-spacer-12);
.m-automargin: auto;
.mt-0margin-top: var(--cui-spacer-0);
.mt-1margin-top: var(--cui-spacer-1);
.mt-2margin-top: var(--cui-spacer-2);
.mt-3margin-top: var(--cui-spacer-3);
.mt-4margin-top: var(--cui-spacer-4);
.mt-5margin-top: var(--cui-spacer-5);
.mt-6margin-top: var(--cui-spacer-6);
.mt-7margin-top: var(--cui-spacer-7);
.mt-8margin-top: var(--cui-spacer-8);
.mt-9margin-top: var(--cui-spacer-9);
.mt-10margin-top: var(--cui-spacer-10);
.mt-11margin-top: var(--cui-spacer-11);
.mt-12margin-top: var(--cui-spacer-12);
.mt-automargin-top: auto;
.mb-0margin-bottom: var(--cui-spacer-0);
.mb-1margin-bottom: var(--cui-spacer-1);
.mb-2margin-bottom: var(--cui-spacer-2);
.mb-3margin-bottom: var(--cui-spacer-3);
.mb-4margin-bottom: var(--cui-spacer-4);
.mb-5margin-bottom: var(--cui-spacer-5);
.mb-6margin-bottom: var(--cui-spacer-6);
.mb-7margin-bottom: var(--cui-spacer-7);
.mb-8margin-bottom: var(--cui-spacer-8);
.mb-9margin-bottom: var(--cui-spacer-9);
.mb-10margin-bottom: var(--cui-spacer-10);
.mb-11margin-bottom: var(--cui-spacer-11);
.mb-12margin-bottom: var(--cui-spacer-12);
.mb-automargin-bottom: auto;
.ms-0margin-inline-start: var(--cui-spacer-0);
.ms-1margin-inline-start: var(--cui-spacer-1);
.ms-2margin-inline-start: var(--cui-spacer-2);
.ms-3margin-inline-start: var(--cui-spacer-3);
.ms-4margin-inline-start: var(--cui-spacer-4);
.ms-5margin-inline-start: var(--cui-spacer-5);
.ms-6margin-inline-start: var(--cui-spacer-6);
.ms-7margin-inline-start: var(--cui-spacer-7);
.ms-8margin-inline-start: var(--cui-spacer-8);
.ms-9margin-inline-start: var(--cui-spacer-9);
.ms-10margin-inline-start: var(--cui-spacer-10);
.ms-11margin-inline-start: var(--cui-spacer-11);
.ms-12margin-inline-start: var(--cui-spacer-12);
.ms-automargin-inline-start: auto;
.me-0margin-inline-end: var(--cui-spacer-0);
.me-1margin-inline-end: var(--cui-spacer-1);
.me-2margin-inline-end: var(--cui-spacer-2);
.me-3margin-inline-end: var(--cui-spacer-3);
.me-4margin-inline-end: var(--cui-spacer-4);
.me-5margin-inline-end: var(--cui-spacer-5);
.me-6margin-inline-end: var(--cui-spacer-6);
.me-7margin-inline-end: var(--cui-spacer-7);
.me-8margin-inline-end: var(--cui-spacer-8);
.me-9margin-inline-end: var(--cui-spacer-9);
.me-10margin-inline-end: var(--cui-spacer-10);
.me-11margin-inline-end: var(--cui-spacer-11);
.me-12margin-inline-end: var(--cui-spacer-12);
.me-automargin-inline-end: auto;
.mx-0margin-inline: var(--cui-spacer-0);
.mx-1margin-inline: var(--cui-spacer-1);
.mx-2margin-inline: var(--cui-spacer-2);
.mx-3margin-inline: var(--cui-spacer-3);
.mx-4margin-inline: var(--cui-spacer-4);
.mx-5margin-inline: var(--cui-spacer-5);
.mx-6margin-inline: var(--cui-spacer-6);
.mx-7margin-inline: var(--cui-spacer-7);
.mx-8margin-inline: var(--cui-spacer-8);
.mx-9margin-inline: var(--cui-spacer-9);
.mx-10margin-inline: var(--cui-spacer-10);
.mx-11margin-inline: var(--cui-spacer-11);
.mx-12margin-inline: var(--cui-spacer-12);
.mx-automargin-inline: auto;
.my-0margin-block: var(--cui-spacer-0);
.my-1margin-block: var(--cui-spacer-1);
.my-2margin-block: var(--cui-spacer-2);
.my-3margin-block: var(--cui-spacer-3);
.my-4margin-block: var(--cui-spacer-4);
.my-5margin-block: var(--cui-spacer-5);
.my-6margin-block: var(--cui-spacer-6);
.my-7margin-block: var(--cui-spacer-7);
.my-8margin-block: var(--cui-spacer-8);
.my-9margin-block: var(--cui-spacer-9);
.my-10margin-block: var(--cui-spacer-10);
.my-11margin-block: var(--cui-spacer-11);
.my-12margin-block: var(--cui-spacer-12);
.my-automargin-block: auto;

Overview

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. Classes are built from a default Sass map ranging from .25rem to 8rem.

Laying out with CSS Grid or flexbox? Consider using the gap utility instead — it spaces the children from the container, so you do not have to strip the margin off the last one.

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 m{sides}-{size} for xs and m{sides}-{breakpoint}-{size} for sm, md, lg, xl, and 2xl.

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 margin-left and margin-right
  • y - for classes that set both margin-top and margin-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 * .375
  • 3 - (by default) for classes that set the margin to $spacer * .5
  • 4 - (by default) for classes that set the margin to $spacer * .75
  • 5 - (by default) for classes that set the margin to $spacer
  • 6 - (by default) for classes that set the margin to $spacer * 1.25
  • 7 - (by default) for classes that set the margin to $spacer * 1.5
  • 8 - (by default) for classes that set the margin to $spacer * 1.75
  • 9 - (by default) for classes that set the margin to $spacer * 2
  • 10 - (by default) for classes that set the margin to $spacer * 2.25
  • 11 - (by default) for classes that set the margin to $spacer * 2.5
  • 12 - (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

Here are some representative examples of these classes:

.mt-0 {
  margin-top: 0;
}

.ms-1 {
  margin-left: ($spacer * .25);
}

.my-3 {
  margin-top: ($spacer * .5);
  margin-bottom: ($spacer * .5);
}

Horizontal centering

Additionally, CoreUI for 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" style="width: 200px;">
  Centered element
</div>

Negative margin

Two negative steps ship for the inline margins, .me--1, .me--2, .ms--1 and .ms--2, for pulling an element back over its neighbour’s gap: -1 is -.25rem and -2 is -.5rem, so the keys do not mirror the positive steps (.ms-2 is .375rem). They come from the $negative-spacers map; add a key there to extend the set ("-3": calc(var(--cui-spacer) * -.75)), or set one to null to drop it. The block margins have no negative form.

scss
$negative-spacers: () !default;
// stylelint-disable-next-line scss/dollar-variable-default
$negative-spacers: defaults(
  (
    "-1": calc(var(--cui-spacer) * -.25),
    "-2": calc(var(--cui-spacer) * -.5),
  ),
  $negative-spacers
);

Customizing

Sass variables

$spacer sets the default of --cui-spacer, the step the whole scale is built from: the stops --cui-spacer-1 … -12 are calc() multiples of it on :root, the utilities read the stops, so :root { --cui-spacer: .75rem } rescales every spacing utility at run time, no recompile.

scss
$spacer: 1rem !default;

Sass maps

Each entry is one step of the scale, generating the matching .m-{key} class, and its per-side and negative variants,. Add a key to define a step, or set one to null to drop it:

scss
$spacers: () !default;
// stylelint-disable-next-line scss/dollar-variable-default
$spacers: defaults(
  (
    0: 0,
    1: calc(var(--cui-spacer) * .25),
    2: calc(var(--cui-spacer) * .375),
    3: calc(var(--cui-spacer) * .5),
    4: calc(var(--cui-spacer) * .75),
    5: var(--cui-spacer),
    6: calc(var(--cui-spacer) * 1.25),
    7: calc(var(--cui-spacer) * 1.5),
    8: calc(var(--cui-spacer) * 1.75),
    9: calc(var(--cui-spacer) * 2),
    10: calc(var(--cui-spacer) * 2.25),
    11: calc(var(--cui-spacer) * 2.5),
    12: calc(var(--cui-spacer) * 3),
  ),
  $spacers
);

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($utilities-spacers, (auto: auto))
),
"margin-x": (
  responsive: true,
  property: margin-inline,
  class: mx,
  values: map.merge($utilities-spacers, (auto: auto))
),
"margin-y": (
  responsive: true,
  property: margin-block,
  class: my,
  values: map.merge($utilities-spacers, (auto: auto))
),
"margin-top": (
  responsive: true,
  property: margin-top,
  class: mt,
  values: map.merge($utilities-spacers, (auto: auto))
),
"margin-end": (
  responsive: true,
  property: margin-inline-end,
  class: me,
  values: map-merge-multiple($utilities-spacers, $negative-spacers, (auto: auto)),
),
"margin-bottom": (
  responsive: true,
  property: margin-bottom,
  class: mb,
  values: map.merge($utilities-spacers, (auto: auto))
),
"margin-start": (
  responsive: true,
  property: margin-inline-start,
  class: ms,
  values: map-merge-multiple($utilities-spacers, $negative-spacers, (auto: auto)),
),
// Padding utilities