margin

Sets the outer spacing of an element. Control each direction individually with directional prefixes (mt, mr, mb, ml), or set all at once with m. Use mxa for horizontal center alignment.

Class List

Append a value with unit after the prefix. Use px for values under 20px, and rem (1rem = 10px) for 20px and above.

PrefixCSS PropertyExampleCSS Output
mmarginm16pxmargin: 16px
mmarginm2remmargin: 2rem (20px)
mtmargin-topmt8pxmargin-top: 8px
mrmargin-rightmr16pxmargin-right: 16px
mbmargin-bottommb2-4remmargin-bottom: 2.4rem (24px)
mlmargin-leftml4remmargin-left: 4rem (40px)
mmargin (% 단위)m5pmargin: 5%
mtmargin-top (em)mt1emmargin-top: 1em

Auto Values

Directional auto values and mxa which applies auto to both left and right simultaneously.

ClassCSSDescription
mtamargin-top: autopush down in flex container
mramargin-right: autopush element to the left
mbamargin-bottom: autopush up in flex container
mlamargin-left: autopush element to the right
mxamargin-left: auto; margin-right: autohorizontal center alignment (most commonly used)

Unit Comparison

Examples of the same spacing expressed in px, rem, %, and em. Based on: html { font-size: 10px }

<tr> <th class="tal py12px px16px bg18181B bb2pxsolid27272A cA1A1AA fw600 ttu fs12px ls0-05em">spacingpxrem%em
8pxm8pxm0-8remm1pm0-8em
12pxm12pxm1-2remm2pm1-2em
16pxm16pxm1-6remm3pm1-6em
20pxm20pxm2remm5pm2em
32pxm32pxm3-2remm8pm3-2em
40pxm40pxm4remm10pm4em

Visual Comparison

See how the outer spacing of an element changes depending on the margin value.

margin: 0 (default) —

Box

no margin; flush with parent

margin: 8px — m8px

Box

8px spacing on all sides

margin: 16px — m16px

Box

16px spacing on all sides

margin-top: 2rem (20px) — mt2rem

Box

20px spacing on top only

margin-left: 4rem (40px) — ml4rem

Box

40px spacing on left only

mxa — Horizontal Center Alignment

mxa is shorthand for margin-left: auto; margin-right: auto, the most commonly used pattern for horizontally centering block elements. A width must be set for it to work.

<!-- Page container (most common pattern) -->
<div class="mxa maxw120rem px2rem">
  <h1>Page content</h1>
  <p>Max width 1200px, center-aligned with auto margin</p>
</div>

<!-- Center-aligned card -->
<div class="mxa maxw40rem bg18181B p2rem br8px">
  <h2>Center-aligned card</h2>
  <p>maxw + mxa Combination</p>
</div>

<!-- Center alignment at various widths -->
<div class="mxa maxw60rem mb2rem">Wide container</div>
<div class="mxa maxw40rem mb2rem">Medium container</div>
<div class="mxa maxw24rem">Narrow container</div>

mxa in action

maxw40rem mxa (가운데 정렬)
maxw24rem mxa
maxw16rem mxa

Directional margin

Control each direction individually with mt, mr, mb, and ml.

<!-- Section gap -->
<section class="mt4rem mb4rem">
  <h2 class="mb16px">Section Title</h2>
  <p class="mb8px">First paragraph</p>
  <p>Second paragraph</p>
</section>

<!-- Push element right (flex container) -->
<div class="df aic">
  <span>Logo</span>
  <nav class="mla">Pushed right (margin-left: auto)</nav>
</div>

<!-- List indentation -->
<ul class="ml2-4rem">
  <li class="mb8px">Item 1</li>
  <li class="mb8px">Item 2</li>
  <li>Item 3</li>
</ul>

Directional margin visualization

mt2rem (top 20px)

mt2rem

mb2rem (bottom 20px)

mb2rem

ml2rem (left 20px)

ml2rem

mr2rem (right 20px)

mr2rem
next

Negative Margin (neg- prefix)

Add the neg- prefix to apply negative margin. Used to pull elements in the opposite direction or create overlap effects.

<!-- Full width by canceling parent padding -->
<div class="p2rem">
  <div class="neg-ml2rem neg-mr2rem bg6366F1 p16px">
    Cancels parent left/right padding with negative margin
  </div>
</div>

<!-- Overlap effect (avatar stack) -->
<div class="df">
  <div class="w4rem h4rem br50p bg6366F1 b2pxsolidFFFFFF"></div>
  <div class="w4rem h4rem br50p bg34D399 b2pxsolidFFFFFF neg-ml12px"></div>
  <div class="w4rem h4rem br50p bgFBBF24 b2pxsolidFFFFFF neg-ml12px"></div>
</div>
ClassCSSDescription
neg-mt10pxmargin-top: -10pxpull up 10px
neg-mt2remmargin-top: -2rempull up 20px
neg-ml16pxmargin-left: -16pxpull left 16px
neg-mb8pxmargin-bottom: -8pxoverlap bottom element by 8px
neg-mr12pxmargin-right: -12pxoverlap right element by 12px

Caution

Be careful with negative margins as elements may overlap. Especially when using neg-mt, elements may overflow outside their bounds if the parent lacks oh (overflow: hidden).

Tips & Notes

Use multiples of 4

For consistent rhythm, use multiples of 4 for margin values: 4px, 8px, 12px, 16px, 2rem(20px), 2-4rem(24px), 3-2rem(32px), 4rem(40px)

Horizontal center with mxa

To center a block element, combine mxa with maxw. The most common pattern for page containers: mxa maxw120rem px2rem

Beware of Margin Collapse

Vertical margins on block elements collapse. For example, if an element with mb2rem is followed by one with mt2rem, the gap is 20px, not 40px. Collapse does not occur inside flex/grid containers, so using gap is recommended.

Shorthand decomposition: m (margin) is internally decomposed into margin-top/right/bottom/left in all 4 directions. When using m2rem mt0, only margin-top is precisely overridden to 0. Multi-values are also decomposed following CSS shorthand rules: m10px-20px (2 values) → top/bottom 10px, right/left 20px, m10px-20px-30px (3 values) → top 10px, right/left 20px, bottom 30px, m10px-20px-30px-40px (4 values) → top 10px, right 20px, bottom 30px, left 40px.