CSS margin Property
The CSS margin property is a shorthand property that sets the outer spacing around an element by defining its top, right, bottom, and left margins.
Margins create space outside an element's border and can separate elements, center certain block-level elements, or adjust their position in a layout. The shorthand accepts one to four values, including lengths, percentages, and the auto keyword. Margins can also be negative.
margin Syntax
The margin shorthand accepts one, two, three, or four values. Each value can be a length, percentage, or auto.
Basic Syntax
selector {
margin: 20px;
}
One Value
div {
margin: 20px;
}
Applies 20 pixels to all four sides.
Two Values
div {
margin: 10px 20px;
}
Applies 10 pixels to the top and bottom, and 20 pixels to the right and left.
Three Values
div {
margin: 10px 20px 30px;
}
Applies 10 pixels to the top, 20 pixels to the right and left, and 30 pixels to the bottom.
Four Values
div {
margin: 10px 20px 30px 40px;
}
Applies the values clockwise: top, right, bottom, and left.
CSS-Wide Keywords
div {
margin: initial;
}
div {
margin: inherit;
}
div {
margin: unset;
}
div {
margin: revert;
}
div {
margin: revert-layer;
}
The initial value of each margin side is 0.
Property Values
The margin property supports the following value types.
| Value | Description |
|---|---|
<length> |
Specifies a fixed or relative length using units such as px, em, rem, or vw. |
<percentage> |
Specifies a percentage relative to the width of the containing block in ordinary horizontal writing modes. |
auto |
Lets the browser calculate the margin according to the applicable layout rules. |
initial |
Resets all four margins to 0. |
inherit |
Inherits the computed margin values from the parent element. |
unset |
Resets all four margins to their initial value because margins are not inherited. |
revert |
Reverts the margins according to cascade origin rules. |
revert-layer |
Reverts the margins according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | 0 for each side |
| Inherited | No |
| Applies to | All elements, except elements with table display types other than table-caption, table, and inline-table |
| Computed value | Each side as a computed length, percentage, or auto |
| Animation type | Each margin side animates as a length, percentage, or calculated combination |
| Negative values | Allowed for lengths and percentages |
| Shorthand for | margin-top, margin-right, margin-bottom, and margin-left |
The shorthand sets all four physical margin properties, even when fewer than four values are supplied.
How It Works
Margins occupy space outside an element's border. Unlike padding, margins do not form part of the element's background painting area.
HTML Example
<div class="example-box">
CSS Margin Example
</div>
CSS Example
.example-box {
margin: 20px;
padding: 15px;
border: 2px solid #2563eb;
background-color: #dbeafe;
}
The element receives 20 pixels of margin outside its border and 15 pixels of padding between its border and content.
The CSS Box Model
The box model describes four main areas, from the inside outward:
- Content: The text, images, or other content inside the element.
- Padding: The space between the content and border.
- Border: The boundary surrounding the padding and content.
- Margin: The outer space surrounding the border.
Important Consideration
Margins affect layout spacing but do not increase the dimensions of the element's border box. In normal block layout, adjacent vertical margins may collapse rather than add together.
One Value
When one value is supplied, it applies to all four sides.
Example
.box {
margin: 20px;
}
This is equivalent to:
.box {
margin-top: 20px;
margin-right: 20px;
margin-bottom: 20px;
margin-left: 20px;
}
Using Relative Units
.box {
margin: 1.5rem;
}
All four margins use a value relative to the root element's font size.
Removing All Margins
.box {
margin: 0;
}
This resets all four margins to zero.
Two Values
When two values are supplied, the first applies to the top and bottom, and the second applies to the right and left.
Example
.box {
margin: 10px 30px;
}
This is equivalent to:
.box {
margin-top: 10px;
margin-right: 30px;
margin-bottom: 10px;
margin-left: 30px;
}
Vertical Spacing with Automatic Horizontal Margins
.container {
width: 80%;
margin: 20px auto;
}
This applies 20 pixels of vertical margin and automatically calculates the horizontal margins. In a suitable block layout, the container is centered horizontally.
Three Values
When three values are supplied, they represent the top, horizontal sides, and bottom.
Example
.box {
margin: 10px 20px 30px;
}
This is equivalent to:
.box {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 30px;
margin-left: 20px;
}
Common Use
.section-heading {
margin: 0 0 1.5rem;
}
This removes the top and horizontal margins while adding space below the heading.
Four Values
When four values are supplied, they apply clockwise from the top: top, right, bottom, and left.
Example
.box {
margin: 10px 20px 30px 40px;
}
This is equivalent to:
.box {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 30px;
margin-left: 40px;
}
Mixed Units
.box {
margin: 1rem 20px 2em 5%;
}
Each side can use a different supported unit or value type.
Important Consideration
Remember the clockwise order: top, right, bottom, left. The same ordering convention is used by several CSS box model shorthands.
Auto Margins
The auto keyword allows the browser to calculate a margin based on the layout context.
Center a Block Horizontally
.centered-box {
width: 400px;
max-width: 100%;
margin: 0 auto;
}
In normal block layout, the left and right auto margins divide the available horizontal space when the element is narrower than its containing block.
Center a Responsive Container
.page-container {
width: 90%;
max-width: 1100px;
margin-inline: auto;
}
This uses logical inline margins to center the container across different writing directions.
Push a Flex Item to the End
.toolbar {
display: flex;
}
.toolbar .last-item {
margin-left: auto;
}
In a left-to-right flex row, the automatic left margin absorbs available space and pushes the item toward the right.
Important Consideration
Automatic margins behave differently depending on the formatting context. They do not automatically center every element in every layout.
Negative Margins
Margins can use negative lengths or percentages. Negative margins can reduce spacing, pull elements closer together, or create overlapping layouts.
Negative Top Margin
.overlapping-box {
margin-top: -20px;
}
This can move the element upward relative to its normal position in the layout.
Negative Left Margin
.offset-box {
margin-left: -15px;
}
Mixed Positive and Negative Margins
.box {
margin: -10px 20px 30px -5px;
}
Overlapping a Content Panel
.content-panel {
position: relative;
margin-top: -30px;
padding: 20px;
background-color: #fff;
}
Important Consideration
Negative margins affect layout calculations and may cause elements to overlap or extend beyond their containers. Test carefully at different viewport widths.
Percentage Margins
Percentage margins are calculated relative to the containing block's inline size, which is its width in a horizontal writing mode.
Horizontal Percentage Margins
.box {
margin: 0 5%;
}
Percentage Margins on All Sides
.box {
margin: 5%;
}
Percentage Top Margin
.box {
margin-top: 10%;
}
In ordinary horizontal writing modes, the percentage is based on the containing block's width, not its height.
Responsive Example
.content-section {
margin: 4% 6%;
}
The margins change as the containing block's inline size changes.
Important Consideration
Percentage-based top and bottom margins can be surprising because they are not normally calculated from the containing block's height. For predictable vertical spacing, consider rem, em, or other appropriate length units.
Margin Collapsing
In normal block layout, adjoining vertical margins can collapse into a single margin rather than being added together.
Adjacent Block Margins
.first-box {
margin-bottom: 30px;
}
.second-box {
margin-top: 20px;
}
If the two vertical margins collapse, the resulting space is 30 pixels rather than 50 pixels.
Parent and Child Margins
A first or last in-flow child's vertical margin may collapse with its parent's margin when no border, padding, inline content, clearance, or other layout condition separates them.
Positive and Negative Margins
When collapsing margins include positive and negative values, the resulting margin is calculated using the largest positive margin and the most negative margin.
Prevent Collapsing with a New Formatting Context
.container {
display: flow-root;
}
A flow-root container establishes a new block formatting context, preventing its margins from collapsing with its children's margins.
Margins in Flexbox and Grid
Margins between flex items or grid items do not collapse.
Important Consideration
Margin collapsing applies to certain vertical margins in block formatting contexts. Horizontal margins do not collapse, and not every pair of vertical margins qualifies.
Margin vs. Padding
Margins and padding both create space, but they occupy different areas of the CSS box model.
| Feature | Margin | Padding |
|---|---|---|
| Location | Outside the border | Inside the border |
| Background | Does not paint the element's background | Covered by the element's background by default |
| Negative values | Allowed | Not allowed |
| Auto value | Supported | Not supported |
| Collapsing | Some vertical margins can collapse | Does not collapse |
Margin Example
.box {
margin: 20px;
border: 2px solid #2563eb;
}
Padding Example
.box {
padding: 20px;
border: 2px solid #2563eb;
}
Using Both
.card {
margin: 20px;
padding: 20px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
The margin separates the card from surrounding elements, while the padding creates space between the card's content and border.
Flexbox and Grid
Margins can be used with Flexbox and CSS Grid, although these layout systems also provide the gap property for spacing between items.
Flexbox with Margins
.flex-container {
display: flex;
}
.flex-item {
margin: 10px;
}
Flexbox with Gap
.flex-container {
display: flex;
gap: 20px;
}
The gap property creates spacing between flex items without adding margins around their outer edges.
Grid with Margins
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
.grid-item {
margin: 10px;
}
Grid with Gap
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
Auto Margins in Flexbox
.navigation {
display: flex;
align-items: center;
}
.navigation .account-link {
margin-left: auto;
}
Important Consideration
Use gap for consistent spacing between flex or grid items, and margins when individual elements require additional spacing or alignment.
Logical Margins
The margin shorthand sets physical sides. CSS also provides logical margin properties that follow the writing mode and text direction.
Physical Margins
.box {
margin-top: 20px;
margin-right: 15px;
margin-bottom: 20px;
margin-left: 15px;
}
Logical Margin Shorthands
.box {
margin-block: 20px;
margin-inline: 15px;
}
Logical Inline Margins
.box {
margin-inline-start: 15px;
margin-inline-end: 15px;
}
Logical Block Margins
.box {
margin-block-start: 20px;
margin-block-end: 20px;
}
Important Consideration
Logical properties are useful for multilingual layouts because their start and end sides adapt to writing modes. Physical and logical declarations can affect the same margin side, so the CSS cascade determines which declaration takes precedence.
Shorthand Reset Behavior
The margin shorthand sets all four physical margins. A later shorthand declaration can overwrite earlier individual margin declarations.
Individual Margin Followed by Shorthand
.box {
margin-top: 30px;
margin: 10px;
}
The final top margin is 10 pixels because the shorthand resets all four sides.
Shorthand Followed by Individual Margin
.box {
margin: 10px;
margin-top: 30px;
}
The final top margin is 30 pixels, while the other three margins remain 10 pixels.
Mixed Physical and Logical Properties
.box {
margin: 20px;
margin-inline: auto;
}
The logical inline margins override the corresponding physical sides in the applicable writing mode, assuming the declarations have equal cascade priority.
Important Consideration
Declaration order matters when the same side is assigned by multiple properties at the same cascade priority. Check for conflicting margin declarations when spacing behaves unexpectedly.
Responsive Margins
Margins can change across viewport sizes to provide appropriate spacing for desktop, tablet, and mobile layouts.
Media Query Example
.content-section {
margin: 40px;
}
@media (max-width: 768px) {
.content-section {
margin: 20px;
}
}
Using rem Units
.content-section {
margin: 2rem;
}
The margin scales with the root font size.
Using clamp()
.content-section {
margin-block: clamp(1rem, 3vw, 3rem);
}
The block-axis margins adjust fluidly within the specified minimum and maximum values.
Responsive Centered Container
.page-content {
width: min(90%, 1100px);
margin-inline: auto;
}
Important Consideration
Avoid large fixed margins that reduce available content width on small screens. Responsive units, logical properties, and media queries help maintain balanced spacing.
margin Example
This example compares different margin shorthand values and demonstrates horizontal centering with automatic margins.
HTML Example
<div class="margin-demo">
<div class="demo-section">
<h3>One Value: 15px</h3>
<div class="margin-area">
<div class="demo-box one-value">All Sides</div>
</div>
</div>
<div class="demo-section">
<h3>Two Values: 10px 25px</h3>
<div class="margin-area">
<div class="demo-box two-values">Vertical / Horizontal</div>
</div>
</div>
<div class="demo-section">
<h3>Four Values: 5px 10px 20px 30px</h3>
<div class="margin-area">
<div class="demo-box four-values">Top / Right / Bottom / Left</div>
</div>
</div>
<div class="demo-section">
<h3>Auto Margins</h3>
<div class="margin-area">
<div class="demo-box auto-margin">Centered Box</div>
</div>
</div>
</div>
CSS Example
.margin-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 20px;
}
.margin-demo .demo-section {
padding: 15px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
.margin-demo h3 {
margin: 0 0 12px;
font-size: 1rem;
}
.margin-demo .margin-area {
display: flow-root;
min-height: 100px;
background-color: #e2e8f0;
}
.margin-demo .demo-box {
padding: 12px;
border: 2px solid #2563eb;
background-color: #bfdbfe;
color: #1e3a8a;
text-align: center;
font-size: 0.875rem;
}
.margin-demo .one-value {
margin: 15px;
}
.margin-demo .two-values {
margin: 10px 25px;
}
.margin-demo .four-values {
margin: 5px 10px 20px 30px;
}
.margin-demo .auto-margin {
width: 70%;
margin: 15px auto;
}
One Value: 15px
Two Values: 10px 25px
Four Values: 5px 10px 20px 30px
Auto Margins
The gray areas show the space available to each example. The blue boxes demonstrate how margins change the distance between an element's border and its surrounding layout.
Change All Margins
.margin-demo .one-value {
margin: 25px;
}
Remove the Margins
.margin-demo .two-values {
margin: 0;
}
Center the Box with Auto Margins
.margin-demo .auto-margin {
width: 60%;
margin: 20px auto;
}
These changes demonstrate how the same property can control spacing on every side or allow the browser to calculate horizontal alignment.
Common Mistakes
Confusing Margin with Padding
Margin creates space outside the border. Padding creates space inside the border.
Using the Wrong Value Order
Four values follow the clockwise order: top, right, bottom, left.
Expecting Vertical Margins to Always Add Together
Adjacent vertical margins can collapse in normal block layout.
Assuming auto Always Centers an Element
Automatic margins depend on the layout context and available space. A normal block with an automatic width does not become horizontally centered merely by adding margin: auto.
Using Percentage Top Margins as a Percentage of Height
In ordinary horizontal writing modes, percentage margins are based on the containing block's width.
Overusing Negative Margins
Negative margins can cause unintended overlap and overflow, especially on responsive layouts.
Forgetting the Shorthand Resets All Four Sides
.box {
margin-left: 40px;
margin: 10px;
}
The final left margin is 10 pixels.
Using Margins Instead of gap for Every Flex or Grid Layout
The gap property is often simpler for consistent spacing between flex or grid items.
Ignoring Browser Default Margins
Elements such as headings, paragraphs, and lists may have default margins supplied by the browser stylesheet.
Using Fixed Margins That Cause Mobile Overflow
Large horizontal margins can leave too little space for content on narrow screens.
Important Considerations
Keep the following points in mind when using margin:
- The property is a shorthand for four physical margin sides.
- Margins create space outside an element's border.
- The initial value of each margin side is
0. - The property is not inherited.
- The shorthand accepts one to four values.
- One value applies to all four sides.
- Two values represent vertical and horizontal margins.
- Three values represent top, horizontal sides, and bottom.
- Four values follow the order top, right, bottom, left.
- Lengths can use absolute or relative CSS units.
- Percentages are based on the containing block's inline size.
- The
autokeyword is supported. - Horizontal auto margins can center certain block elements.
- Auto margins have additional uses in Flexbox and Grid.
- Negative lengths and percentages are allowed.
- Negative margins can produce overlap or overflow.
- Some adjoining vertical margins collapse in block layout.
- Horizontal margins do not collapse.
- Margins between flex or grid items do not collapse.
- The
flow-rootdisplay value establishes a new block formatting context. - Margins and padding occupy different areas of the box model.
- Margins do not paint the element's background.
- Padding does not support negative or auto values.
- The
gapproperty is useful for flex and grid spacing. - Logical margin properties adapt to writing modes.
- Physical and logical margin declarations can affect the same side.
- The shorthand resets all four physical margin properties.
- Browser default styles may include margins.
- Responsive spacing should be tested at different viewport widths.
- Use margins to separate elements and padding to space content within them.
Browser Support
The CSS margin property is widely supported across modern browsers, including Chrome, Edge, Firefox, Safari, and Opera.
The basic shorthand syntax, length and percentage values, negative margins, and automatic margins have long-established browser support. Specific layout behaviors, such as margin collapsing and automatic alignment, depend on the applicable CSS formatting context.
For current compatibility information, visit Can I Use? and search for margin.
Specifications
The margin property is defined in the CSS Box Model Module Level 3 specification.
CSS Box Model Module Level 3: margin
The specification defines the shorthand syntax and the margin properties that control spacing outside the border box.
For additional documentation and examples, consult MDN Web Docs.
