Margin and padding

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.

Using the CSS Grid layout module? Consider using the gap utility.

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 - (start) for classes that set margin-left or padding-left in LTR, margin-right or padding-right in RTL
  • e - (end) for classes that set margin-right or padding-right in LTR, margin-left or padding-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 or padding on all 4 sides of the element

Where size is one of:

  • 0 - for classes that eliminate the margin or padding by setting it to 0
  • 1 - (by default) for classes that set the margin or padding to $spacer * .25
  • 2 - (by default) for classes that set the margin or padding to $spacer * .5
  • 3 - (by default) for classes that set the margin or padding to $spacer
  • 4 - (by default) for classes that set the margin or padding to $spacer * 1.5
  • 5 - (by default) for classes that set the margin or padding 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:

  1. .mt-0 {
  2. margin-top: 0 !important;
  3. }
  4. .ms-1 {
  5. margin-left: ($spacer * .25) !important;
  6. }
  7. .px-2 {
  8. padding-left: ($spacer * .5) !important;
  9. padding-right: ($spacer * .5) !important;
  10. }
  11. .p-3 {
  12. padding: $spacer !important;
  13. }

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.

Spacing - 图1

  1. <div class="mx-auto" style="width: 200px;">
  2. Centered element
  3. </div>

Negative margin

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

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:

  1. .mt-n1 {
  2. margin-top: -0.25rem !important;
  3. }

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). Gap utilities are responsive by default, and are generated via our utilities API, based on the $spacers Sass map.

Spacing - 图2

  1. <div class="d-grid gap-3">
  2. <div class="p-2 bg-light border">Grid item 1</div>
  3. <div class="p-2 bg-light border">Grid item 2</div>
  4. <div class="p-2 bg-light border">Grid item 3</div>
  5. </div>

Support includes responsive options for all of Bootstrap’s grid breakpoints, as well as six sizes from the $spacers map (05). There is no .gap-auto utility class as it’s effectively the same as .gap-0.

Sass

Maps

Spacing utilities are declared via Sass map and then generated with our utilities API.

  1. $spacer: 1rem;
  2. $spacers: (
  3. 0: 0,
  4. 1: $spacer / 4,
  5. 2: $spacer / 2,
  6. 3: $spacer,
  7. 4: $spacer * 1.5,
  8. 5: $spacer * 3,
  9. );
  10. $negative-spacers: if($enable-negative-margins, negativify-map($spacers), null);

Utilities API

Spacing utilities are declared in our utilities API in scss/_utilities.scss. Learn how to use the utilities API.

  1. "margin": (
  2. responsive: true,
  3. property: margin,
  4. class: m,
  5. values: map-merge($spacers, (auto: auto))
  6. ),
  7. "margin-x": (
  8. responsive: true,
  9. property: margin-right margin-left,
  10. class: mx,
  11. values: map-merge($spacers, (auto: auto))
  12. ),
  13. "margin-y": (
  14. responsive: true,
  15. property: margin-top margin-bottom,
  16. class: my,
  17. values: map-merge($spacers, (auto: auto))
  18. ),
  19. "margin-top": (
  20. responsive: true,
  21. property: margin-top,
  22. class: mt,
  23. values: map-merge($spacers, (auto: auto))
  24. ),
  25. "margin-end": (
  26. responsive: true,
  27. property: margin-right,
  28. class: me,
  29. values: map-merge($spacers, (auto: auto))
  30. ),
  31. "margin-bottom": (
  32. responsive: true,
  33. property: margin-bottom,
  34. class: mb,
  35. values: map-merge($spacers, (auto: auto))
  36. ),
  37. "margin-start": (
  38. responsive: true,
  39. property: margin-left,
  40. class: ms,
  41. values: map-merge($spacers, (auto: auto))
  42. ),
  43. // Negative margin utilities
  44. "negative-margin": (
  45. responsive: true,
  46. property: margin,
  47. class: m,
  48. values: $negative-spacers
  49. ),
  50. "negative-margin-x": (
  51. responsive: true,
  52. property: margin-right margin-left,
  53. class: mx,
  54. values: $negative-spacers
  55. ),
  56. "negative-margin-y": (
  57. responsive: true,
  58. property: margin-top margin-bottom,
  59. class: my,
  60. values: $negative-spacers
  61. ),
  62. "negative-margin-top": (
  63. responsive: true,
  64. property: margin-top,
  65. class: mt,
  66. values: $negative-spacers
  67. ),
  68. "negative-margin-end": (
  69. responsive: true,
  70. property: margin-right,
  71. class: me,
  72. values: $negative-spacers
  73. ),
  74. "negative-margin-bottom": (
  75. responsive: true,
  76. property: margin-bottom,
  77. class: mb,
  78. values: $negative-spacers
  79. ),
  80. "negative-margin-start": (
  81. responsive: true,
  82. property: margin-left,
  83. class: ms,
  84. values: $negative-spacers
  85. ),
  86. // Padding utilities
  87. "padding": (
  88. responsive: true,
  89. property: padding,
  90. class: p,
  91. values: $spacers
  92. ),
  93. "padding-x": (
  94. responsive: true,
  95. property: padding-right padding-left,
  96. class: px,
  97. values: $spacers
  98. ),
  99. "padding-y": (
  100. responsive: true,
  101. property: padding-top padding-bottom,
  102. class: py,
  103. values: $spacers
  104. ),
  105. "padding-top": (
  106. responsive: true,
  107. property: padding-top,
  108. class: pt,
  109. values: $spacers
  110. ),
  111. "padding-end": (
  112. responsive: true,
  113. property: padding-right,
  114. class: pe,
  115. values: $spacers
  116. ),
  117. "padding-bottom": (
  118. responsive: true,
  119. property: padding-bottom,
  120. class: pb,
  121. values: $spacers
  122. ),
  123. "padding-start": (
  124. responsive: true,
  125. property: padding-left,
  126. class: ps,
  127. values: $spacers
  128. ),