CSS margin-block-end Property
The CSS margin-block-end property sets the outer margin at the end of an element's block axis, with the physical side determined by its writing mode.
In a typical horizontal writing mode, margin-block-end corresponds to margin-bottom. In vertical writing modes, it corresponds to either the left or right margin. This makes the property useful for creating layouts that adapt to different writing directions.
margin-block-end Syntax
The margin-block-end property accepts a length, percentage, or the auto keyword. It also supports CSS-wide keywords.
Basic Syntax
selector {
margin-block-end: 20px;
}
Length Value
div {
margin-block-end: 2rem;
}
Percentage Value
div {
margin-block-end: 5%;
}
Auto Value
div {
margin-block-end: auto;
}
Negative Value
div {
margin-block-end: -15px;
}
CSS-Wide Keywords
div {
margin-block-end: initial;
}
div {
margin-block-end: inherit;
}
div {
margin-block-end: unset;
}
div {
margin-block-end: revert;
}
div {
margin-block-end: revert-layer;
}
The initial value of margin-block-end is 0.
Property Values
| Value | Description |
|---|---|
<length> |
Sets the margin using a CSS length such as px, em, rem, or vw. |
<percentage> |
Sets the margin as a percentage of the containing block's inline size. |
auto |
Allows the browser to calculate the margin according to the layout context. |
initial |
Resets the margin to 0. |
inherit |
Inherits the computed value from the parent element. |
unset |
Resets the margin to its initial value because the property is not inherited. |
revert |
Reverts the value according to cascade origin rules. |
revert-layer |
Reverts the value according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | 0 |
| Inherited | No |
| Applies to | Same as the corresponding physical margin properties |
| Computed value | Computed length, percentage, or auto |
| Animation type | Length, percentage, or calculated combination |
| Negative values | Allowed for lengths and percentages |
| Logical axis | Block axis |
| Physical equivalent | Depends on writing-mode |
How It Works
The block axis describes the direction in which blocks of content are arranged. The margin-block-end property adds space outside the element's border at the end of that axis.
HTML Example
<div class="content-box">
First Content Box
</div>
<div class="content-box">
Second Content Box
</div>
CSS Example
.content-box {
margin-block-end: 20px;
padding: 15px;
border: 2px solid #2563eb;
background-color: #dbeafe;
}
In a horizontal writing mode, each box receives a 20-pixel bottom margin, creating space after it in the block direction.
Equivalent Physical Property
.content-box {
margin-bottom: 20px;
}
This produces the same result when the writing mode is horizontal-tb.
Important Consideration
The margin is outside the element's border. It does not add internal spacing or extend the element's background.
Writing Modes
The writing-mode property determines which physical side corresponds to block-end.
| Writing Mode | Block-End Side | Physical Equivalent |
|---|---|---|
horizontal-tb |
Bottom | margin-bottom |
vertical-rl |
Left | margin-left |
vertical-lr |
Right | margin-right |
Horizontal Writing Mode
.horizontal-box {
writing-mode: horizontal-tb;
margin-block-end: 20px;
}
Vertical Right-to-Left Writing Mode
.vertical-rl-box {
writing-mode: vertical-rl;
margin-block-end: 20px;
}
Vertical Left-to-Right Writing Mode
.vertical-lr-box {
writing-mode: vertical-lr;
margin-block-end: 20px;
}
Important Consideration
The direction property controls inline text progression but does not reverse the block-end side in horizontal-tb.
Physical vs. Logical Margins
Physical margins refer to fixed sides, while logical margins follow the writing mode.
Physical Margin
.box {
margin-bottom: 20px;
}
Logical Margin
.box {
margin-block-end: 20px;
}
Both declarations affect the bottom margin in a horizontal writing mode.
Comparison
| Feature | margin-bottom | margin-block-end |
|---|---|---|
| Physical side | Always bottom | Depends on writing mode |
| Horizontal writing mode | Bottom | Bottom |
| Vertical writing modes | Bottom | Left or right |
| Writing-mode aware | No | Yes |
Important Consideration
Logical margins are useful when the same component must work in multiple writing modes without separate physical margin declarations.
Length Values
The property supports CSS length units, including absolute and relative lengths.
Pixels
.box {
margin-block-end: 24px;
}
Root Font Size
.box {
margin-block-end: 2rem;
}
Element Font Size
.box {
margin-block-end: 1.5em;
}
Viewport Units
.box {
margin-block-end: 3vw;
}
Calculated Value
.box {
margin-block-end: calc(1rem + 10px);
}
Important Consideration
Relative units can help maintain consistent spacing as font sizes or viewport dimensions change.
Percentage Values
Percentage values are calculated relative to the containing block's inline size, which is normally its width in a horizontal writing mode.
Percentage Example
.box {
margin-block-end: 5%;
}
Percentage with a Fixed Container Width
.container {
width: 600px;
}
.container .box {
margin-block-end: 10%;
}
In a horizontal writing mode, the 10% margin is calculated from the containing block's width, giving a margin of 60 pixels when that width is 600 pixels.
Important Consideration
A percentage block-end margin is not normally based on the containing block's height. For predictable spacing, rem or another length unit may be easier to use.
Auto Margins
The auto keyword allows the browser to calculate the block-end margin according to the layout context.
Basic Example
.box {
margin-block-end: auto;
}
Flexbox Example
.flex-container {
display: flex;
flex-direction: column;
min-height: 300px;
}
.flex-item:first-child {
margin-block-end: auto;
}
In a typical horizontal writing mode, the first item can absorb available vertical space after itself, pushing following flex items toward the bottom of the container.
Important Consideration
Automatic block-end margins do not always create visible spacing. Their behavior depends on the formatting context and available free space.
Negative Margins
Negative values reduce the margin contribution and can pull following content closer or cause elements to overlap.
Negative Length
.box {
margin-block-end: -20px;
}
Negative Percentage
.box {
margin-block-end: -5%;
}
Overlapping Sections
.featured-section {
margin-block-end: -30px;
}
.following-section {
position: relative;
padding: 20px;
background-color: #f1f5f9;
}
Important Consideration
Negative margins can cause unintended overlap or overflow. Test them carefully when building responsive layouts.
Margin Collapsing
In normal block layout, adjoining vertical margins may collapse rather than add together.
Adjacent Elements
.first-box {
margin-block-end: 30px;
}
.second-box {
margin-block-start: 20px;
}
In a horizontal writing mode, if the margins collapse, the resulting space is 30 pixels rather than 50 pixels.
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 applies only when the relevant layout conditions are met. Logical margins do not automatically collapse in every writing mode or formatting context.
margin-block Shorthand
The margin-block shorthand sets both block-start and block-end margins.
Individual Properties
.box {
margin-block-start: 10px;
margin-block-end: 30px;
}
Equivalent Shorthand
.box {
margin-block: 10px 30px;
}
Equal Block Margins
.box {
margin-block: 20px;
}
Shorthand Reset Behavior
.box {
margin-block-end: 30px;
margin-block: 10px;
}
The final block-end margin is 10 pixels because the later shorthand resets both block-axis margins.
Flexbox and Grid
The margin-block-end property works with Flexbox and CSS Grid. It can add spacing after individual items without affecting every item in the layout.
Flexbox Example
.flex-container {
display: flex;
flex-direction: column;
}
.flex-item {
margin-block-end: 15px;
}
Grid Example
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
.grid-item {
margin-block-end: 20px;
}
Using gap Instead
.grid-container {
display: grid;
gap: 20px;
}
The gap property is often preferable for consistent spacing between flex or grid items because it does not add spacing around the outside edges.
Important Consideration
Use margin-block-end when a specific element needs additional spacing after it. Use gap for uniform spacing between items.
Responsive Spacing
Block-end margins can be adjusted for different viewport sizes using relative units, media queries, and CSS functions.
Media Query Example
.content-section {
margin-block-end: 40px;
}
@media (max-width: 768px) {
.content-section {
margin-block-end: 20px;
}
}
Using rem Units
.content-section {
margin-block-end: 2rem;
}
Fluid Spacing with clamp()
.content-section {
margin-block-end: clamp(1rem, 3vw, 3rem);
}
Important Consideration
Responsive spacing should remain proportional to the surrounding content. Avoid unnecessarily large fixed margins on narrow screens.
margin-block-end Example
This example demonstrates how different block-end margin values change the space between two boxes in a horizontal writing mode.
HTML Example
<div class="margin-block-end-demo">
<div class="demo-section">
<h3>No Block-End 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 Block-End 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 Block-End 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-block-end-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 20px;
}
.margin-block-end-demo .demo-section {
padding: 15px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
.margin-block-end-demo h3 {
margin: 0 0 12px;
font-size: 1rem;
}
.margin-block-end-demo .margin-area {
padding: 12px;
background-color: #e2e8f0;
}
.margin-block-end-demo .demo-box {
padding: 12px;
border: 2px solid #2563eb;
background-color: #bfdbfe;
color: #1e3a8a;
text-align: center;
font-size: 0.875rem;
}
.margin-block-end-demo .no-margin {
margin-block-end: 0;
}
.margin-block-end-demo .medium-margin {
margin-block-end: 20px;
}
.margin-block-end-demo .large-margin {
margin-block-end: 40px;
}
.margin-block-end-demo .following-box {
margin: 0;
border-color: #64748b;
background-color: #f1f5f9;
color: #334155;
}
No Block-End Margin
20px Block-End Margin
40px Block-End Margin
As the block-end margin increases, the space between the first and second boxes increases. The margin is outside the first box's border.
Change the Margin to 30px
.margin-block-end-demo .medium-margin {
margin-block-end: 30px;
}
Use a Relative Unit
.margin-block-end-demo .large-margin {
margin-block-end: 2rem;
}
Remove the Margin
.margin-block-end-demo .large-margin {
margin-block-end: 0;
}
Common Mistakes
Assuming Block-End Always Means Bottom
In vertical writing modes, block-end corresponds to the left or right side.
Confusing margin-block-end with margin-inline-end
margin-block-end follows the block axis. margin-inline-end follows the inline axis.
Expecting Percentage Values to Use Container Height
Percentage margins are calculated from the containing block's inline size.
Expecting Auto to Always Create Space
Automatic margins depend on the formatting context and available space.
Forgetting Margin Collapsing
Adjacent vertical margins can collapse in normal block layout.
Using Negative Margins Without Testing
Negative values can cause overlap or overflow.
Overwriting the Value with a Shorthand
.box {
margin-block-end: 30px;
margin-block: 10px;
}
The final block-end margin is 10 pixels.
Mixing Physical and Logical Margins
Declarations such as margin-bottom and margin-block-end can affect the same side in horizontal writing modes.
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-block-end:
- The property controls the margin at the end of the block axis.
- It is a logical margin property.
- The initial value is
0. - The property is not inherited.
- The physical side depends on the writing mode.
- In
horizontal-tb, block-end is the bottom side. - In
vertical-rl, block-end is the left side. - In
vertical-lr, block-end is the right 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.
- Some adjoining vertical margins can collapse in normal block layout.
- Margins between flex and grid items do not collapse.
- The margin is outside the element's border.
- Margins do not extend the element's background.
- The property can be set using the
margin-blockshorthand. - The
marginshorthand can also affect the corresponding physical 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.
- Logical margins are useful for writing-mode-aware components.
Browser Support
The CSS margin-block-end property is widely supported across modern browsers, including Chrome, Edge, Firefox, Safari, and Opera.
The property is part of CSS Logical Properties and Values. Older browsers may require a physical margin fallback, such as margin-bottom for horizontal writing modes.
For current compatibility information, visit Can I Use? and search for margin-block-end.
Specifications
The margin-block-end property is defined in the CSS Logical Properties and Values Module Level 1 specification.
CSS Logical Properties and Values Module Level 1: Margin Properties
The specification defines how logical margins correspond to physical sides according to writing modes.
For additional documentation and examples, consult MDN Web Docs.
