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.
Fundamentals
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.
Foundation
📝 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;}
Cheat Sheet
⚡ Quick Reference
Question
Answer
Initial value
0
Applies to
All elements except table display types that use separate border model
Inherited
No
Animatable
Yes, as a length
Logical equivalent
margin-block-start (in horizontal LTR writing)
Reference
💎 Property Values
Value
Example
Meaning
Length
margin-top: 20px;
Fixed margin using px, em, rem, vw, etc.
Percentage
margin-top: 10%;
Margin as a percentage of the containing block width
auto
margin-top: auto;
Browser calculates the margin; rarely used for vertical centering with margin-top
inherit
margin-top: inherit;
Takes the computed value from the parent element
initial
margin-top: initial;
Resets to the default value (0)
unset
margin-top: unset;
Resets to inherit or initial depending on context
margin-top: 1rem;margin-top: 24px;margin-top: 5%;
Default
🎯 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.
Preview
👀 Live Preview
A heading followed by a box with top margin pushing it down:
Heading above
margin-top: 1.25rem
Hands-On
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>
For simple English pages, margin-top is familiar and widely used. Choose margin-block-start when layouts must adapt to writing mode.
Context
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.
A11y
♿ 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.
Compatibility
🖥 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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Legacy & Chromium
Full support
OperaAll versions · Modern & legacy
Full support
margin-top property99% supported
Bottom line: Safe to use everywhere. For writing-mode-aware spacing, see margin-block-start.
Wrap Up
🎉 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.