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

How it works

Assign responsive-friendly marginor paddingvalues 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 .25remto 3rem.

We have further added quick margin and padding options for by using gutter space variable .m-g, margin bottom gutter .mb-g. Padding gutter can be adding by using .p-gmodifier. The *-gwill change based on the gutter variable.

Learn more about this component on bootstrap's official documentationalso on this codepen example

Default Panel

Spacing utilities that apply to all breakpoints, from xsto xl, have no breakpoint abbreviation in them. This is because those classes are applied from min-width: 0and 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 xsand {property}{sides}-{breakpoint}-{size}for sm, md, lg, and xl.

Where propertyis one of:

  • t- for classes that set margin-topor padding-top
  • b- for classes that set margin-bottomor padding-bottom
  • l- for classes that set margin-leftor padding-left
  • r- for classes that set margin-rightor padding-right
  • x- for classes that set both *-leftand *-right
  • y- for classes that set both *-topand *-bottom
  • blank - for classes that set a marginor paddingon all 4 sides of the element

Where sizeis one of:

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

Example padding:

.p-2 .p-sm-3 .p-md-4 .p-lg-5 .p-xl-6
.p-1 .p-sm-2 .p-md-3 .p-lg-4 .p-xl-5
.p-0 .p-sm-1 .p-md-2 .p-lg-3 .p-xl-4
.p-1 .p-sm-0 .p-md-1 .p-lg-2 .p-xl-3
.p-4 .p-sm-4 .p-md-0 .p-lg-1 .p-xl-2
.p-5 .p-sm-6 .p-md-3 .p-lg-0 .p-xl-1
.p-6 .p-sm-5 .p-md-5 .p-lg-6 .p-xl-0

Horizontal centering

Additionally, Bootstrap also includes an .mx-autoclass for horizontally centering fixed-width block level content—that is, content that has display: blockand a widthset—by setting the horizontal margins to auto.

Centered element

Negative margin

The syntax is nearly the same as the default, positive margin utilities, but with the addition of nbefore the requested size.

Here’s an example of customizing the Bootstrap grid at the medium (md) breakpoint and above. We’ve increased the .colpadding with .px-md-5and then counteracted that with .mx-md-n5on the parent .row.

Custom column padding
Custom column padding

Margin examples

Margin specific viewports example
*-xs-* <576px
*-sm-* ≥576px
*-md-* ≥768px
*-lg-* ≥992px
*-xl-* ≥1200px
.ml-1 .ml-sm-2 .ml-md-3 .ml-lg-4 .ml-xl-5
.mx-1 .mx-sm-2 .mx-md-3 .mx-lg-4 .mx-xl-5
.my-1 .my-sm-2 .my-md-3 .my-lg-4 .my-xl-5
.m-1 .m-sm-2 .m-md-3 .m-lg-4 .m-xl-5