CSS margin Property

Beginner
6 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The margin property is a shorthand that sets the outer spacing on all four sides of an element — outside the border. It sets margin-top, margin-right, margin-bottom, and margin-left.

Use one to four values (length, percentage, or auto). Negative values pull neighbors closer. It is not inherited. Initial value for each side is 0.

Baseline · Widely available.

Not deprecated and not experimental. Core box-model spacing supported in all browsers for decades.

Margin vs padding.

margin is outside the border. padding is inside the border, between the border and the content.

CSS Syntax

syntax.css
margin: [ <length> | <percentage> | auto ]{1,4} | initial | inherit;

1 value = all sides. 2 values = top/bottom | left/right. 3 values = top | left/right | bottom. 4 values = top | right | bottom | left (clockwise).

Property Values

ValueDescriptionTry it
<length>Fixed size such as 16px or 1rem. Negatives allowedTry it
Two valuesTop/bottom, then left/right — for example 8px 24pxTry it
Four valuesTop, right, bottom, left in clockwise orderTry it
autoBrowser picks the margin. With a set width, 0 auto centers horizontallyTry it
<percentage>Relative to the containing block’s inline size (usually its width)Try it
initialResets each side to 0Try it
inheritInherits the computed margin from the parentTry it

How to Use margin

Pick how many sides you need, then write one to four values.

  1. Decide the spacing: all sides equal, vertical/horizontal pairs, or each side.
  2. Write margin with 1–4 values (or use a longhand like margin-top).
  3. For horizontal centering of a block, set a width and use margin: 0 auto.
  4. Watch for collapsing between stacked block margins in normal flow.
GoalTypical value
Equal space all aroundmargin: 1rem;
More side space than verticalmargin: 0.5rem 1.5rem;
Center a fixed-width blockmargin: 0 auto; (plus a width)

Live Preview

The middle (amber) box is the target. Change its margin and watch the space outside its border.

Orange dashed area is the parent. Blue boxes are neighbors. Amber is the element with margin.

Boxes
Above
Target
Below

margin: 0

CSS Examples

Each snippet shows only the margin you are studying. Use Try It Yourself for a live editor.

Example 1 — One Value (All Sides)

Apply the same margin on every side.

CSS
<style>
  .box {
    margin: 1rem;
  }
</style>

<div class="box">Box A</div>
<div class="box">Box B</div>
Try It Yourself

How It Works

  1. One length. margin: 1rem sets top, right, bottom, and left to 1rem.
  2. Space is outside. The gap sits outside each box border.
  3. Neighbors move apart. Stacked boxes gain outer space between them (subject to collapsing).
  4. Same idea for any unit. px, em, and rem all work.

Example 2 — Two Values

Set vertical and horizontal margins with two values.

CSS
<style>
  .box {
    margin: 0.5rem 1.5rem;
  }
</style>

<div class="box">Target</div>
Try It Yourself

How It Works

  1. First value. 0.5rem applies to top and bottom.
  2. Second value. 1.5rem applies to left and right.
  3. Read the pair. Think “vertical | horizontal.”
  4. Need three or four? Add a bottom-only third value, or go clockwise with four.

Example 3 — Horizontal Center with auto

Center a block that has a set width.

CSS
<style>
  .box {
    width: 60%;
    margin: 0.75rem auto;
  }
</style>

<div class="box">Centered</div>
Try It Yourself

How It Works

  1. Set a width. The box must be narrower than its parent.
  2. Use auto on the sides. Left and right share leftover space equally.
  3. Shorthand form. margin: 0.75rem auto keeps vertical margin and centers horizontally.
  4. Modern alternative. Flex/grid centering on the parent is also common.

Edge Cases & Pitfalls

If spacing looks smaller than expected, collapsing or auto rules are often why.

collapse

Vertical margins can collapse

Adjacent top/bottom margins of blocks in normal flow may merge into one margin (the larger value), not a sum.

inline

Top/bottom margin on non-replaced inline

For elements like span, vertical margins do not affect layout. Horizontal margins still apply.

percent

Percentages use the containing block width

Even top and bottom percentage margins resolve against the containing block’s inline size (usually width), not its height.

auto

auto needs leftover space

Horizontal centering with auto fails if the box is full width or not a block-level box with a definite width.

negative

Negative margins pull elements closer

Useful for overlaps, but easy to break responsive layouts. Prefer gap/padding when you only need breathing room.

Browser Support

margin is a browser CSS property for the box model. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available

margin

Supported in all modern browsers and Internet Explorer for many years. Part of the CSS Box Model.

99% Box model support
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1+
Full support
Microsoft Edge 12+
Full support
Opera 3.5+
Full support
Samsung Internet 1+
Full support
Internet Explorer 3+
Full support
margin Widely supported

Bottom line: Safe for every layout. Logical longhands like margin-block are newer; the classic margin shorthand is universal. It is not a runtime API.

Key Takeaways

  • Role: outer spacing outside the border on all four sides.
  • Shorthand: 1–4 values map to sides (clockwise when four).
  • Center: width + margin: 0 auto for horizontal centering.
  • Collapse: stacked vertical margins may merge, not add.

One line: margin sets space outside the border — use 1–4 values, or auto to center a sized block.

Frequently Asked Questions

It is a shorthand that sets the outer spacing on all four sides of an element — outside the border — by setting margin-top, margin-right, margin-bottom, and margin-left.
Each side defaults to 0. Browsers may still add margins on elements like p or h1 through their own stylesheets.
Margin is space outside the border, between elements. Padding is space inside the border, between the border and the content.
Give the element a width less than its container, then use margin-left and margin-right as auto — often written as margin: 0 auto.
Yes. Adjacent top and bottom margins of block elements in normal flow can collapse into one margin equal to the larger value.
It is Baseline and widely available in all browsers. It is a browser CSS property for layout, not a Node.js feature.

Next: margin-block

Learn the logical shorthand for block-axis margins.

margin-block tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful