CSS margin-bottom Property
The CSS margin-bottom property sets the outer margin below an element's bottom border, controlling the space between the element and surrounding content.
Unlike padding, a margin is outside the element's border and does not extend its background. The margin-bottom property uses the physical bottom side, regardless of the writing mode or text direction.
margin-bottom Syntax
The margin-bottom property accepts a CSS length, percentage, or the auto keyword. CSS-wide keywords are also supported.
Basic Syntax
selector {
margin-bottom: 20px;
}
Length Value
div {
margin-bottom: 2rem;
}
Percentage Value
div {
margin-bottom: 5%;
}
Auto Value
div {
margin-bottom: auto;
}
Negative Value
div {
margin-bottom: -15px;
}
CSS-Wide Keywords
div {
margin-bottom: initial;
}
div {
margin-bottom: inherit;
}
div {
margin-bottom: unset;
}
div {
margin-bottom: revert;
}
div {
margin-bottom: revert-layer;
}
The initial value of margin-bottom is 0.
Property Values
| Value | Description |
|---|---|
<length> |
Sets the bottom margin using a CSS length such as px, em, rem, or vw. |
<percentage> |
Sets the bottom margin as a percentage of the containing block's inline size. |
auto |
Allows the browser to calculate the bottom margin according to the layout context. |
initial |
Resets the property to its initial value of 0. |
inherit |
Inherits the computed value from the parent element. |
unset |
Resets the property to its initial value 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 | Bottom |
| Logical equivalent | margin-block-end in horizontal-tb |
How It Works
The margin-bottom property adds space outside the bottom border of an element. In normal block layout, this commonly creates separation between vertically stacked elements.
HTML Example
<div class="content-box">
First Content Box
</div>
<div class="content-box">
Second Content Box
</div>
CSS Example
.content-box {
margin-bottom: 20px;
padding: 15px;
border: 2px solid #2563eb;
background-color: #dbeafe;
}
Each box has a 20-pixel bottom margin. The space between the boxes depends on any adjoining margins and whether margin collapsing applies.
Remove the Bottom Margin
.content-box:last-child {
margin-bottom: 0;
}
This is useful when the last item in a group should not add extra space after itself.
Margin vs. Padding
Both margins and padding create spacing, but they affect different parts of the CSS box model.
Bottom Margin
.box {
margin-bottom: 20px;
}
The margin creates space outside the bottom border.
Bottom Padding
.box {
padding-bottom: 20px;
}
The padding creates space inside the bottom border, between the content and border.
Comparison
| Feature | margin-bottom | padding-bottom |
|---|---|---|
| Location | Outside the border | Inside the border |
| Background coverage | Does not extend into the margin | Background extends into the padding by default |
| Negative values | Allowed | Not allowed |
| Margin collapsing | Possible | Not applicable |
Important Consideration
Use margins to separate elements and padding to create space within an element's border.
Length Values
The margin-bottom property accepts absolute and relative CSS length units.
Pixels
.box {
margin-bottom: 24px;
}
Root Font Size
.box {
margin-bottom: 2rem;
}
Element Font Size
.box {
margin-bottom: 1.5em;
}
Viewport Units
.box {
margin-bottom: 3vw;
}
Calculated Value
.box {
margin-bottom: calc(1rem + 10px);
}
Important Consideration
Relative units such as rem can help maintain consistent spacing when font sizes change.
Percentage Values
A percentage bottom margin is calculated relative to the containing block's inline size, which is normally its width in a horizontal writing mode.
Percentage Example
.box {
margin-bottom: 5%;
}
Percentage with a Fixed Container Width
.container {
width: 600px;
}
.container .box {
margin-bottom: 10%;
}
In a horizontal writing mode, a 10% bottom margin is 60 pixels when the containing block's width is 600 pixels.
Important Consideration
The percentage is not normally calculated from the containing block's height. Use a length value when a fixed amount of vertical spacing is needed.
Auto Margins
The auto keyword allows the browser to determine the bottom margin according to the formatting context.
Basic Example
.box {
margin-bottom: auto;
}
In normal block layout, an automatic bottom margin generally resolves to zero.
Flexbox Example
.flex-container {
display: flex;
flex-direction: column;
min-height: 300px;
}
.flex-item:first-child {
margin-bottom: auto;
}
When the flex container has available vertical space, the first item's automatic bottom margin can absorb that space and push subsequent items toward the bottom.
Important Consideration
Automatic margins are especially useful in flex and grid layouts, but their effect depends on the available free space and layout rules.
Negative Margins
Negative bottom margins reduce the space an element contributes after itself and can pull following content upward.
Negative Length
.box {
margin-bottom: -20px;
}
Negative Percentage
.box {
margin-bottom: -5%;
}
Overlapping Sections
.featured-section {
margin-bottom: -30px;
}
.following-section {
position: relative;
padding: 20px;
background-color: #f1f5f9;
}
The negative margin reduces the separation between the sections and may cause overlap.
Important Consideration
Negative margins can be useful for deliberate visual effects, but they may also cause unintended overlap or overflow at smaller viewport sizes.
Margin Collapsing
In normal block layout, adjoining vertical margins can collapse into a single margin rather than adding together.
Adjacent Elements
.first-box {
margin-bottom: 30px;
}
.second-box {
margin-top: 20px;
}
When these margins collapse, the resulting space is 30 pixels rather than 50 pixels.
Negative Margin Collapsing
.first-box {
margin-bottom: 30px;
}
.second-box {
margin-top: -10px;
}
If these margins collapse, the resulting margin is 20 pixels because the largest positive margin is combined with the most negative margin.
Prevent Parent-Child Margin Collapsing
.container {
display: flow-root;
}
A new block formatting context prevents the container's margins from collapsing with its children's margins.
Flexbox and Grid
Margins between flex items and grid items do not collapse.
Important Consideration
Margin collapsing depends on the formatting context and the relationship between adjoining margins. It does not apply to every pair of vertical margins.
margin Shorthand
The margin shorthand sets the margins on all four physical sides of an element.
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 left and right margins are 20 pixels.
Three Values
.box {
margin: 10px 20px 30px;
}
The top margin is 10 pixels, the left and right margins are 20 pixels, and the bottom margin is 30 pixels.
Four Values
.box {
margin: 10px 20px 30px 40px;
}
The values apply in clockwise order: top, right, bottom, and left.
Shorthand Reset Behavior
.box {
margin-bottom: 40px;
margin: 10px;
}
The final bottom margin is 10 pixels because the later shorthand resets all four margins.
Logical Margins
The margin-bottom property always affects the physical bottom side. Logical margin properties adapt to the writing mode.
Physical Bottom Margin
.box {
margin-bottom: 20px;
}
Logical Block-End Margin
.box {
margin-block-end: 20px;
}
In the default horizontal-tb writing mode, both declarations affect the bottom margin.
Writing Mode Comparison
| Writing Mode | margin-bottom | margin-block-end |
|---|---|---|
horizontal-tb |
Bottom | Bottom |
vertical-rl |
Bottom | Left |
vertical-lr |
Bottom | Right |
Important Consideration
When a physical and logical margin target the same side, the CSS cascade determines which declaration wins.
Flexbox and Grid
The margin-bottom property works with Flexbox and CSS Grid. It can create additional spacing below individual items.
Flexbox Example
.flex-container {
display: flex;
flex-direction: column;
}
.flex-item {
margin-bottom: 15px;
}
Grid Example
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
.grid-item {
margin-bottom: 20px;
}
Using gap Instead
.grid-container {
display: grid;
gap: 20px;
}
The gap property is often preferable when all items need consistent spacing because it adds space between items without adding margins to their outside edges.
Important Consideration
Use margin-bottom for spacing below a specific element and gap for consistent spacing between flex or grid items.
Responsive Spacing
Bottom margins can be adjusted for different viewport sizes using relative units, media queries, and CSS functions.
Media Query Example
.content-section {
margin-bottom: 40px;
}
@media (max-width: 768px) {
.content-section {
margin-bottom: 20px;
}
}
Using rem Units
.content-section {
margin-bottom: 2rem;
}
Fluid Spacing with clamp()
.content-section {
margin-bottom: clamp(1rem, 3vw, 3rem);
}
Important Consideration
Responsive spacing should remain proportional to the surrounding content. Avoid unnecessarily large fixed margins on narrow screens.
margin-bottom Example
This example demonstrates how different bottom margin values change the spacing between vertically stacked boxes.
HTML Example
<div class="margin-bottom-demo">
<div class="demo-section">
<h3>No Bottom Margin</h3>
<div class="margin-area">
<div class="demo-box no-margin">First Box</div>
<div class="demo-box following-box">Second Box</div>
</div>
</div>
<div class="demo-section">
<h3>20px Bottom Margin</h3>
<div class="margin-area">
<div class="demo-box medium-margin">First Box</div>
<div class="demo-box following-box">Second Box</div>
</div>
</div>
<div class="demo-section">
<h3>40px Bottom Margin</h3>
<div class="margin-area">
<div class="demo-box large-margin">First Box</div>
<div class="demo-box following-box">Second Box</div>
</div>
</div>
</div>
CSS Example
.margin-bottom-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 20px;
}
.margin-bottom-demo .demo-section {
padding: 15px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
.margin-bottom-demo h3 {
margin: 0 0 12px;
font-size: 1rem;
}
.margin-bottom-demo .margin-area {
padding: 12px;
background-color: #e2e8f0;
}
.margin-bottom-demo .demo-box {
padding: 12px;
border: 2px solid #2563eb;
background-color: #bfdbfe;
color: #1e3a8a;
text-align: center;
font-size: 0.875rem;
}
.margin-bottom-demo .no-margin {
margin-bottom: 0;
}
.margin-bottom-demo .medium-margin {
margin-bottom: 20px;
}
.margin-bottom-demo .large-margin {
margin-bottom: 40px;
}
.margin-bottom-demo .following-box {
margin: 0;
border-color: #64748b;
background-color: #f1f5f9;
color: #334155;
}
No Bottom Margin
20px Bottom Margin
40px Bottom Margin
Increasing the bottom margin on the first box creates more space before the second box. The margin is outside the first box's border.
Change the Margin to 30px
.margin-bottom-demo .medium-margin {
margin-bottom: 30px;
}
Use a Relative Unit
.margin-bottom-demo .large-margin {
margin-bottom: 2rem;
}
Remove the Margin
.margin-bottom-demo .large-margin {
margin-bottom: 0;
}
Common Mistakes
Confusing Margin with Padding
Margins create space outside the border, while padding creates space inside it.
Expecting Percentage Values to Use Container Height
Percentage bottom margins are based on the containing block's inline size.
Expecting Auto to Always Create Space
In normal block layout, an automatic bottom margin generally resolves to zero.
Forgetting Margin Collapsing
Adjacent vertical margins can collapse instead of adding together.
Using Negative Margins Without Testing
Negative bottom margins can cause overlap or overflow.
Overwriting the Value with a Shorthand
.box {
margin-bottom: 40px;
margin: 10px;
}
The final bottom margin is 10 pixels.
Mixing Physical and Logical Margins
margin-bottom and margin-block-end target the same side in a horizontal writing mode.
Using Margins Instead of gap for Every Layout
Use gap for consistent spacing between flex or grid items when appropriate.
Important Considerations
Keep the following points in mind when using margin-bottom:
- The property controls the outer margin below an element.
- It is a physical margin property.
- The initial value is
0. - The property is not inherited.
- It always targets the physical bottom side.
- CSS length values are supported.
- Percentage values are based on the containing block's inline size.
- The
autokeyword is supported. - Negative lengths and percentages are allowed.
- Negative margins can cause overlap or overflow.
- Adjoining vertical margins can collapse in normal block layout.
- Margins between flex and grid items do not collapse.
- Margins are outside an element's border.
- Margins do not extend the element's background.
- Padding creates internal spacing, while margins create external spacing.
- The
marginshorthand can set the bottom margin. - In
horizontal-tb,margin-block-endtargets the same side. - Physical and logical margin declarations can conflict.
- The CSS cascade determines which conflicting declaration wins.
- The property works with Flexbox and CSS Grid.
- The
gapproperty is useful for uniform spacing between layout items. - Relative units can provide scalable spacing.
- Media queries can adjust margins for smaller screens.
- Removing the last child's bottom margin can prevent unnecessary spacing at the end of a component.
Browser Support
The CSS margin-bottom property is widely supported across modern browsers, including Chrome, Edge, Firefox, Safari, and Opera.
It has been supported for many years and generally does not require vendor prefixes.
For current compatibility information, visit Can I Use? and search for margin-bottom.
Specifications
The margin-bottom property is defined in the CSS Box Model Module Level 3 specification.
CSS Box Model Module Level 3: Physical Margins
The specification defines how physical margins affect spacing around an element's border box.
For additional documentation and examples, consult MDN Web Docs.
