CSS margin-top Property

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

What You’ll Learn

The margin-top property adds outer space above an element. It is one of the most common tools for vertical spacing in CSS layouts.

01

Top Space

Outside border.

02

Box Model

Outer spacing.

03

Stack gaps

Separate blocks.

04

Units

px, rem, %.

05

Collapse

Vertical margins.

06

Default 0

No spacing.

Introduction

The margin-top property in CSS is used to set the top margin of an element.

The margin is the space outside the border of an element, which separates it from other elements on the page. By adjusting the margin-top, you can position elements vertically and control the layout of your web page.

Definition and Usage

Apply margin-top when you need spacing on only the top side without changing bottom, left, or right margins. It is ideal for separating headings from content, spacing stacked sections, and creating gaps between paragraphs.

💡
Beginner Tip

margin-top creates space outside the border above the element. Use padding-top when you need space inside the border.

📝 Syntax

The syntax for the margin-top property is simple and can accept various units for the value.

syntax.css
element {
  margin-top: value;
}

Here, value can be a length, percentage, or the keyword auto.

Basic Example

margin-top.css
p {
  margin-top: 20px;
}

⚡ Quick Reference

QuestionAnswer
Initial value0
Applies toAll elements except table display types that use separate border model
InheritedNo
AnimatableYes, as a length
Logical equivalentmargin-block-start (in horizontal LTR writing)

💎 Property Values

ValueExampleMeaning
Lengthmargin-top: 20px;Fixed margin using px, em, rem, vw, etc.
Percentagemargin-top: 10%;Margin as a percentage of the containing block width
automargin-top: auto;Browser calculates the margin; rarely used for vertical centering with margin-top
inheritmargin-top: inherit;Takes the computed value from the parent element
initialmargin-top: initial;Resets to the default value (0)
unsetmargin-top: unset;Resets to inherit or initial depending on context
margin-top: 1rem; margin-top: 24px; margin-top: 5%;

🎯 Default Value

The default value of the margin-top property is 0. This means that there is no margin applied to the top of the element unless explicitly specified.

👀 Live Preview

A heading followed by a box with top margin pushing it down:

Heading above

margin-top: 1.25rem

Examples Gallery

Add top margin to paragraphs, space stacked cards, use rem for typography rhythm, and compare with the logical equivalent.

🔢 Top Spacing

Start with the reference example — apply top margin to paragraph elements.

Example 1 — Paragraph Top Margin

In this example, we apply a top margin of 20px to paragraph elements.

margin-top.html
<style>
  p {
    margin-top: 20px;
  }
</style>

<h1>Paragraph with Custom Top Margin</h1>
<p>This paragraph has a top margin of 20px, pushing it down from the heading above.</p>
<p>Another paragraph with the same margin, creating consistent spacing.</p>
Try It Yourself

How It Works

Top margin adds space above each paragraph, pushing it down from the heading and from the paragraph above it.

Example 2 — Stacked Cards

Use top margin to separate stacked UI cards, skipping the first card in the list.

margin-top-stack.css
.card {
  padding: 1rem;
  background: #fff;
  border: 1px solid #e2e8f0;
  border-radius: 0.5rem;
}

.card + .card {
  margin-top: 1rem;
}
Try It Yourself

How It Works

The adjacent sibling selector adds top margin only from the second card onward, creating vertical rhythm without extra space above the first item.

📈 Typography & Logical Properties

Use scalable units for content spacing and understand the logical property equivalent.

Example 3 — rem for Content Rhythm

Use rem units so top spacing scales with the root font size.

margin-top-rem.css
.article p + p {
  margin-top: 1.25rem;
  line-height: 1.6;
}
Try It Yourself

How It Works

1.25rem scales with the user’s root font size, making paragraph spacing more accessible than fixed pixels alone.

Example 4 — Physical vs Logical

In horizontal LTR writing, margin-top matches margin-block-start:

margin-top-logical.css
/* Physical */
.note {
  margin-top: 1.5rem;
}

/* Logical equivalent in horizontal LTR */
.note {
  margin-block-start: 1.5rem;
}
Try It Yourself

How It Works

For simple English pages, margin-top is familiar and widely used. Choose margin-block-start when layouts must adapt to writing mode.

margin-top in the Box Model

Margin sits outside the border. Top margin specifically adds transparent space above the element:

  • Content — text or child elements inside the box.
  • Padding — inner space between content and border.
  • Border — the visible edge.
  • Margin-top — outer space above the border.
Margin Collapse

When two block elements stack, their vertical margins can collapse into the larger value. This is normal CSS behavior in document flow.

♿ Accessibility

  • Keep readable paragraph spacing — Top margin helps separate headings from body content for easier reading.
  • Do not rely on empty elements for spacing — Use margin instead of blank divs.
  • Test zoomed layouts — Large top margins can push content off-screen on small viewports.
  • Maintain touch target spacing — Buttons and links benefit from enough margin so they are easy to tap.
  • Combine with semantic HTML — Margins adjust spacing; structure still comes from proper elements.

🧠 How margin-top Works

1

You set top spacing

Apply margin-top with px, rem, %, or a keyword.

CSS rule
2

Browser reserves space

Transparent margin area is added above the border on the top edge.

Box model
3

Element shifts downward

The element moves away from content above, creating vertical separation.

Layout
=

Clean vertical rhythm

Stacked elements breathe with clear gaps between them.

🖥 Browser Compatibility

The margin-top property is supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is a widely used property and should work consistently across different platforms and devices.

Universal · All browsers

Core layout spacing everywhere

margin-top 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-top property 99% supported

Bottom line: Safe to use everywhere. For writing-mode-aware spacing, see margin-block-start.

🎉 Conclusion

The margin-top property is a fundamental CSS property for managing the layout and spacing of elements on a webpage. By adjusting the top margin, you can control the vertical positioning of elements, enhancing the overall structure and flow of your design.

Whether you’re creating space between headers and content, or aligning multiple elements, margin-top provides a simple yet powerful tool for web developers.

💡 Best Practices

✅ Do

  • Use margin-top for spacing between stacked blocks and after headings
  • Prefer rem for scalable top spacing
  • Use adjacent sibling selectors to skip the first item in a stack
  • Combine with other margin longhands for precise control
  • Consider margin-block-start for multilingual layouts

❌ Don’t

  • Confuse margin-top with padding-top
  • Use empty divs only to create spacing
  • Use huge fixed margins that break mobile layouts
  • Forget vertical margin collapse between blocks
  • Apply top margin when flex or grid gap would be cleaner

Key Takeaways

Knowledge Unlocked

Five things to remember about margin-top

Use these points when spacing elements above the top edge.

5
Core concepts
0 02

Default 0

No spacing.

Default
rem 03

Scalable

Use rem.

Units
col 04

Collapse

Vertical margins.

Behavior
pad 05

Not padding

Inside vs out.

Box model

❓ Frequently Asked Questions

The margin-top property sets outer spacing above an element, outside of its border. It pushes the element away from content above it.
The default value is 0, meaning there is no top margin unless you set one explicitly.
margin-top adds space outside the border between elements. padding-top adds space inside the border between the border and the content.
margin-top always targets the physical top edge. margin-block-start follows the writing mode and adapts when text flow or direction changes.
Yes. Vertical margins between block elements can collapse. The larger margin wins when two block elements stack vertically.

Practice in the Live Editor

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

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