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 setmargin
p
- for classes that setpadding
Where sides is one of:
t
- for classes that setmargin-top
orpadding-top
b
- for classes that setmargin-bottom
orpadding-bottom
s
- (start) for classes that setmargin-left
orpadding-left
in LTR,margin-right
orpadding-right
in RTLe
- (end) for classes that setmargin-right
orpadding-right
in LTR,margin-left
orpadding-left
in RTLx
- for classes that set both*-left
and*-right
y
- for classes that set both*-top
and*-bottom
- blank - for classes that set a
margin
orpadding
on all 4 sides of the element
Where size is one of:
none
- for classes that eliminate themargin
orpadding
3xs
- for classes that set themargin
orpadding
2xs
- for classes that set themargin
orpadding
xs
- for classes that set themargin
orpadding
sm
- for classes that set themargin
orpadding
md
- for classes that set themargin
orpadding
lg
- for classes that set themargin
orpadding
xl
- for classes that set themargin
orpadding
2xl
- for classes that set themargin
orpadding
3xl
- for classes that set themargin
orpadding
4xl
- for classes that set themargin
orpadding
5xl
- for classes that set themargin
orpadding
auto
- for classes that set themargin
to 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 setmargin
p
- for classes that setpadding
Where sides is one of:
t
- for classes that setmargin-top
orpadding-top
b
- for classes that setmargin-bottom
orpadding-bottom
s
- (start) for classes that setmargin-left
orpadding-left
in LTR,margin-right
orpadding-right
in RTLe
- (end) for classes that setmargin-right
orpadding-right
in LTR,margin-left
orpadding-left
in RTLx
- for classes that set both*-left
and*-right
y
- for classes that set both*-top
and*-bottom
- blank - for classes that set a
margin
orpadding
on all 4 sides of the element
Where size is one of:
0
- for classes that eliminate themargin
orpadding
by setting it to0
1
- (by default) for classes that set themargin
orpadding
to$spacer * .25
2
- (by default) for classes that set themargin
orpadding
to$spacer * .75
3
- (by default) for classes that set themargin
orpadding
to$spacer * 1.5
4
- (by default) for classes that set themargin
orpadding
to$spacer * 2.5
5
- (by default) for classes that set themargin
orpadding
to$spacer * 3.5
auto
- for classes that set themargin
to 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 setmargin
p
- for classes that setpadding
Where sides is one of:
t
- for classes that setmargin-top
orpadding-top
b
- for classes that setmargin-bottom
orpadding-bottom
s
- (start) for classes that setmargin-left
orpadding-left
in LTR,margin-right
orpadding-right
in RTLe
- (end) for classes that setmargin-right
orpadding-right
in LTR,margin-left
orpadding-left
in RTLx
- for classes that set both*-left
and*-right
y
- for classes that set both*-top
and*-bottom
- blank - for classes that set a
margin
orpadding
on all 4 sides of the element
Where size is one of:
3xs
- for classes that set themargin
orpadding
2xs
- for classes that set themargin
orpadding
xs
- for classes that set themargin
orpadding
sm
- for classes that set themargin
orpadding
md
- for classes that set themargin
orpadding
lg
- for classes that set themargin
orpadding
xl
- for classes that set themargin
orpadding
2xl
- for classes that set themargin
orpadding
3xl
- for classes that set themargin
orpadding
(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>