CSS margin-block-start Property
The CSS margin-block-start property sets the outer margin at the beginning of an element's block axis, with the physical side determined by its writing mode.
In a typical horizontal writing mode, margin-block-start corresponds to margin-top. In vertical writing modes, it corresponds to the left or right margin. This logical property helps layouts adapt to different writing directions without changing individual physical margin declarations.
margin-block-start Syntax
The margin-block-start property accepts a CSS length, percentage, or the auto keyword. CSS-wide keywords are also supported.
Basic Syntax
selector {
margin-block-start: 20px;
}
Length Value
div {
margin-block-start: 2rem;
}
Percentage Value
div {
margin-block-start: 5%;
}
Auto Value
div {
margin-block-start: auto;
}
Negative Value
div {
margin-block-start: -15px;
}
CSS-Wide Keywords
div {
margin-block-start: initial;
}
div {
margin-block-start: inherit;
}
div {
margin-block-start: unset;
}
div {
margin-block-start: revert;
}
div {
margin-block-start: revert-layer;
}
The initial value of margin-block-start is 0.
Property Values
| Value | Description |
|---|---|
<length> |
Sets the margin using a 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 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 | 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-start property adds space outside the element's border at the beginning 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-start: 20px;
padding: 15px;
border: 2px solid #2563eb;
background-color: #dbeafe;
}
In a horizontal writing mode, each box receives a 20-pixel top margin, creating space before it in the block direction.
Equivalent Physical Property
.content-box {
margin-top: 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-start.
| Writing Mode | Block-Start Side | Physical Equivalent |
|---|---|---|
horizontal-tb |
Top | margin-top |
vertical-rl |
Right | margin-right |
vertical-lr |
Left | margin-left |
Horizontal Writing Mode
.horizontal-box {
writing-mode: horizontal-tb;
margin-block-start: 20px;
}
Vertical Right-to-Left Writing Mode
.vertical-rl-box {
writing-mode: vertical-rl;
margin-block-start: 20px;
}
Vertical Left-to-Right Writing Mode
.vertical-lr-box {
writing-mode: vertical-lr;
margin-block-start: 20px;
}
Important Consideration
The direction property affects inline text progression but does not reverse the block-start 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-top: 20px;
}
Logical Margin
.box {
margin-block-start: 20px;
}
Both declarations affect the top margin in a horizontal writing mode.
Comparison
| Feature | margin-top | margin-block-start |
|---|---|---|
| Physical side | Always top | Depends on writing mode |
| Horizontal writing mode | Top | Top |
| Vertical writing modes | Top | Right or left |
| 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 absolute and relative CSS length units.
Pixels
.box {
margin-block-start: 24px;
}
Root Font Size
.box {
margin-block-start: 2rem;
}
Element Font Size
.box {
margin-block-start: 1.5em;
}
Viewport Units
.box {
margin-block-start: 3vw;
}
Calculated Value
.box {
margin-block-start: 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, normally its width in a horizontal writing mode.
Percentage Example
.box {
margin-block-start: 5%;
}
Percentage with a Fixed Container Width
.container {
width: 600px;
}
.container .box {
margin-block-start: 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-start 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-start margin according to the layout context.
Basic Example
.box {
margin-block-start: auto;
}
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 item can absorb available vertical space before itself, pushing it toward the bottom of the container.
Important Consideration
Automatic block-start 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 an element closer to preceding content or cause overlap.
Negative Length
.box {
margin-block-start: -20px;
}
Negative Percentage
.box {
margin-block-start: -5%;
}
Overlapping Sections
.featured-section {
margin-block-start: -30px;
position: relative;
}
.featured-section .content {
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-start: 30px;
margin-block: 10px;
}
The final block-start margin is 10 pixels because the later shorthand resets both block-axis margins.
Flexbox and Grid
The margin-block-start property works with Flexbox and CSS Grid. It can add spacing before individual items without affecting every item in the layout.
Flexbox Example
.flex-container {
display: flex;
flex-direction: column;
}
.flex-item {
margin-block-start: 15px;
}
Grid Example
.grid-container {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
.grid-item {
margin-block-start: 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-start when a specific element needs additional spacing before it. Use gap for uniform spacing between items.
Responsive Spacing
Block-start margins can be adjusted for different viewport sizes using relative units, media queries, and CSS functions.
Media Query Example
.content-section {
margin-block-start: 40px;
}
@media (max-width: 768px) {
.content-section {
margin-block-start: 20px;
}
}
Using rem Units
.content-section {
margin-block-start: 2rem;
}
Fluid Spacing with clamp()
.content-section {
margin-block-start: 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-start Example
This example demonstrates how different block-start margin values change the space between two boxes in a horizontal writing mode.
HTML Example
<div class="margin-block-start-demo">
<div class="demo-section">
<h3>No Block-Start Margin</h3>
<div class="margin-area">
<div class="demo-box first-box">First Box</div>
<div class="demo-box no-margin">Second Box</div>
</div>
</div>
<div class="demo-section">
<h3>20px Block-Start Margin</h3>
<div class="margin-area">
<div class="demo-box first-box">First Box</div>
<div class="demo-box medium-margin">Second Box</div>
</div>
</div>
<div class="demo-section">
<h3>40px Block-Start Margin</h3>
<div class="margin-area">
<div class="demo-box first-box">First Box</div>
<div class="demo-box large-margin">Second Box</div>
</div>
</div>
</div>
CSS Example
.margin-block-start-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 20px;
}
.margin-block-start-demo .demo-section {
padding: 15px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
.margin-block-start-demo h3 {
margin: 0 0 12px;
font-size: 1rem;
}
.margin-block-start-demo .margin-area {
padding: 12px;
background-color: #e2e8f0;
}
.margin-block-start-demo .demo-box {
padding: 12px;
border: 2px solid #2563eb;
background-color: #bfdbfe;
color: #1e3a8a;
text-align: center;
font-size: 0.875rem;
}
.margin-block-start-demo .first-box {
margin: 0;
border-color: #64748b;
background-color: #f1f5f9;
color: #334155;
}
.margin-block-start-demo .no-margin {
margin-block-start: 0;
}
.margin-block-start-demo .medium-margin {
margin-block-start: 20px;
}
.margin-block-start-demo .large-margin {
margin-block-start: 40px;
}
No Block-Start Margin
20px Block-Start Margin
40px Block-Start Margin
As the block-start margin increases, the space before the second box increases. The margin is outside the second box's border.
Change the Margin to 30px
.margin-block-start-demo .medium-margin {
margin-block-start: 30px;
}
Use a Relative Unit
.margin-block-start-demo .large-margin {
margin-block-start: 2rem;
}
Remove the Margin
.margin-block-start-demo .large-margin {
margin-block-start: 0;
}
Common Mistakes
Assuming Block-Start Always Means Top
In vertical writing modes, block-start corresponds to the right or left side.
Confusing margin-block-start with margin-inline-start
margin-block-start follows the block axis. margin-inline-start 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-start: 30px;
margin-block: 10px;
}
The final block-start margin is 10 pixels.
Mixing Physical and Logical Margins
Declarations such as margin-top and margin-block-start 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-start:
- The property controls the margin at the beginning 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-start is the top side. - In
vertical-rl, block-start is the right side. - In
vertical-lr, block-start is the left 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-start 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-top for horizontal writing modes.
For current compatibility information, visit Can I Use? and search for margin-block-start.
Specifications
The margin-block-start 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.
