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 jumbo. 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 eliminate themarginorpadding3xs- for classes that set themarginorpadding2xs- for classes that set themarginorpaddingxs- for classes that set themarginorpaddingsm- for classes that set themarginorpaddingmd- for classes that set themarginorpaddinglg- for classes that set themarginorpaddingxl- for classes that set themarginorpadding2xl- for classes that set themarginorpadding3xl- for classes that set themarginorpadding4xl- for classes that set themarginorpadding5xl- for classes that set themarginorpaddingauto- for classes that set themarginto auto
(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$spacer * .252- (by default) for classes that set themarginorpaddingto$spacer * .753- (by default) for classes that set themarginorpaddingto$spacer * 1.54- (by default) for classes that set themarginorpaddingto$spacer * 2.55- (by default) for classes that set themarginorpaddingto$spacer * 3.5auto- 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:
3xs- for classes that set themarginorpadding2xs- for classes that set themarginorpaddingxs- for classes that set themarginorpaddingsm- for classes that set themarginorpaddingmd- for classes that set themarginorpaddinglg- for classes that set themarginorpaddingxl- for classes that set themarginorpadding2xl- for classes that set themarginorpadding3xl- for classes that set themarginorpadding
(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-lg {
margin-left: var(--bs-space-scaled-lg) !important;
}
.px-scaled-2xs {
padding-right: var(--bs-space-scaled-2xs) !important;
padding-left: var(--bs-space-scaled-2xs) !important;
}
.p-2xl {
padding: $ouds-space-fixed-tall !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-lg">Tall scaled x margin</div>
<div class="mx-lg">Tall x margin (fixed)</div>
<div class="mx-lg mx-md-xl mx-xl-2xl">Tall 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-sm" 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-sm and .ms-scaled-lg:
.mt-nsm {
margin-top: -$ouds-space-fixed-sm !important;
}
.ms-scaled-nlg {
margin-top: calc(-1 * var(--bs-space-scaled-lg)) !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-lg">
<div class="p-sm">Grid item 1</div>
<div class="p-sm">Grid item 2</div>
<div class="p-sm">Grid item 3</div>
<div class="p-sm">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–jumbo). 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-lg">
<div class="p-sm">Grid item 1</div>
<div class="p-sm">Grid item 2</div>
<div class="p-sm">Grid item 3</div>
<div class="p-sm">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-lg">
<div class="p-sm">Grid item 1</div>
<div class="p-sm">Grid item 2</div>
<div class="p-sm">Grid item 3</div>
<div class="p-sm">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-md">
<div class="p-sm">Grid item 1</div>
<div class="p-sm">Grid item 2</div>
<div class="p-sm">Grid item 3</div>
<div class="p-sm">Grid item 4</div>
</div>