Stripped personal data from development repository
Samo Penic
2019-02-20 83c3f647c35477564b77cbc5b36d37d793d5442a
commit | author | age
83c3f6 1 ---
SP 2 layout: docs
3 title: Spacing
4 description: Bootstrap includes a wide range of shorthand responsive margin and padding utility classes to modify an element's appearance.
5 group: utilities
6 toc: true
7 ---
8
9 ## How it works
10
11 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`.
12
13 ## Notation
14
15 Spacing utilities that apply to all breakpoints, from `xs` to `xl`, 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.
16
17 The classes are named using the format `{property}{sides}-{size}` for `xs` and `{property}{sides}-{breakpoint}-{size}` for `sm`, `md`, `lg`, and `xl`.
18
19 Where *property* is one of:
20
21 * `m` - for classes that set `margin`
22 * `p` - for classes that set `padding`
23
24 Where *sides* is one of:
25
26 * `t` - for classes that set `margin-top` or `padding-top`
27 * `b` - for classes that set `margin-bottom` or `padding-bottom`
28 * `l` - for classes that set `margin-left` or `padding-left`
29 * `r` - for classes that set `margin-right` or `padding-right`
30 * `x` - for classes that set both `*-left` and `*-right`
31 * `y` - for classes that set both `*-top` and `*-bottom`
32 * blank - for classes that set a `margin` or `padding` on all 4 sides of the element
33
34 Where *size* is one of:
35
36 * `0` - for classes that eliminate the `margin` or `padding` by setting it to `0`
37 * `1` - (by default) for classes that set the `margin` or `padding` to `$spacer * .25`
38 * `2` - (by default) for classes that set the `margin` or `padding` to `$spacer * .5`
39 * `3` - (by default) for classes that set the `margin` or `padding` to `$spacer`
40 * `4` - (by default) for classes that set the `margin` or `padding` to `$spacer * 1.5`
41 * `5` - (by default) for classes that set the `margin` or `padding` to `$spacer * 3`
42 * `auto` - for classes that set the `margin` to auto
43
44 (You can add more sizes by adding entries to the `$spacers` Sass map variable.)
45
46 ## Examples
47
48 Here are some representative examples of these classes:
49
50 {% highlight scss %}
51 .mt-0 {
52   margin-top: 0 !important;
53 }
54
55 .ml-1 {
56   margin-left: ($spacer * .25) !important;
57 }
58
59 .px-2 {
60   padding-left: ($spacer * .5) !important;
61   padding-right: ($spacer * .5) !important;
62 }
63
64 .p-3 {
65   padding: $spacer !important;
66 }
67 {% endhighlight %}
68
69 ### Horizontal centering
70
71 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`.
72
73 <div class="bd-example">
74   <div class="mx-auto" style="width: 200px; background-color: rgba(86,61,124,.15);">
75     Centered element
76   </div>
77 </div>
78
79 {% highlight html %}
80 <div class="mx-auto" style="width: 200px;">
81   Centered element
82 </div>
83 {% endhighlight %}
84
85 ### Negative margin
86
87 In CSS, `margin` properties can utilize negative values (`padding` cannot). As of 4.2, we've added negative margin utilities for every non-zero integer size listed above (e.g., `1`, `2`, `3`, `4`, `5`). These utilities are ideal for customizing grid column gutters across breakpoints.
88
89 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`:
90
91 {% highlight scss %}
92 .mt-n1 {
93   margin-top: -0.25rem !important;
94 }
95 {% endhighlight %}
96
97 Here's an example of customizing the Bootstrap grid at the medium (`md`) breakpoint and above. We've increased the `.col` padding with `.px-md-5` and then counteracted that with `.mx-md-n5` on the parent `.row`.
98
99 {% capture example %}
100 <div class="row mx-md-n5">
101   <div class="col py-3 px-md-5 border bg-light">Custom column padding</div>
102   <div class="col py-3 px-md-5 border bg-light">Custom column padding</div>
103 </div>
104 {% endcapture %}
105 {% include example.html content=example %}