CSS margin-top Property

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

Definition and Usage

The margin-top property sets the margin area on the top of an element. A positive value moves the element farther from neighbors above; a negative value pulls it closer.

It is part of the box model (outside the border), is not inherited, and has no effect on non-replaced inline elements such as <span>. Prefer margin-block-start when you want spacing that follows writing mode.

Baseline · Widely available.

Not deprecated. Core length, percentage, and auto values work across modern browsers.

CSS Syntax

syntax.css
margin-top: <length> | <percentage> | auto | inherit;

Example: margin-top: 1.5rem;. Values may be positive, zero, or negative.

Property Values

ValueDescriptionTry it
<length>Fixed size such as 16px or 1emTry it
<percentage>Relative to the containing block’s width (inline size)Try it
autoBrowser chooses a used value (often 0 for top alone)Try it
Negative lengthPulls the element closer to content aboveTry it
0 / initialNo top margin (initial value is 0)Try it
inheritTakes the parent’s margin-topTry it

How to Use margin-top

Use it to space a block away from whatever comes before it.

  1. Select the element that needs space above it.
  2. Set a length (1rem, 24px) or a percentage of the container width.
  3. Prefer gap on flex/grid parents when spacing many siblings evenly.
  4. Watch for vertical margin collapse between stacked blocks.
PieceRequired?Role
Block or replaced boxTypicalInline non-replaced elements ignore margin-top
Length or %Yes (or auto)Sets the top outer space
InheritedNoSet on each element that needs it
Preceding contentTo see itThe gap appears after the previous box in normal flow

Live Preview

Change margin-top on the yellow box and watch the space after the box above.

Percentages are based on the container width, not its height.

Stacked boxes
Above
Target

margin-top: 24px

CSS Examples

Each snippet focuses on margin-top. Use Try It Yourself for a live editor.

Example 1 — Length Spacing

Add a fixed gap above a box.

CSS
<style>
  .card {
    margin-top: 24px;
  }
</style>

<div>Above</div>
<div class="card">Card</div>
Try It Yourself

How It Works

  1. Target the second box. The rule applies to .card.
  2. Set 24px. Outer space appears above its border.
  3. The card moves down. It starts after that gap from the box above.
  4. Not padding. The space is outside the border, not inside the content box.

Example 2 — Percentage

Size the top margin from the container width.

CSS
<style>
  .box {
    margin-top: 10%;
  }
</style>

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

How It Works

  1. Percent of width. Vertical margin percentages use the containing block’s inline size.
  2. Wider parent → larger gap. The top margin scales with container width.
  3. Not based on height. A common surprise for beginners.
  4. Try resizing the editor pane to see the gap change.

Example 3 — Negative Margin

Pull the box closer to the content above (or overlap).

CSS
<style>
  .pull {
    margin-top: -12px;
  }
</style>

<div>Above</div>
<div class="pull">Pull</div>
Try It Yourself

How It Works

  1. Negative length. The top margin becomes less than zero.
  2. The box moves up. Content can overlap if the value is large enough.
  3. Use sparingly. Prefer padding, gap, or positive margins for everyday layout.
  4. Check stacking and click targets when boxes overlap.

Edge Cases & Pitfalls

If the gap looks wrong or missing, check these first.

collapse

Vertical margins can collapse

A block’s margin-bottom and the next block’s margin-top may combine into one gap — often the larger value, not the sum.

inline

No effect on non-replaced inline

MDN: margin-top does not affect elements like <span>. Use a block, inline-block, or flex/grid item instead.

percent

% is based on width

Even for top margin, percentages resolve against the containing block’s inline size (width in horizontal writing modes).

vs

margin-top vs padding-top

Use padding-top for inner space (backgrounds extend into padding). Use margin for space between separate boxes.

logical

Physical top vs block-start

For multilingual or vertical layouts, prefer margin-block-start so spacing follows writing mode.

Browser Support

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

✓ Baseline · Widely available

margin-top

Length, percentage, and auto values work in all modern browsers and older engines.

99% Browser support
Google Chrome All modern versions
Full support
Mozilla Firefox All modern versions
Full support
Apple Safari All modern versions
Full support
Microsoft Edge All modern versions
Full support
Opera All modern versions
Full support
Samsung Internet Supported
Full support
Internet Explorer Supported
Full support
margin-top Widely supported

Bottom line: Safe for everyday vertical spacing. Remember collapse and the %/width rule. It is not a runtime API.

Key Takeaways

  • Role: outer space above an element’s border.
  • Initial: 0. Not inherited.
  • Percentages use containing-block width.
  • Collapse can merge adjacent vertical margins.

One line: margin-top pushes space above a box — outside the border, after the previous element.

Frequently Asked Questions

It sets the outer space above an element, outside its border. A positive value pushes the element down from content above; a negative value pulls it closer.
The initial value is 0. There is no top margin until you set one.
margin-top is outside the border, between elements. padding-top is inside the border, between the border and the content.
margin-top always targets the physical top edge. margin-block-start follows writing mode, so it maps to the block-start side in vertical or RTL layouts.
Yes. Adjacent vertical margins between block boxes can collapse into one. The used gap is often the larger of the two margins.
It is Baseline Widely available. It is a browser CSS box-model property, not a Node.js runtime feature.

Next: mask-image

Hide parts of an element using an image as a mask.

mask-image 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