CSS margin-top Property
The CSS margin-top property sets the outer margin above an element, controlling the space between its top border and surrounding content.
It accepts lengths, percentages, negative values, and the auto keyword. Unlike logical margin properties, margin-top always targets the physical top side, regardless of writing mode or text direction. In normal block layout, vertical margins may also collapse with adjoining margins.
margin-top Syntax
The margin-top property accepts a CSS length, percentage, or auto. CSS-wide keywords are also supported.
Basic Syntax
selector {
margin-top: 20px;
}
Length Value
div {
margin-top: 2rem;
}
Percentage Value
div {
margin-top: 10%;
}
Auto Value
div {
margin-top: auto;
}
Negative Value
div {
margin-top: -15px;
}
CSS-Wide Keywords
div {
margin-top: initial;
}
div {
margin-top: inherit;
}
div {
margin-top: unset;
}
div {
margin-top: revert;
}
div {
margin-top: revert-layer;
}
The initial value of margin-top is 0.
Property Values
| Value | Description |
|---|---|
<length> |
Sets the top margin using a length such as px, em, rem, or vh. |
<percentage> |
Sets the top margin as a percentage of the containing block's width. |
auto |
Allows the browser to calculate the top margin according to the formatting context. |
initial |
Resets the property to its initial value of 0. |
inherit |
Inherits the computed top margin from the parent element. |
unset |
Resets the property to 0 because margins are not inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | 0 |
| Inherited | No |
| Applies to | All elements except elements with table display types other than table-caption, table, and inline-table |
| Computed value | Absolute length, percentage, or auto |
| Animation type | Length, percentage, or calculated combination |
| Negative values | Allowed for lengths and percentages |
| Physical side | Top |
| Shorthand property | margin |
How It Works
The margin-top property adds space outside an element's top border. In normal block layout, a positive top margin typically separates the element from content above it, although margin collapsing can affect the resulting distance.
HTML Example
<div class="content-container">
<div class="content-box">First Box</div>
<div class="content-box spaced-box">Second Box</div>
</div>
CSS Example
.content-box {
padding: 15px;
border: 2px solid #2563eb;
background-color: #dbeafe;
}
.spaced-box {
margin-top: 30px;
}
The second box has a 30-pixel top margin. Because the first box has no bottom margin, the space between their borders is 30 pixels.
Remove the Top Margin
.spaced-box {
margin-top: 0;
}
Setting the margin to 0 removes the additional spacing above the second box.
Margin vs. Padding
Margins and padding both create spacing, but they affect different parts of the CSS box model.
Top Margin
.box {
margin-top: 20px;
}
The space appears outside the element's top border.
Top Padding
.box {
padding-top: 20px;
}
The space appears inside the top border, between the border and the content.
Using Both Properties
.box {
margin-top: 20px;
padding-top: 15px;
border: 2px solid #2563eb;
background-color: #dbeafe;
}
The top margin separates the box from surrounding content, while the top padding creates space inside the box. The background normally extends through the padding but not the margin.
Length Values
The margin-top property supports absolute and relative CSS length units.
Pixels
.box {
margin-top: 24px;
}
Root Font Size
.box {
margin-top: 2rem;
}
Element Font Size
.box {
margin-top: 1.5em;
}
Viewport Units
.box {
margin-top: 3vh;
}
Calculated Value
.box {
margin-top: calc(1rem + 10px);
}
Important Consideration
Relative units such as rem are useful for maintaining consistent vertical spacing throughout a responsive layout.
Percentage Values
Percentage top margins are calculated relative to the containing block's width, not its height. This is an important distinction when using vertical margins.
Percentage Example
.box {
margin-top: 10%;
}
Fixed Container Width
.container {
width: 600px;
}
.container .box {
margin-top: 10%;
}
The top margin equals 60 pixels when the containing block's width is 600 pixels.
Percentage vs. Viewport Height
.box-one {
margin-top: 10%;
}
.box-two {
margin-top: 10vh;
}
The first declaration uses the containing block's width. The second uses 10% of the viewport height.
Important Consideration
Percentage top margins may change unexpectedly when the container width changes. Use vh, rem, or another suitable unit when spacing should follow a different measurement.
Auto Margins
The auto keyword allows the browser to calculate the top margin according to the layout context. Its behavior differs between normal block layout, Flexbox, and Grid.
Basic Example
.box {
margin-top: auto;
}
In normal block layout, an automatic top margin generally resolves to zero. It does not automatically push a block to the bottom of its container.
Flexbox Example
.flex-container {
display: flex;
flex-direction: column;
height: 300px;
}
.flex-item:last-child {
margin-top: auto;
}
When free vertical space is available, the automatic top margin on the last flex item absorbs that space and pushes the item toward the bottom of the container.
Card Footer Example
.card {
display: flex;
flex-direction: column;
min-height: 250px;
}
.card-footer {
margin-top: auto;
}
This technique keeps a card footer near the bottom when the card contains less content than its available height.
Important Consideration
Automatic top margins depend on available free space and formatting rules. They are particularly useful in column-oriented flex layouts.
Negative Margins
Negative top margins reduce the space above an element and can pull it upward, potentially causing overlap with preceding content.
Negative Length
.box {
margin-top: -20px;
}
Negative Percentage
.box {
margin-top: -5%;
}
Overlapping a Previous Element
.overlapping-box {
margin-top: -25px;
padding: 15px;
border: 2px solid #2563eb;
background-color: #dbeafe;
}
The negative margin can pull the box upward so that it overlaps preceding content.
Important Consideration
Negative margins are valid CSS but may cause unexpected overlap or clipping. They should be tested with varying content lengths and viewport sizes.
Margin Collapsing
In normal block layout, adjoining vertical margins can collapse into a single margin instead of adding together. This commonly affects the top and bottom margins of adjacent block elements.
Adjacent Margins
.first-box {
margin-bottom: 30px;
}
.second-box {
margin-top: 20px;
}
When these positive margins collapse, the space between the boxes is 30 pixels, not 50 pixels. The larger margin determines the collapsed distance.
Equal Margins
.first-box {
margin-bottom: 20px;
}
.second-box {
margin-top: 20px;
}
The collapsed margin is 20 pixels rather than 40 pixels.
Positive and Negative Margins
.first-box {
margin-bottom: 30px;
}
.second-box {
margin-top: -10px;
}
When these margins collapse, the result is 20 pixels because the negative margin reduces the positive margin.
Parent and Child Margins
A first child's top margin may collapse with its parent's top margin when no border, padding, inline content, or other condition separates them.
.parent {
background-color: #f1f5f9;
}
.child {
margin-top: 30px;
}
Under the appropriate conditions, the child's margin can collapse through the parent rather than creating 30 pixels of internal spacing.
Preventing Parent-Child Margin Collapsing
.parent {
display: flow-root;
}
.child {
margin-top: 30px;
}
The flow-root value establishes a new block formatting context, preventing the child's top margin from collapsing with the parent's margin.
Margins in Flexbox and Grid
Margins of flex and grid items do not collapse. For example, a 20-pixel bottom margin on one flex item and a 30-pixel top margin on the next remain separate rather than collapsing into a single margin.
Important Consideration
Margin collapsing applies only under specific block formatting conditions. It does not occur between flex items, grid items, or absolutely positioned elements.
margin Shorthand
The margin shorthand sets all four physical margins, including margin-top.
Individual Properties
.box {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 30px;
margin-left: 40px;
}
Equivalent Shorthand
.box {
margin: 10px 20px 30px 40px;
}
With four values, the order is top, right, bottom, and left.
One Value
.box {
margin: 20px;
}
All four margins are 20 pixels.
Two Values
.box {
margin: 10px 20px;
}
The top and bottom margins are 10 pixels, while the right and left margins are 20 pixels.
Three Values
.box {
margin: 10px 20px 30px;
}
The top margin is 10 pixels, the horizontal margins are 20 pixels, and the bottom margin is 30 pixels.
Shorthand Override
.box {
margin-top: 40px;
margin: 10px;
}
The final top margin is 10 pixels because the later shorthand resets all four margins.
Override Only the Top Margin
.box {
margin: 20px;
margin-top: 40px;
}
The top margin becomes 40 pixels, while the other three margins remain 20 pixels.
Physical vs. Logical Margins
The margin-top property always targets the physical top side. The logical margin-block-start and margin-block-end properties follow the element's writing mode.
Physical Top Margin
.box {
margin-top: 20px;
}
Logical Block-Start Margin
.box {
margin-block-start: 20px;
}
These declarations are equivalent in the default horizontal writing mode.
Comparison
| Feature | margin-top | margin-block-start |
|---|---|---|
| Physical side | Always top | Depends on writing mode |
| Horizontal writing mode | Top | Top |
vertical-rl |
Top | Right |
vertical-lr |
Top | Left |
| Writing-mode-aware | No | Yes |
Conflicting Declarations
.box {
margin-top: 10px;
margin-block-start: 20px;
}
In a horizontal writing mode, both declarations target the top margin. With equal specificity and importance, the later declaration wins.
Important Consideration
Use physical margins when spacing must remain on a fixed side. Use logical margins when spacing should adapt to different writing modes.
Flexbox and Grid
The margin-top property works with Flexbox and CSS Grid. These layout systems also avoid the vertical margin collapsing behavior found in normal block layout.
Flexbox Example
.flex-container {
display: flex;
flex-direction: column;
gap: 10px;
}
.flex-item:last-child {
margin-top: 20px;
}
The last item receives an additional 20-pixel top margin. The margin and the container's gap both contribute to the spacing.
Grid Example
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20px;
}
.grid-item {
margin-top: 10px;
}
Each grid item receives a 10-pixel top margin within its grid area.
Using gap Instead
.grid-container {
display: grid;
gap: 20px;
}
Use gap for consistent spacing between flex or grid items. Use margin-top when a specific item needs additional space above it.
Important Consideration
Margins of flex and grid items do not collapse. When using both margins and gap, account for their combined spacing.
Responsive Spacing
Top margins can be adjusted for different screen sizes using relative units, media queries, and CSS functions.
Media Query Example
.content-box {
margin-top: 40px;
}
@media (max-width: 768px) {
.content-box {
margin-top: 15px;
}
}
Relative Spacing
.content-box {
margin-top: 1.5rem;
}
Fluid Spacing
.content-box {
margin-top: clamp(1rem, 4vw, 3rem);
}
Important Consideration
Large fixed top margins may create excessive empty space on smaller screens. Responsive values can help maintain balanced spacing.
margin-top Example
This example demonstrates how different top margin values affect the vertical space between two boxes.
HTML Example
<div class="margin-top-demo">
<div class="demo-section">
<h3>No Top Margin</h3>
<div class="margin-area">
<div class="demo-box first-box">First Box</div>
<div class="demo-box no-margin">Second Box</div>
</div>
</div>
<div class="demo-section">
<h3>20px Top Margin</h3>
<div class="margin-area">
<div class="demo-box first-box">First Box</div>
<div class="demo-box medium-margin">Second Box</div>
</div>
</div>
<div class="demo-section">
<h3>40px Top Margin</h3>
<div class="margin-area">
<div class="demo-box first-box">First Box</div>
<div class="demo-box large-margin">Second Box</div>
</div>
</div>
</div>
CSS Example
.margin-top-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 20px;
}
.margin-top-demo .demo-section {
padding: 15px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
.margin-top-demo h3 {
margin: 0 0 12px;
font-size: 1rem;
}
.margin-top-demo .margin-area {
display: flex;
flex-direction: column;
padding: 12px;
background-color: #e2e8f0;
}
.margin-top-demo .demo-box {
padding: 12px;
border: 2px solid #2563eb;
background-color: #bfdbfe;
color: #1e3a8a;
text-align: center;
font-size: 0.875rem;
}
.margin-top-demo .first-box {
margin: 0;
border-color: #64748b;
background-color: #f1f5f9;
color: #334155;
}
.margin-top-demo .no-margin {
margin-top: 0;
}
.margin-top-demo .medium-margin {
margin-top: 20px;
}
.margin-top-demo .large-margin {
margin-top: 40px;
}
No Top Margin
20px Top Margin
40px Top Margin
As the top margin increases, the space between the first and second boxes increases. The example uses a column flex layout, so the margins do not collapse.
Change the Margin to 30px
.margin-top-demo .medium-margin {
margin-top: 30px;
}
Use a Relative Unit
.margin-top-demo .large-margin {
margin-top: 2rem;
}
Remove the Margin
.margin-top-demo .large-margin {
margin-top: 0;
}
Use a Negative Margin
.margin-top-demo .large-margin {
margin-top: -10px;
}
The negative margin pulls the second box upward, potentially causing it to overlap the first box.
Common Mistakes
Confusing Margin with Padding
margin-top creates space outside the border, while padding-top creates space inside it.
Expecting Vertical Margins to Always Add Together
Adjoining vertical margins in normal block layout may collapse rather than add together.
Expecting Percentage Values to Use Container Height
Percentage top margins are based on the containing block's width, not its height.
Expecting Auto to Center an Element Vertically
Automatic top margins generally resolve to zero in normal block layout. Flexbox and Grid have different automatic margin behavior.
Ignoring Parent-Child Margin Collapsing
A first child's top margin can collapse with its parent's margin under certain conditions.
Using Negative Margins Without Testing
Negative margins can cause unintended overlap or clipping.
Overwriting the Value with a Shorthand
.box {
margin-top: 40px;
margin: 10px;
}
The final top margin is 10 pixels.
Mixing Physical and Logical Margins
In horizontal writing modes, margin-top and margin-block-start target the same side. The cascade determines which declaration wins.
Using Margins Instead of gap for Every Layout
Use gap for consistent spacing between flex or grid items.
Important Considerations
Keep the following points in mind when using margin-top:
- The property controls the outer margin on the physical top side.
- The initial value is
0. - The property is not inherited.
- It always targets the top side regardless of writing mode or direction.
- CSS length values are supported.
- Percentage values are based on the containing block's width.
- The
autokeyword is supported. - Automatic top margins generally resolve to zero in normal block layout.
- Automatic margins can absorb free space in flex and grid layouts.
- Negative lengths and percentages are allowed.
- Negative margins can cause overlap or overflow.
- Margins are outside the element's border.
- Margins do not extend the element's background.
- Adjoining vertical margins may collapse in normal block layout.
- Positive adjoining margins typically collapse to the larger value.
- Positive and negative adjoining margins may combine during collapsing.
- A first child's top margin may collapse with its parent's margin.
- Flex and grid item margins do not collapse.
- The property can be set using the
marginshorthand. - Later shorthand declarations can override earlier top margins.
- Physical and logical margin declarations can affect the same side.
- The CSS cascade determines which conflicting declaration wins.
- Relative units can provide scalable spacing.
- Media queries can adjust margins for smaller screens.
- Use logical margins when spacing should follow the writing mode.
Browser Support
The CSS margin-top property is widely supported across modern and older browsers, including Chrome, Edge, Firefox, Safari, and Opera.
It is a long-established CSS property and does not require vendor prefixes.
For current compatibility information, visit Can I Use? and search for margin-top.
Specifications
The margin-top property is defined in the CSS Box Model Module Level 3 specification.
CSS Box Model Module Level 3: Physical Margin Properties
The specification defines physical margins, their accepted values, and how margins affect the CSS box model.
For additional documentation and examples, consult MDN Web Docs.
