Spacing
OUDS Web includes a wide range of shorthand responsive margin, padding, and gap utility classes to modify an element’s appearance.
On this page
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 none to 5xlarge. Classes include fixed values on zoom (using px).
Using the CSS Grid layout module? Consider using the gap utility instead.
Notation for fixed values
Spacing utilities that apply to all breakpoints, from 2xs to 3xl, 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 2xs and {property}{sides}-{breakpoint}-{size} for xs, sm, md, lg, xl, 2xl, and 3xl.
Where property is one of:
m- for classes that setmarginp- for classes that setpadding
Where sides is one of:
t- for classes that setmargin-toporpadding-topb- for classes that setmargin-bottomorpadding-bottoms- (start) for classes that setmargin-leftorpadding-leftin LTR,margin-rightorpadding-rightin RTLe- (end) for classes that setmargin-rightorpadding-rightin LTR,margin-leftorpadding-leftin RTLx- for classes that set both*-leftand*-righty- for classes that set both*-topand*-bottom- blank - for classes that set a
marginorpaddingon all 4 sides of the element
Where size is one of:
none- for classes that set themarginorpaddingto0px3xsmall- for classes that set themarginorpaddingto2px2xsmall- for classes that set themarginorpaddingto4pxxsmall- for classes that set themarginorpaddingto8pxsmall- for classes that set themarginorpaddingto12pxmedium- for classes that set themarginorpaddingto16pxlarge- for classes that set themarginorpaddingto24pxxlarge- for classes that set themarginorpaddingto32px2xlarge- for classes that set themarginorpaddingto40px3xlarge- for classes that set themarginorpaddingto48px4xlarge- for classes that set themarginorpaddingto56px5xlarge- for classes that set themarginorpaddingto64pxauto- for classes that set themargintoauto
(You can but shouldn’t add more sizes by adding entries to the $ouds-dimension-space-fixed Sass map variable.)
Bootstrap
$enable-bootstrap-compatibility: true
This part is enabled only when $enable-bootstrap-compatibility is set to true. Read more
about Bootstrap compatibility.
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 setmarginp- for classes that setpadding
Where sides is one of:
t- for classes that setmargin-toporpadding-topb- for classes that setmargin-bottomorpadding-bottoms- (start) for classes that setmargin-leftorpadding-leftin LTR,margin-rightorpadding-rightin RTLe- (end) for classes that setmargin-rightorpadding-rightin LTR,margin-leftorpadding-leftin RTLx- for classes that set both*-leftand*-righty- for classes that set both*-topand*-bottom- blank - for classes that set a
marginorpaddingon all 4 sides of the element
Where size is one of:
0- for classes that eliminate themarginorpaddingby setting it to01- (by default) for classes that set themarginorpaddingto.25rem2- (by default) for classes that set themarginorpaddingto.75rem3- (by default) for classes that set themarginorpaddingto1.5rem4- (by default) for classes that set themarginorpaddingto2.5rem5- (by default) for classes that set themarginorpaddingto3.5remauto- for classes that set themarginto auto
(You can add more sizes by adding entries to the $spacers Sass map variable.)
Notation for scaled values
Spacing utilities that evolve across all breakpoints, from 2xs to 3xl, have no breakpoint abbreviation in them since they are auto-sufficient from a responsive point of view.
The classes are named using the format {property}{sides}-scaled-{size}.
Where property is one of:
m- for classes that setmarginp- for classes that setpadding
Where sides is one of:
t- for classes that setmargin-toporpadding-topb- for classes that setmargin-bottomorpadding-bottoms- (start) for classes that setmargin-leftorpadding-leftin LTR,margin-rightorpadding-rightin RTLe- (end) for classes that setmargin-rightorpadding-rightin LTR,margin-leftorpadding-leftin RTLx- for classes that set both*-leftand*-righty- for classes that set both*-topand*-bottom- blank - for classes that set a
marginorpaddingon all 4 sides of the element
Where size is one of:
| Size name | Size for mobile (2xs to sm) | Size for tablet (md to lg) | Size for desktop (xl and upper) |
|---|---|---|---|
none | 0px | 0px | 0px |
3xsmall | 2px | 4px | 4px |
2xsmall | 4px | 8px | 8px |
xsmall | 8px | 12px | 16px |
small | 12px | 16px | 24px |
medium | 16px | 24px | 32px |
large | 24px | 32px | 40px |
xlarge | 32px | 40px | 48px |
2xlarge | 40px | 48px | 56px |
3xlarge | 48px | 56px | 64px |
(You can but shouldn’t add more sizes by adding entries to the $ouds-dimension-space-scaled Sass map variable.)
Examples
Here are some representative examples of these classes:
.mt-none {
margin-top: $ouds-space-fixed-none !important;
}
.ms-scaled-large {
margin-left: var(--bs-space-scaled-large) !important;
}
.px-scaled-2xsmall {
padding-right: var(--bs-space-scaled-2xsmall) !important;
padding-left: var(--bs-space-scaled-2xsmall) !important;
}
.p-2xlarge {
padding: $ouds-space-fixed-2xlarge !important;
}
Bootstrap
$enable-bootstrap-compatibility: true
This part is enabled only when $enable-bootstrap-compatibility is set to true. Read more
about Bootstrap compatibility.
.mt-0 {
margin-top: 0 !important;
}
.ms-1 {
margin-left: ($spacer * .25) !important;
}
.px-2 {
padding-left: ($spacer * .5) !important;
padding-right: ($spacer * .5) !important;
}
.p-3 {
padding: $spacer !important;
}
The first two examples show the scaled and fixed margin classes in action. The last one shows how to recreate an equivalent of .mx-scaled-lg by combining other responsive margin classes.
Please note that if you mix the scaled and fixed classes, the fixed classes will override the scaled ones.
<div class="mx-scaled-large">Large scaled x margin</div>
<div class="mx-large">Large x margin (fixed)</div>
<div class="mx-large mx-md-xlarge mx-xl-2xlarge">Large scaled x margin (manually)</div> Horizontal centering
Additionally, OUDS Web 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.
<div class="mx-auto p-small" style="width: 200px;">
Centered element
</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 are some example classes that are the opposite of .mt-small and .ms-scaled-large:
.mt-nsmall {
margin-top: -$ouds-space-fixed-small !important;
}
.ms-scaled-nlarge {
margin-top: calc(-1 * var(--bs-space-scaled-large)) !important;
}
Bootstrap
$enable-bootstrap-compatibility: true
This part is enabled only when $enable-bootstrap-compatibility is set to true. Read more
about Bootstrap compatibility.
Here’s an example class that’s the opposite of .mt-1:
.mt-n1 {
margin-top: -0.25rem !important;
}
Gap
When using display: grid or display: flex, you can make use of gap utilities on the parent element. This can save on having to add margin utilities to individual children of a grid or flex container. Gap utilities are responsive by default, and are generated via our utilities API, based on the $ouds-dimension-space-fixed Sass map.
<div style="grid-template-columns: 1fr 1fr;" class="d-grid gap-large">
<div class="p-small">Grid item 1</div>
<div class="p-small">Grid item 2</div>
<div class="p-small">Grid item 3</div>
<div class="p-small">Grid item 4</div>
</div> Support includes responsive options for all of OUDS Web’s grid breakpoints, as well as sizes from the $ouds-dimension-space-fixed map (none–5xlarge). There is no .gap-auto utility class as it’s effectively the same as .gap-none.
Bootstrap
$enable-bootstrap-compatibility: true
This part is enabled only when $enable-bootstrap-compatibility is set to true. Read more
about Bootstrap compatibility.
Gap utilities are responsive by default, and are generated via our utilities API, based on the $spacers Sass map.
<div style="grid-template-columns: 1fr 1fr;" class="d-grid gap-3">
<div class="p-2">Grid item 1</div>
<div class="p-2">Grid item 2</div>
<div class="p-2">Grid item 3</div>
<div class="p-2">Grid item 4</div>
</div> Support includes responsive options for all of OUDS Web’s grid breakpoints, as well as six sizes from the $spacers map (0–5). There is no .gap-auto utility class as it’s effectively the same as .gap-0.
row-gap
row-gap sets the vertical space between children items in the specified container.
<div style="grid-template-columns: 1fr 1fr;" class="d-grid row-gap-large">
<div class="p-small">Grid item 1</div>
<div class="p-small">Grid item 2</div>
<div class="p-small">Grid item 3</div>
<div class="p-small">Grid item 4</div>
</div>
Bootstrap
$enable-bootstrap-compatibility: true
This part is enabled only when $enable-bootstrap-compatibility is set to true. Read more
about Bootstrap compatibility.
<div style="grid-template-columns: 1fr 1fr;" class="d-grid row-gap-3">
<div class="p-2">Grid item 1</div>
<div class="p-2">Grid item 2</div>
<div class="p-2">Grid item 3</div>
<div class="p-2">Grid item 4</div>
</div> column-gap
column-gap sets the horizontal space between children items in the specified container.
<div style="grid-template-columns: 1fr 1fr;" class="d-grid column-gap-large">
<div class="p-small">Grid item 1</div>
<div class="p-small">Grid item 2</div>
<div class="p-small">Grid item 3</div>
<div class="p-small">Grid item 4</div>
</div>
Bootstrap
$enable-bootstrap-compatibility: true
This part is enabled only when $enable-bootstrap-compatibility is set to true. Read more
about Bootstrap compatibility.
<div style="grid-template-columns: 1fr 1fr;" class="d-grid column-gap-3">
<div class="p-2">Grid item 1</div>
<div class="p-2">Grid item 2</div>
<div class="p-2">Grid item 3</div>
<div class="p-2">Grid item 4</div>
</div> Scaled gap
We also provide scaled versions of the gap (including row-gap and column-gap). These are generated via our Utilities API, based on the $ouds-dimension-space-scaled Sass map.
<div style="grid-template-columns: 1fr 1fr;" class="d-grid gap-scaled-medium">
<div class="p-small">Grid item 1</div>
<div class="p-small">Grid item 2</div>
<div class="p-small">Grid item 3</div>
<div class="p-small">Grid item 4</div>
</div>