Immutable margin utilities are based on a global white space scale defined with custom properties. These can dramatically help reduce the size of large stylesheets and allow for greater flexibility and quicker iteration when designing in the browser.
Naming Convention
Due to the ubiquitous nature of setting margin, these utilities use a shorthand naming convention.
Shorthand | Description |
---|---|
m | Margin |
t | Top |
r | Right |
b | Bottom |
l | Left |
x | X-axis (left and right) |
y | Y-axis (top and bottom) |
n | Negative |
0 | 0 reset |
1 | --space-1 (default .5rem) |
2 | --space-2 (default 1rem) |
3 | --space-3 (default 2rem) |
4 | --space-4 (default 4rem) |
Change or reset default margins using the white space scale. Negative x-axis margins are used to offset margins and padding of child elements. Margin auto is used to horizontally center block-level elements with a set width.
Resetting Margins
To increase information density and to better align elements, remove default margins from typographic elements using the margin utilities.
No marginNo margin topNo margin bottom
Add Spacing
Add spacing around elements using a combination of margin utilities.
Hamburger Hamburger Hamburger
The negative margin utilities also work with padded children.
Padded div
Center Elements
Block elements with a set width can be centered with .mx-auto
.
Space Flexbox Items
The .ml-auto
, .mr-auto
, .mt-auto
, and .mb-auto
utilities can be used to add spacing around flexbox items.
Hamburger Hot Dog Hamburger Hot Dog Hamburger Hot Dog
Margins should never be declared outside of these utilities. This is meant to help create consistency and avoid magic numbers. If, for some reason, the default white space scale does not suit your design, customize and extend it before using it.