spacing

margin

Shorthand for setting outer spacing on all four sides of an element. Accepts lengths, percentages, or the auto keyword for horizontal centering.

margin: <top> <right> <bottom> <left> | <top> <horizontal> <bottom> | <vertical> <horizontal> | <all>

Common values

Value Effect
0 Removes all outside spacing
auto Distributes remaining space equally, useful for horizontal centering
1rem Equal 1rem margin on all sides
1rem 0 Vertical margin only
1rem 2rem Vertical then horizontal margin

Examples

.container { margin: 0 auto; max-width: 960px; }

Classic horizontally-centered container

p { margin: 0 0 1rem; }

Rhythm spacing between paragraphs

.card { margin: 1rem 2rem; }

Different vertical and horizontal margins

Gotcha

Vertical margins between block siblings collapse into the larger of the two; flex and grid items do not collapse.

Related properties

← All CSS properties · CSS selectors · Grid vs Flexbox