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

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
  • 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.)

Here are some representative examples of these classes:

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.

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

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.

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.

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

Spacing utilities are declared in our utilities API in scss/_utilities.scss.

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