Spacing

Bootstrap 5 Spacing

CoatsDigital includes a wide range of shorthand responsive margin and padding utility classes to modify an element’s appearance.

How it works

Assign responsive-friendly margin or padding 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 3rem.

Notation

Spacing 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 one of:

  • m - for classes that set margin
  • p - for classes that set padding

Where sides is one of:

  • t - for classes that set margin-top or padding-top
  • b - for classes that set margin-bottom or padding-bottom
  • s - for classes that set margin-left or padding-left
  • e - for classes that set margin-right or padding-right
  • 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 or padding on all 4 sides of the element

Where size is one of:

  • 0 - for - for classes that eliminate the margin padduing
  • 1 - (by default) for classes that set the margin or padding
  • 2 - (by default) for classes that set the margin or padding
  • 3 - (by default) for classes that set the margin or padding
  • 4 - (by default) for classes that set the margin or padding
  • 5 - - (by default) for classes that set the margin or padding
  • auto - for classes that set the margin to auto

Spacing classes

Here are the representative classes:

Margin

.m-0

.m-1

.m-2

.m-3

.m-4

.m-5

.m-auto

.mt-0

.mt-1

.mt-2

.mt-3

.mt-4

.mt-5

.mt-auto

.me-0

.me-1

.me-2

.me-3

.me-4

.me-5

.me-auto

.mb-0

.mb-1

.mb-2

.mb-3

.mb-4

.mb-5

.mb-auto

.ms-0

.ms-1

.ms-2

.ms-3

.ms-4

.ms-5

.ms-auto

.mx-0

.mx-1

.mx-2

.mx-3

.mx-4

.mx-5

.mx-auto

.my-0

.my-1

.my-2

.my-3

.my-4

.my-5

.my-auto


'mt' = margin-top, 'me' = margin-right, 'mb' = margin-bottom, 'ms' = margin-left, 'mx' = margin-left and margin-right, 'my' = margin-top and margin-bottom.

Padding

.p-0

.p-1

.p-2

.p-3

.p-4

.p-5

.p-auto

.pt-0

.pt-1

.pt-2

.pt-3

.pt-4

.pt-5

.pt-auto

.pe-0

.pe-1

.pe-2

.pe-3

.pe-4

.pe-5

.pe-auto

.pb-0

.pb-1

.pb-2

.pb-3

.pb-4

.pb-5

.pb-auto

.ps-0

.ps-1

.ps-2

.ps-3

.ps-4

.ps-5

.ps-auto

.px-0

.px-1

.px-2

.px-3

.px-4

.px-5

.px-auto

.py-0

.py-1

.py-2

.py-3

.py-4

.py-5

.py-auto


'pt' = padding-top, 'pe' = padding-right, 'pb' = padding-bottom, 'ps' = padding-left, 'px' = padding-left and padding-right, 'py' = padding-top and padding-bottom.

Horizontal centering

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

                              
                                
                              
                            

Negative margin

In CSS, margin properties can utilize negative values (padding cannot).

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:

.m-n1

.m-n2

.m-n3

.m-n4

.m-n5

.mt-n1

.mt-n2

.mt-n3

.mt-n4

.mt-n5

.me-n1

.me-n2

.me-n3

.me-n4

.me-n5

.mb-n1

.mb-n2

.mb-n3

.mb-n4

.mb-n5

.ms-n1

.ms-n2

.ms-n3

.ms-n4

.ms-n5

.mx-n1

.mx-n2

.mx-n3

.mx-n4

.mx-n5

.my-n1

.my-n2

.my-n3

.my-n4

.my-n5


'mt-n' = negative-margin-top, 'me-n' = negative-margin-right, 'mb-n' = negative-margin-bottom, 'ms-n' = negative-margin-left, 'mx-n' = negative-margin-left and negative-margin-right, 'my-n' = negative-margin-top and negative-margin-bottom.

Gap

When using display: grid, you can make use of gap utilities on the parent grid container. This can save on having to add margin utilities to individual grid items (children of a display: grid container).

Grid item 1
Grid item 2
Grid item 3