CSS Portal

CSS margin-top Property

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The margin-top property controls the amount of space placed outside an element’s top edge, effectively pushing the element away from the content or element above it. It is one side of the box model’s external spacing mechanism and is commonly used to create vertical rhythm between block-level elements; it is related to the overall margin concept and contrasts with internal spacing provided by padding. Because it sits outside the element’s border, changing margin-top does not alter an element’s own width or height but moves the element within the surrounding layout.

The property participates in CSS margin-collapsing rules: adjacent vertical margins of block-level elements in normal flow can combine into a single margin rather than stack, so the effective gap between two blocks may be different from simply adding their margins. Collapsing can occur between a parent and its first/last child in some situations, but it does not apply to floated elements, absolutely positioned elements, or elements that establish new formatting contexts. When elements are taken out of normal flow via position or manually offset by the top property, the way margin-top affects layout differs because those elements do not participate in the same flow and collapsing behaviors.

In practical use, margin-top is a primary tool for managing vertical spacing and alignment, but it must be applied thoughtfully because interactions with layout modes and neighboring elements can produce unintuitive results. For example, items inside a flex or grid container follow different spacing rules and may not experience margin collapsing, so understanding the container’s display behavior is important when you rely on top margins. Developers often combine careful use of top margins with padding, alignment properties, and layout contexts to achieve predictable spacing across different screen sizes and component compositions.

Definition

Initial value
0
Applies to
All elements
Inherited
No
Computed value
As specified, but with relative lengths converted into absolute pixel values.
Animatable
Yes
JavaScript syntax
object.style.marginTop

Interactive Demo

Margin Top Example

Syntax

margin-top: <length> | <percentage> | auto

Values

  • <length>Specifies a fixed length, using any standard CSS length units. Negative Values are allowed.
  • <percentage>A percentage always relative to the width of the containing block.
  • inherit

Example

<div class="container">
<h2>Margin-top example</h2>
<div class="box box-1">Box 1</div>
<div class="box box-2">Box 2 (margin-top: 30px)</div>
</div>
.container {
    width: 480px;
    border: 1px solid #e0e0e0;
    padding: 16px;
    font-family: Arial, sans-serif;
}

.box {
    background-color: #f8fafc;
    border: 1px solid #dbeafe;
    padding: 12px 16px;
    color: #0f172a;
}

.box-2 {
    margin-top: 30px;
}

Browser Support

The following information will show you the current browser support for the CSS margin-top property. Hover over a browser icon to see the version that first introduced support for this CSS property.

This property is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!