CSS Basics

CSS Margins

CSS Margins are transparent areas outside an element's border that create space between elements, affecting layout positioning and element spacing without affecting the element's background or content area.

What is margin in CSS?

Margin is the transparent space outside an element's border that pushes neighbouring elements away. Set all sides with margin: 16px, vertical and horizontal with margin: 16px 24px, or each side in clockwise order: top, right, bottom, left. margin-inline: auto centres a block element that has a width.

margin: 16px;                 /* all sides */
margin: 16px 24px;            /* vertical | horizontal */
margin: 8px 16px 24px 32px;   /* top right bottom left */
margin-inline: auto;          /* center horizontally */
margin-top: -10px;            /* negative pulls closer */

Margin Concepts

External Space

Space outside the element's border, pushing other elements away

Auto Centering

Use auto margins to center elements horizontally within containers

Negative Values

Negative margins pull elements closer or create overlapping effects

Collapse

Adjacent vertical margins combine into a single margin

Margin Properties Demo

See how different margin values affect element positioning and spacing. Hover over elements to see margin visualization and click the container to toggle margins!

Code Editor

<div class="box">margin: 20px</div>
<div class="box">Vertical margins collapse: the gap is 20px, not 40px</div>
<div class="center">margin-inline: auto centres me</div>

Preview

Margin Syntax

Margin Shorthand Values

/* One value - all sides */ margin: 20px; /* Two values - vertical | horizontal */ margin: 10px 20px; /* top/bottom: 10px, left/right: 20px */ /* Three values - top | horizontal | bottom */ margin: 10px 20px 30px; /* top: 10px, left/right: 20px, bottom: 30px */ /* Four values - top | right | bottom | left (clockwise) */ margin: 10px 15px 20px 25px;

Individual Properties

margin-top: 10px; margin-right: 15px; margin-bottom: 20px; margin-left: 25px;

Control each side individually for precise spacing

Auto Centering

/* Center horizontally */ margin: 0 auto; /* Center with vertical margins */ margin: 20px auto; margin: 10px auto 30px;

Use auto for horizontal centering of block elements

Margin Collapse

Understanding vertical margin behavior

What is Margin Collapse?

When two vertical margins meet, they collapse into a single margin equal to the larger of the two margins.

/* Element 1 has margin-bottom: 20px */ .element-1 { margin-bottom: 20px; } /* Element 2 has margin-top: 30px */ .element-2 { margin-top: 30px; } /* Actual gap between elements: 30px (not 50px) */

When Collapse Happens

  • •Adjacent block elements
  • •Parent and first/last child
  • •Empty block elements

Preventing Collapse

  • •Use padding instead of margins
  • •Add borders or padding to parent
  • •Use flexbox or grid layouts

Your Learning Path

Master CSS margins and spacing control

What You've Learned

  • ▶Margin property syntax and values
  • ▶Individual margin sides control
  • ▶Auto margins for centering
  • ▶Negative margins and their effects
  • ▶Margin collapse behavior

What's Next

  • ▶CSS Padding - Internal element spacing
  • ▶CSS Height & Width - Element dimensions
  • ▶CSS Box Model - Complete layout system
  • ▶CSS Text & Fonts - Typography control

Pro Layout Tip

Use consistent margin values throughout your design system. Consider using CSS custom properties to maintain spacing consistency across your entire website.

CSS Margins — step by step

1 / 4
1

Margin Shorthand

margin: 10px 20px sets top/bottom to 10px and left/right to 20px. All four sides: margin: top right bottom left (clockwise from top).

1 of 4
🔍Margins Collapse, Padding Does Not
Vertical margins between two block siblings collapse to the larger value, not the sum. This never happens with padding, flex children, or grid items — understanding this prevents mysterious spacing gaps.

🎯 Quick Check

What does margin: 0 auto do to a block element with a defined width?

CSS Margins: summary

Key points

  • Vertical margins between blocks collapse into the larger one; horizontal margins add together.
  • Percentage margins, even vertical ones, are calculated from the parent's width.
  • In flex and grid layouts, gap is usually cleaner than margins between items.
  • margin-left: auto in a flex container pushes an item (and everything after it) to the far end.

Frequently asked questions

›Why is my margin-top not working?

It's often margin collapse: the child's top margin merges with the parent's and moves the parent instead. Add padding or a border to the parent, or use display: flow-root, flex or grid on it.

›Can margin be negative?

Yes. A negative margin pulls the element, or its neighbours, closer and can make elements overlap.