Margin
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.
Quick reference
Class | Properties |
---|---|
.m-0 | margin: 0; |
.m-1 | margin: 0.25rem; |
.m-2 | margin: 0.5rem; |
.m-3 | margin: 0.75rem; |
.m-4 | margin: 1rem; |
.m-5 | margin: 1.5rem; |
.m-6 | margin: 3rem; |
.m-7 | margin: 6rem; |
.m-auto | margin: auto; |
.mx-0 | margin-left: 0; margin-right: 0; |
.mx-1 | margin-left: 0.25rem; margin-right: 0.25rem; |
.mx-2 | margin-left: 0.5rem; margin-right: 0.5rem; |
.mx-3 | margin-left: 0.75rem; margin-right: 0.75rem; |
.mx-4 | margin-left: 1rem; margin-right: 1rem; |
.mx-5 | margin-left: 1.5rem; margin-right: 1.5rem; |
.mx-6 | margin-left: 3rem; margin-right: 3rem; |
.mx-7 | margin-left: 6rem; margin-right: 6rem; |
.mx-auto | margin-left: auto; margin-right: auto; |
.my-0 | margin-top: 0; margin-bottom: 0; |
.my-1 | margin-top: 0.25rem; margin-bottom: 0.25rem; |
.my-2 | margin-top: 0.5rem; margin-bottom: 0.5rem; |
.my-3 | margin-top: 0.75rem; margin-bottom: 0.75rem; |
.my-4 | margin-top: 1rem; margin-bottom: 1rem; |
.my-5 | margin-top: 1.5rem; margin-bottom: 1.5rem; |
.my-6 | margin-top: 3rem; margin-bottom: 3rem; |
.my-7 | margin-top: 6rem; margin-bottom: 6rem; |
.my-auto | margin-top: auto; margin-bottom: auto; |
.mt-0 | margin-top: 0; |
.mt-1 | margin-top: 0.25rem; |
.mt-2 | margin-top: 0.5rem; |
.mt-3 | margin-top: 0.75rem; |
.mt-4 | margin-top: 1rem; |
.mt-5 | margin-top: 1.5rem; |
.mt-6 | margin-top: 3rem; |
.mt-7 | margin-top: 6rem; |
.mt-auto | margin-top: auto; |
.mb-0 | margin-bottom: 0; |
.mb-1 | margin-bottom: 0.25rem; |
.mb-2 | margin-bottom: 0.5rem; |
.mb-3 | margin-bottom: 0.75rem; |
.mb-4 | margin-bottom: 1rem; |
.mb-5 | margin-bottom: 1.5rem; |
.mb-6 | margin-bottom: 3rem; |
.mb-7 | margin-bottom: 6rem; |
.mb-auto | margin-bottom: auto; |
.ms-0 | margin-left: 0; |
.ms-1 | margin-left: 0.25rem; |
.ms-2 | margin-left: 0.5rem; |
.ms-3 | margin-left: 0.75rem; |
.ms-4 | margin-left: 1rem; |
.ms-5 | margin-left: 1.5rem; |
.ms-6 | margin-left: 3rem; |
.ms-7 | margin-left: 6rem; |
.ms-auto | margin-left: auto; |
.me-0 | margin-right: 0; |
.me-1 | margin-right: 0.25rem; |
.me-2 | margin-right: 0.5rem; |
.me-3 | margin-right: 0.75rem; |
.me-4 | margin-right: 1rem; |
.me-5 | margin-right: 1.5rem; |
.me-6 | margin-right: 3rem; |
.me-7 | margin-right: 6rem; |
.me-auto | margin-right: auto; |
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 setmargin-top
b
- for classes that setmargin-bottom
s
- (start) for classes that setmargin-left
in LTR,margin-right
in RTLe
- (end) for classes that setmargin-right
in LTR,margin-left
in RTLx
- 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 themargin
by setting it to0
1
- (by default) for classes that set themargin
to$spacer * .25
2
- (by default) for classes that set themargin
to$spacer * .5
3
- (by default) for classes that set themargin
to$spacer
4
- (by default) for classes that set themargin
to$spacer * 1.5
5
- (by default) for classes that set themargin
to$spacer * 3
6
- (by extended) for classes that set themargin
to$spacer * 4
7
- (by extended) for classes that set themargin
to$spacer * 5
8
- (by extended) for classes that set themargin
to$spacer * 6
9
- (by extended) for classes that set themargin
to$spacer * 7
10
- (by extended) for classes that set themargin
to$spacer * 8
auto
- for classes that set themargin
to auto
(You can add more sizes by adding entries to the $spacers
Sass map variable.)
Basic examples
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.
<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.
<div class="mx-5 ...">mx-5</div>
Add vertical margin
Control the vertical margin of an element using the my-{size}
utilities.
<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.
<div class="m-4 ...">m-4</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
.
<div class="mx-auto" style="width: 200px;">
Centered element
</div>
Right align margin
Control the righ-align margin of an element using the ms-auto
utilities.
<nav class="navbar navbar-expand-lg">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="/images/bootstrap-logo.svg" width="36" alt="Logo" />
</a>
<button class="btn btn-primary ms-auto">Sign up</button>
</div>
</nav>
Using negative values
In CSS, margin
properties can utilize negative values (padding
cannot). These negative margins are enabled by default, 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}
.
<div class="d-inline-block"></div>
<div class="mt-n2 d-inline-block">mt-n2</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.
<div class="my-3 my-md-4 ...">my-3 my-md-4</div>