CSS margin-block Property
The CSS margin-block property is a logical shorthand that sets the margins at the start and end of an element's block axis.
Unlike physical margin properties, which always refer to the top, right, bottom, or left, margin-block follows the element's writing mode. In horizontal writing modes, it controls the top and bottom margins. In vertical writing modes, it controls the left and right margins.
margin-block Syntax
The margin-block shorthand accepts one or two values. Each value can be a length, percentage, or the auto keyword.
Basic Syntax
selector {
margin-block: 20px;
}
One Value
div {
margin-block: 20px;
}
Applies 20 pixels to both the block-start and block-end margins.
Two Values
div {
margin-block: 10px 30px;
}
Applies 10 pixels to the block-start margin and 30 pixels to the block-end margin.
Mixed Values
div {
margin-block: 1rem 24px;
}
Automatic Margins
div {
margin-block: auto;
}
CSS-Wide Keywords
div {
margin-block: initial;
}
div {
margin-block: inherit;
}
div {
margin-block: unset;
}
div {
margin-block: revert;
}
div {
margin-block: revert-layer;
}
The initial value of each underlying logical margin property is 0.
Property Values
The margin-block property supports the following value types.
| Value | Description |
|---|---|
<length> |
Specifies a margin using a CSS length such as px, em, rem, or vw. |
<percentage> |
Specifies a percentage based on the containing block's inline size. |
auto |
Allows the browser to calculate the margin according to the layout context. |
initial |
Resets both logical margins to 0. |
inherit |
Inherits the computed logical margin values from the parent. |
unset |
Resets both margins to their initial values 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 both underlying margins |
| Inherited | No |
| Applies to | Same as the corresponding physical margin properties |
| Computed value | Each underlying margin as a computed length, percentage, or auto |
| Animation type | Each underlying margin animates as a length, percentage, or calculated combination |
| Negative values | Allowed for lengths and percentages |
| Shorthand for | margin-block-start and margin-block-end |
The property sets two logical margins rather than all four physical sides. The physical sides affected depend on the element's writing mode.
How It Works
CSS logical properties describe layout directions using the block and inline axes instead of fixed physical directions.
The block axis is the direction in which block-level content progresses. In a typical English document, blocks progress vertically from top to bottom.
HTML Example
<div class="content-box">
CSS Logical Margins
</div>
CSS Example
.content-box {
margin-block: 20px;
padding: 15px;
border: 2px solid #2563eb;
background-color: #dbeafe;
}
In a horizontal writing mode, the element receives 20 pixels of margin above and below its border.
Equivalent Longhand Properties
.content-box {
margin-block-start: 20px;
margin-block-end: 20px;
}
Important Consideration
The property changes which physical sides receive margins when the writing mode changes. It does not change the element's writing mode itself.
One Value
When one value is supplied, it applies to both block-start and block-end.
Example
.box {
margin-block: 20px;
}
This is equivalent to:
.box {
margin-block-start: 20px;
margin-block-end: 20px;
}
Using Relative Units
.box {
margin-block: 1.5rem;
}
Remove Block-Axis Margins
.box {
margin-block: 0;
}
Common Use
.content-section {
margin-block: 2rem;
}
This provides consistent spacing before and after a content section in the block direction.
Two Values
When two values are supplied, the first sets the block-start margin and the second sets the block-end margin.
Example
.box {
margin-block: 10px 30px;
}
This is equivalent to:
.box {
margin-block-start: 10px;
margin-block-end: 30px;
}
Heading Spacing
h2 {
margin-block: 1rem 0.75rem;
}
Paragraph Spacing
p {
margin-block: 0 1rem;
}
Section Spacing
section {
margin-block: 2rem 3rem;
}
Important Consideration
The two values follow the block axis, not a fixed top-to-bottom order. In vertical writing modes, they can correspond to right and left margins.
Writing Modes
The physical sides controlled by margin-block depend on the writing-mode property.
| Writing Mode | Block Start | Block End |
|---|---|---|
horizontal-tb |
Top | Bottom |
vertical-rl |
Right | Left |
vertical-lr |
Left | Right |
Horizontal Writing Mode
.horizontal-box {
writing-mode: horizontal-tb;
margin-block: 10px 30px;
}
The top margin is 10 pixels and the bottom margin is 30 pixels.
Vertical Right-to-Left Writing Mode
.vertical-rl-box {
writing-mode: vertical-rl;
margin-block: 10px 30px;
}
The right margin is 10 pixels and the left margin is 30 pixels.
Vertical Left-to-Right Writing Mode
.vertical-lr-box {
writing-mode: vertical-lr;
margin-block: 10px 30px;
}
The left margin is 10 pixels and the right margin is 30 pixels.
Important Consideration
The direction property affects inline progression but does not reverse block-start and block-end in a horizontal writing mode.
Logical vs. Physical Margins
Physical margins refer to fixed sides of an element. Logical margins refer to sides based on the writing mode.
Physical Margin Example
.box {
margin-top: 20px;
margin-bottom: 30px;
}
Logical Margin Example
.box {
margin-block: 20px 30px;
}
These declarations affect the same physical sides when the writing mode is horizontal-tb.
Comparison
| Feature | Physical Margins | Logical Margins |
|---|---|---|
| Direction | Fixed physical sides | Writing-mode dependent |
| Examples | margin-top, margin-bottom |
margin-block-start, margin-block-end |
| Vertical writing modes | Remain on their physical sides | Map to the appropriate block-axis sides |
| Common use | Fixed-direction layouts | Writing-mode-aware layouts |
Important Consideration
Logical margins are useful for reusable components that may appear in different writing modes without requiring separate physical margin declarations.
Auto Margins
The auto keyword allows the browser to calculate block-axis margins according to the formatting context.
Both Margins Automatic
.box {
margin-block: auto;
}
Only the Block-Start Margin Automatic
.box {
margin-block: auto 20px;
}
Flexbox Example
.flex-container {
display: flex;
flex-direction: column;
min-height: 300px;
}
.flex-item:last-child {
margin-block-start: auto;
}
In a typical horizontal writing mode, the last flex item is pushed toward the bottom when positive free space is available.
Important Consideration
Automatic block-axis margins do not generally provide vertical centering in ordinary block layout. Their behavior depends on the layout context and available space.
Negative Margins
The margin-block property accepts negative lengths and percentages. Negative margins can reduce spacing or cause elements to overlap.
Negative Block-Start Margin
.box {
margin-block: -20px 10px;
}
Negative Block-End Margin
.box {
margin-block: 10px -15px;
}
Both Margins Negative
.box {
margin-block: -10px;
}
Overlapping Content Panel
.content-panel {
position: relative;
margin-block-start: -30px;
padding: 20px;
background-color: #fff;
}
Important Consideration
Negative margins affect layout calculations and can produce unintended overlap or overflow. Their physical direction changes with the writing mode.
Percentage Margins
Percentage values for margin-block are calculated relative to the containing block's inline size.
Equal Percentage Margins
.box {
margin-block: 5%;
}
Different Percentage Margins
.box {
margin-block: 5% 10%;
}
Mixed Percentage and Length
.box {
margin-block: 5% 2rem;
}
Important Consideration
In horizontal writing modes, percentage block margins are based on the containing block's width, not its height. This behavior can be surprising when creating vertical spacing.
Margin Collapsing
In normal block layout, adjoining block-axis margins may collapse into a single margin instead of adding together.
Adjacent Elements
.first-box {
margin-block-end: 30px;
}
.second-box {
margin-block-start: 20px;
}
In a horizontal writing mode, if these margins collapse, the resulting space is 30 pixels rather than 50 pixels.
Equal Margins
.content-box {
margin-block: 20px;
}
Two adjoining vertical margins of 20 pixels can collapse into a single 20-pixel 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 or grid items do not collapse.
Important Consideration
Margin collapsing follows the applicable formatting rules. It is not guaranteed for every pair of adjoining margins, and it does not occur between flex or grid items.
margin-block Longhands
The margin-block shorthand sets two individual logical margin properties.
margin-block-start
.box {
margin-block-start: 20px;
}
Sets the margin at the beginning of the block axis.
margin-block-end
.box {
margin-block-end: 30px;
}
Sets the margin at the end of the block axis.
Equivalent Shorthand
.box {
margin-block: 20px 30px;
}
Important Consideration
Use the individual properties when only one block-axis margin needs to change. Use the shorthand when setting both margins together.
margin-inline Comparison
The margin-inline property controls spacing along the inline axis, while margin-block controls spacing along the block axis.
Block-Axis Margins
.box {
margin-block: 20px;
}
Inline-Axis Margins
.box {
margin-inline: 20px;
}
Using Both
.box {
margin-block: 20px;
margin-inline: 15px;
}
In a horizontal writing mode, this is equivalent to 20-pixel top and bottom margins and 15-pixel left and right margins.
Centering with margin-inline
.container {
width: 80%;
margin-inline: auto;
}
In an appropriate block layout, automatic inline margins center the container along the inline axis.
Important Consideration
Use logical block and inline properties together to create layouts that adapt to writing modes and text directions.
Flexbox and Grid
Logical margins work with Flexbox and CSS Grid. They can provide spacing around individual items or help distribute available space.
Flexbox with Block Margins
.flex-container {
display: flex;
flex-direction: column;
}
.flex-item {
margin-block: 10px;
}
Grid with Block Margins
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
.grid-item {
margin-block: 15px;
}
Using gap Instead
.grid-container {
display: grid;
gap: 20px;
}
The gap property creates spacing between items without adding margins around their outer edges.
Important Consideration
Use gap for consistent spacing between flex or grid items. Use margin-block when individual items need additional block-axis spacing.
Shorthand Reset Behavior
The margin-block shorthand sets both block-axis margins. A later shorthand declaration can overwrite earlier longhand declarations.
Longhand Followed by Shorthand
.box {
margin-block-start: 30px;
margin-block: 10px;
}
Both block-axis margins become 10 pixels.
Shorthand Followed by Longhand
.box {
margin-block: 10px;
margin-block-end: 30px;
}
The block-start margin remains 10 pixels, while the block-end margin becomes 30 pixels.
Physical and Logical Margins
.box {
margin-top: 20px;
margin-block-start: 40px;
}
In a horizontal writing mode, both declarations affect the top margin. At equal cascade priority, the later declaration wins.
Important Consideration
Physical and logical margin properties can target the same side. The CSS cascade determines the winning declaration when they conflict.
Responsive Spacing
The margin-block property is useful for responsive vertical spacing in typical horizontal writing modes.
Media Query Example
.content-section {
margin-block: 40px;
}
@media (max-width: 768px) {
.content-section {
margin-block: 20px;
}
}
Using rem Units
.content-section {
margin-block: 2rem;
}
Fluid Spacing with clamp()
.content-section {
margin-block: clamp(1rem, 3vw, 3rem);
}
Different Start and End Spacing
.content-section {
margin-block: clamp(1rem, 2vw, 2rem) clamp(2rem, 4vw, 4rem);
}
Important Consideration
Responsive block-axis margins can help maintain balanced spacing without relying on fixed top and bottom declarations.
margin-block Example
This example compares equal and different block-axis margins. The colored areas make the spacing around each box easier to see.
HTML Example
<div class="margin-block-demo">
<div class="demo-section">
<h3>One Value: 20px</h3>
<div class="margin-area">
<div class="demo-box one-value">Equal Block Margins</div>
</div>
</div>
<div class="demo-section">
<h3>Two Values: 10px 30px</h3>
<div class="margin-area">
<div class="demo-box two-values">Different Block Margins</div>
</div>
</div>
<div class="demo-section">
<h3>No Block Margins</h3>
<div class="margin-area">
<div class="demo-box no-margin">Zero Block Margins</div>
</div>
</div>
</div>
CSS Example
.margin-block-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 20px;
}
.margin-block-demo .demo-section {
padding: 15px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
.margin-block-demo h3 {
margin: 0 0 12px;
font-size: 1rem;
}
.margin-block-demo .margin-area {
display: flow-root;
min-height: 110px;
background-color: #e2e8f0;
}
.margin-block-demo .demo-box {
padding: 12px;
border: 2px solid #2563eb;
background-color: #bfdbfe;
color: #1e3a8a;
text-align: center;
font-size: 0.875rem;
}
.margin-block-demo .one-value {
margin-block: 20px;
}
.margin-block-demo .two-values {
margin-block: 10px 30px;
}
.margin-block-demo .no-margin {
margin-block: 0;
}
One Value: 20px
Two Values: 10px 30px
No Block Margins
The first box has equal block-start and block-end margins. The second has a smaller block-start margin and a larger block-end margin. The third has no block-axis margins.
Increase Both Margins
.margin-block-demo .one-value {
margin-block: 30px;
}
Change Only the Block-End Margin
.margin-block-demo .two-values {
margin-block: 10px 40px;
}
Use Relative Units
.margin-block-demo .one-value {
margin-block: 1.5rem;
}
These changes demonstrate how logical margins provide spacing along the block axis without requiring fixed top or bottom declarations.
Common Mistakes
Assuming margin-block Always Means Top and Bottom
In vertical writing modes, block-axis margins correspond to the left and right sides.
Using More Than Two Values
The margin-block shorthand accepts only one or two values. Use margin when setting all four physical margins.
Confusing Block and Inline Axes
margin-block controls block-axis spacing. margin-inline controls inline-axis spacing.
Expecting Auto to Always Center Vertically
Automatic block-axis margins depend on the formatting context and available space.
Forgetting Margin Collapsing
Adjoining block-axis margins can collapse in normal block layout.
Assuming Percentages Use the Block Size
Percentage margins are based on the containing block's inline size.
Overwriting Longhand Values
.box {
margin-block-start: 30px;
margin-block: 10px;
}
The shorthand resets both block-axis margins to 10 pixels.
Mixing Physical and Logical Margins Without Checking the Cascade
Physical and logical declarations may affect the same side, depending on the writing mode.
Overusing Negative Margins
Negative values can create unintended overlap and overflow.
Using Margins Instead of gap for Every Layout
For consistent spacing between flex or grid items, gap may be simpler and easier to maintain.
Important Considerations
Keep the following points in mind when using margin-block:
- The property is a logical shorthand for two block-axis margins.
- It sets
margin-block-startandmargin-block-end. - The initial value of each underlying margin is
0. - The property is not inherited.
- It accepts one or two values.
- One value applies to both block-axis sides.
- Two values represent block-start and block-end.
- The physical sides depend on the writing mode.
- In
horizontal-tb, the block axis runs from top to bottom. - In
vertical-rl, the block axis runs from right to left. - In
vertical-lr, the block axis runs from left to right. - The property supports CSS length values.
- 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 block-axis margins may collapse in normal block layout.
- Margins between flex or grid items do not collapse.
- Logical properties adapt to different writing modes.
- Physical margins remain tied to fixed physical sides.
- The
margin-inlineproperty controls inline-axis spacing. - The
marginshorthand controls all four physical margins. - The
margin-blockshorthand resets both underlying logical margins. - Physical and logical margin declarations can affect the same side.
- The CSS cascade determines which conflicting declaration wins.
- The property works with Flexbox and CSS Grid.
- The
gapproperty is useful for spacing between flex or grid items. - Relative units can provide scalable spacing.
- Media queries can adjust margins for smaller screens.
- Use logical margins for reusable, writing-mode-aware layouts.
Browser Support
The CSS margin-block property is widely supported across modern browsers, including Chrome, Edge, Firefox, Safari, and Opera.
The shorthand is part of CSS logical properties and provides writing-mode-aware spacing. Older browsers may require physical margin declarations as a fallback.
For current compatibility information, visit Can I Use? and search for margin-block.
Specifications
The margin-block 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 logical margin properties and their relationship to physical margins and writing modes.
For additional documentation and examples, consult MDN Web Docs.
