CSS margin Property

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 4 Examples
Layout & Box Model

What You’ll Learn

The margin property creates outer space around an element. It is a core box-model tool for layout, spacing, and centering block elements.

01

Outer Space

Outside border.

02

Shorthand

1 to 4 values.

03

auto

Center blocks.

04

TRBL

Top right bottom left.

05

Box Model

Not padding.

06

Default 0

No spacing.

Introduction

The margin property in CSS is used to create space around elements, outside of any defined borders. It helps control layout by positioning elements with spacing between them and their neighbors.

Margin can be set on all four sides of an element (top, right, bottom, and left), allowing flexible control over placement on the page. It is part of the CSS box model along with content, padding, and border.

Definition and Usage

Apply margin to any element that participates in normal document flow: divs, paragraphs, headings, buttons, and more. Use single-value shorthand for equal spacing, four values for precise control, or margin: 0 auto to center block elements.

💡
Beginner Tip

Margin creates space outside the border. Padding creates space inside the border. They solve different layout problems.

📝 Syntax

The margin property can be specified in several ways, allowing either uniform or unique margins for each side:

syntax.css
selector {
  margin: value;
}

Shorthand Rules

  • Single value — Applies the same margin to all four sides.
  • Two values — First applies to top and bottom; second to left and right.
  • Three values — First to top; second to left and right; third to bottom.
  • Four values — Top, right, bottom, left (clockwise).

Basic Example

margin.css
.box {
  margin: 20px 40px 60px 80px;
}

⚡ Quick Reference

QuestionAnswer
Initial value0
Applies toAll elements except elements with table display types that use separate border model
InheritedNo
AnimatableYes, as a length
Common useSpacing between elements, centering blocks, and page layout rhythm

💎 Property Values

ValueExampleMeaning
Lengthmargin: 20px;Fixed margin in px, em, rem, etc.
Percentagemargin: 10%;Margin as a percentage of the containing block width
automargin: 0 auto;Browser calculates margin; commonly used to center block elements
initialmargin: initial;Resets the property to its initial value (0)
inheritmargin: inherit;Inherits the margin value from the parent element
margin: 1rem; margin: 1rem 2rem; margin: 0 auto;

🎯 Default Value

The default value for the margin property is 0, meaning there is no margin unless specified. This default can be overridden by applying specific values to the margin property or its longhand sides (margin-top, margin-right, margin-bottom, margin-left).

👀 Live Preview

A box with uneven margins inside a dashed container:

margin box

Examples Gallery

Set four different margins, apply uniform spacing, center a block with auto, and use two-value shorthand.

🔢 Custom Margins

Start with the reference example — set different margins on each side of a box.

Example 1 — Four-Value Margin

Set different margins on each side of a div element.

margin-four.html
<style>
  .box {
    width: 200px;
    height: 100px;
    background-color: lightblue;
    margin: 20px 40px 60px 80px;
  }
</style>

<div class="box">This box has custom margins.</div>

In this example, the margin is 20px top, 40px right, 60px bottom, and 80px left.

Try It Yourself

How It Works

Values follow clockwise order: top, right, bottom, left. The dashed area around the box shows the outer spacing created by margin.

Example 2 — Uniform Margin

Apply the same spacing on all four sides with one value.

margin-uniform.css
.card {
  margin: 1.5rem;
  padding: 1rem;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 0.5rem;
}
Try It Yourself

How It Works

A single value like 1.5rem is the quickest way to add consistent outer spacing around a component.

📈 Centering & Shorthand

Use auto for horizontal centering and two-value shorthand for vertical and horizontal spacing.

Example 3 — Center with margin: 0 auto

Center a block element horizontally inside its container.

margin-auto.css
.panel {
  width: 16rem;
  margin: 0 auto;
  padding: 1rem;
  background: #dbeafe;
}
Try It Yourself

How It Works

auto tells the browser to split leftover horizontal space equally. The element needs a defined width for this pattern to work.

Example 4 — Two-Value Shorthand

Set vertical and horizontal margins with two values.

margin-two.css
.article {
  margin: 2rem 1rem;
}
Try It Yourself

How It Works

margin: 2rem 1rem applies 2rem to top and bottom, and 1rem to left and right. This is a common rhythm for content sections.

Margin in the Box Model

Every element is drawn as a box with these layers from inside out:

  • Content — the text, image, or child elements.
  • Padding — space inside the border around the content.
  • Border — the visible edge of the box.
  • Margin — transparent space outside the border that separates this box from others.
Margin Collapse

Vertical margins between block elements can collapse into the larger value. Horizontal margins do not collapse. This is normal CSS behavior in document flow.

♿ Accessibility

  • Use margin for layout spacing — Do not rely on empty elements just to create gaps.
  • Keep touch targets spaced — Buttons and links benefit from enough margin so they are easy to tap.
  • Do not remove focus outlines — Margin adjusts spacing; it should not replace visible focus indicators.
  • Test zoomed layouts — Large margins can push content off-screen on small viewports.
  • Maintain readable line length — Centered blocks with margin: 0 auto help keep text readable.

🧠 How margin Works

1

You set outer spacing

Apply margin with one to four values, or use auto for centering.

CSS rule
2

Browser reserves space

Transparent margin area is added outside the border on each side you specify.

Box model
3

Neighbors shift apart

Nearby elements move away, creating gutters, stacks, and centered layouts.

Layout
=

Structured spacing

Pages breathe with clear separation between sections, cards, and components.

🖥 Browser Compatibility

The margin property is fully supported in all modern web browsers, including Chrome, Firefox, Safari, Edge, and Internet Explorer. It is a fundamental CSS property with broad compatibility.

Universal · All browsers

Core layout spacing everywhere

margin is one of the most fundamental CSS properties. You can rely on it in any project.

99% Global browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Legacy & Chromium
Full support
Opera All versions · Modern & legacy
Full support
margin property 99% supported

Bottom line: Safe to use everywhere. For logical spacing in RTL layouts, see margin-block.

🎉 Conclusion

The margin property is essential for controlling the spacing and layout of elements on a webpage. By adjusting margins, you can create space around elements, making your design more visually appealing and organized.

Understanding how to use margin effectively can greatly enhance your ability to create well-structured web layouts. Start with single-value spacing, learn four-value shorthand, and practice margin: 0 auto for centered blocks.

💡 Best Practices

✅ Do

  • Use margin: 0 auto to center block elements with a width
  • Prefer rem or em for scalable spacing
  • Use two-value shorthand for vertical and horizontal rhythm
  • Combine margin with padding for inner and outer spacing
  • Reset browser defaults with a CSS reset or normalize when needed

❌ Don’t

  • Confuse margin with padding
  • Use huge fixed margins that break mobile layouts
  • Expect auto to center elements without a width
  • Add empty divs only to create spacing
  • Forget vertical margin collapse between stacked blocks

Key Takeaways

Knowledge Unlocked

Five things to remember about margin

Use these points when spacing elements with CSS.

5
Core concepts
0 02

Default 0

No spacing.

Default
auto 03

Center blocks

0 auto.

Pattern
TRBL 04

Shorthand

1 to 4 values.

Syntax
pad 05

Not padding

Inside vs out.

Box model

❓ Frequently Asked Questions

The margin property creates space around an element, outside of its border. It pushes neighboring elements away and controls layout spacing.
The default value is 0, meaning there is no outer spacing unless you set a margin explicitly.
Margin adds space outside the border between elements. Padding adds space inside the border between the border and the content.
Set a width on the element and use margin: 0 auto. The browser splits leftover horizontal space equally on both sides.
No. margin is not inherited, but you can use margin: inherit to copy a parent element's margin value.

Practice in the Live Editor

Open the HTML editor, try different margin values, and see how spacing changes your layout instantly.

HTML Editor →

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.

5 people found this page helpful