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
Individual Properties
Control each side individually for precise spacing
Auto Centering
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.
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
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).
🎯 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,
gapis usually cleaner than margins between items. margin-left: autoin 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.