CSS scroll-margin-block Property
The CSS scroll-margin-block property sets the scroll margins at the block-start and block-end edges of an element.
It is a logical shorthand property that adjusts the area used to align an element during scrolling. Unlike physical scroll margins, its edges follow the element's writing mode, making it useful for layouts that support horizontal and vertical text.
scroll-margin-block Syntax
The scroll-margin-block property accepts one or two CSS length values.
Basic Syntax
selector {
scroll-margin-block: 20px;
}
Available Values
.target {
scroll-margin-block: 20px;
}
.target {
scroll-margin-block: 20px 40px;
}
.target {
scroll-margin-block: 1rem;
}
.target {
scroll-margin-block: 2em 3em;
}
.target {
scroll-margin-block: -10px;
}
.target {
scroll-margin-block: calc(1rem + 10px);
}
CSS-Wide Keywords
.target {
scroll-margin-block: initial;
}
.target {
scroll-margin-block: inherit;
}
.target {
scroll-margin-block: unset;
}
.target {
scroll-margin-block: revert;
}
.target {
scroll-margin-block: revert-layer;
}
One value applies to both block edges. Two values set the block-start and block-end scroll margins separately.
The property does not accept percentages or the auto keyword.
Property Values
| Value | Description |
|---|---|
<length> |
Sets both block-start and block-end scroll margins to the same length. |
<length> <length> |
Sets the block-start scroll margin with the first value and the block-end scroll margin with the second. |
| Negative length | Contracts the scroll alignment area along the specified block edge. |
calc() |
Calculates a length using a valid CSS mathematical expression. |
initial |
Resets both block-axis scroll margins to 0. |
inherit |
Uses the computed values from the parent element. |
unset |
Resets both scroll margins to 0 because the longhand properties are not inherited. |
revert |
Reverts the property according to CSS cascade origin rules. |
revert-layer |
Reverts the property according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | 0 for both longhand properties |
| Inherited | No |
| Applies to | All elements |
| Computed value | Computed length values for the two longhand properties |
| Animation type | By computed value, as lengths |
| Percentages | Not accepted |
Valid Values
.target {
scroll-margin-block: 24px;
}
.target {
scroll-margin-block: 1rem 2rem;
}
.target {
scroll-margin-block: 0 80px;
}
.target {
scroll-margin-block: -5px 15px;
}
.target {
scroll-margin-block: calc(2rem + 5px);
}
Invalid Values
.target {
scroll-margin-block: auto;
}
.target {
scroll-margin-block: 10%;
}
.target {
scroll-margin-block: 10px 20px 30px;
}
.target {
scroll-margin-block: 10px, 20px;
}
.target {
scroll-margin-block: 1s;
}
Only one or two length values are permitted. Percentages, auto, comma-separated values, and time units are invalid.
How It Works
The scroll-margin-block property changes the scroll alignment area surrounding an element along its logical block axis.
During scrolling, the browser can use this expanded or contracted area to determine where the element should be positioned within a scroll container.
Default Scroll Margins
.target {
scroll-margin-block: 0;
}
No additional block-axis scroll margin is applied.
Adding Scroll Margins
.target {
scroll-margin-block: 30px;
}
The target's scroll alignment area extends 30 pixels beyond both block edges.
Different Block-Start and Block-End Margins
.target {
scroll-margin-block: 80px 20px;
}
The block-start edge receives 80 pixels of scroll margin, while the block-end edge receives 20 pixels.
Layout Space Remains Unchanged
Scroll margins do not add physical space between neighboring elements or change the dimensions of the target's layout box.
Scroll Alignment Depends on the Operation
The visible effect depends on the scrolling operation, requested alignment, and available scroll range. A target cannot always reach its preferred position near the beginning or end of a scroll container.
Important Distinction
The property adjusts the target's scroll alignment area. It does not create scrolling overflow, enable scroll snapping, or control scrolling animation speed.
One and Two Values
The shorthand accepts either one shared value or two separate values for the block-start and block-end edges.
One Value
.target {
scroll-margin-block: 40px;
}
Both block edges receive 40 pixels of scroll margin.
Two Values
.target {
scroll-margin-block: 60px 20px;
}
The block-start edge receives 60 pixels, and the block-end edge receives 20 pixels.
Zero on One Side
.target {
scroll-margin-block: 80px 0;
}
Only the block-start edge receives additional scroll margin.
Negative Values
.target {
scroll-margin-block: -10px 20px;
}
The block-start alignment area contracts by 10 pixels, while the block-end alignment area expands by 20 pixels.
Using Relative Units
.target {
scroll-margin-block: 2rem 1rem;
}
Relative units allow scroll offsets to respond to font sizing and related design settings.
Important Consideration
Unlike the four-sided scroll-margin shorthand, scroll-margin-block accepts no more than two values.
Longhand Properties
The scroll-margin-block shorthand sets two logical longhand properties.
| Property | Description |
|---|---|
scroll-margin-block-start |
Sets the scroll margin at the beginning of the block axis. |
scroll-margin-block-end |
Sets the scroll margin at the end of the block axis. |
Shorthand Declaration
.target {
scroll-margin-block: 80px 20px;
}
Equivalent Longhand Declarations
.target {
scroll-margin-block-start: 80px;
scroll-margin-block-end: 20px;
}
Overriding One Side
.target {
scroll-margin-block: 20px;
scroll-margin-block-start: 80px;
}
The block-start scroll margin becomes 80 pixels while the block-end scroll margin remains 20 pixels.
Declaration Order
.target {
scroll-margin-block-start: 80px;
scroll-margin-block: 20px;
}
The later shorthand sets both longhand values to 20 pixels.
Important Consideration
Use the shorthand when both block edges need related values. Use a longhand property when only one block edge needs adjustment.
Writing Modes and Logical Directions
The block axis is determined by the element's writing mode. In horizontal writing, blocks progress vertically. In vertical writing, blocks progress horizontally.
Horizontal Writing Mode
.target {
writing-mode: horizontal-tb;
scroll-margin-block: 40px 20px;
}
In horizontal-tb, block-start corresponds to the top edge and block-end corresponds to the bottom edge.
Vertical Right-to-Left Writing Mode
.target {
writing-mode: vertical-rl;
scroll-margin-block: 40px 20px;
}
In vertical-rl, block-start corresponds to the right edge and block-end corresponds to the left edge.
Vertical Left-to-Right Writing Mode
.target {
writing-mode: vertical-lr;
scroll-margin-block: 40px 20px;
}
In vertical-lr, block-start corresponds to the left edge and block-end corresponds to the right edge.
Writing Mode Reference
| Writing Mode | Block-Start | Block-End |
|---|---|---|
horizontal-tb |
Top | Bottom |
vertical-rl |
Right | Left |
vertical-lr |
Left | Right |
Text Direction Versus Writing Mode
The direction property affects inline progression but does not by itself reverse the block-start and block-end edges in horizontal writing.
Important Consideration
Logical properties are useful for internationalized layouts because the same CSS declaration can adapt to different writing modes without manually switching between physical sides.
Anchor Navigation
The scroll-margin-block property can provide additional space around anchor targets when the browser scrolls them into view.
HTML Example
<nav aria-label="Page sections">
<a href="#introduction">Introduction</a>
<a href="#examples">Examples</a>
<a href="#summary">Summary</a>
</nav>
<section id="introduction">
<h2>Introduction</h2>
</section>
<section id="examples">
<h2>Examples</h2>
</section>
<section id="summary">
<h2>Summary</h2>
</section>
CSS Example
section[id] {
scroll-margin-block: 80px 20px;
}
In horizontal writing, the declaration provides 80 pixels of top scroll margin and 20 pixels of bottom scroll margin.
Working With Sticky Headers
section[id] {
scroll-margin-block-start: 90px;
}
In horizontal writing, this can keep anchor targets from being positioned directly behind a sticky header.
Combining With Smooth Scrolling
html {
scroll-behavior: smooth;
}
section[id] {
scroll-margin-block-start: 90px;
}
@media (prefers-reduced-motion: reduce) {
html {
scroll-behavior: auto;
}
}
The scroll margin adjusts target alignment while scroll-behavior requests smooth movement.
Important Consideration
Anchor links remain functional without scroll margins. The property improves target positioning rather than creating navigation behavior.
Scroll Snapping
The scroll-margin-block property can adjust the snap area of an element along the block axis.
Vertical Scroll Snap Example
.scroll-panel {
height: 300px;
overflow-y: auto;
scroll-snap-type: y mandatory;
}
.scroll-panel .item {
scroll-snap-align: start;
scroll-margin-block: 20px;
}
In horizontal writing, the scroll snap area extends 20 pixels above and below each item.
Different Block-Start and Block-End Margins
.scroll-panel .item {
scroll-margin-block: 40px 10px;
}
The expanded snap area is larger at block-start than at block-end.
Combining With scroll-padding-block
.scroll-panel {
scroll-padding-block: 20px;
}
.scroll-panel .item {
scroll-margin-block: 10px;
}
Scroll padding defines offsets within the container's snapport, while scroll margin adjusts the alignment area around each item.
Important Consideration
The property does not activate scroll snapping. The container must have an appropriate scroll-snap-type value, and the target must define snap alignment.
Physical and Logical Scroll Margins
Physical scroll margin properties refer to fixed sides of an element. Logical scroll margin properties follow the writing mode.
| Property | Purpose |
|---|---|
scroll-margin |
Sets the physical top, right, bottom, and left scroll margins. |
scroll-margin-block |
Sets the logical block-start and block-end scroll margins. |
scroll-margin-inline |
Sets the logical inline-start and inline-end scroll margins. |
Physical Scroll Margins
.target {
scroll-margin: 40px 20px;
}
The top and bottom receive 40 pixels, while the right and left receive 20 pixels, regardless of writing mode.
Logical Scroll Margins
.target {
scroll-margin-block: 40px;
scroll-margin-inline: 20px;
}
The margins follow the block and inline axes of the element.
Physical Equivalent in Horizontal Writing
.target {
writing-mode: horizontal-tb;
scroll-margin-block: 40px 20px;
}
In this writing mode, the declaration corresponds to a top scroll margin of 40 pixels and a bottom scroll margin of 20 pixels.
Important Consideration
Physical and logical declarations can affect the same underlying edge. When both are used, normal CSS cascade rules determine which declaration takes precedence.
scroll-margin-block Versus scroll-padding-block
Both properties adjust scrolling alignment along the block axis, but they apply to different elements.
| Property | Applied To | Purpose |
|---|---|---|
scroll-margin-block |
Scroll target | Expands or contracts the target's alignment area along the block axis. |
scroll-padding-block |
Scroll container | Defines offsets within the container's optimal viewing region along the block axis. |
Target-Side Spacing
.target {
scroll-margin-block-start: 60px;
}
The target receives additional scroll alignment space at its block-start edge.
Container-Side Spacing
.scroll-container {
scroll-padding-block-start: 60px;
}
The container reserves an inset at its block-start edge when determining optimal scroll alignment.
Combining Both Properties
.scroll-container {
scroll-padding-block-start: 40px;
}
.scroll-container .target {
scroll-margin-block-start: 20px;
}
Both offsets can contribute to the target's final alignment position.
When to Use Each Property
Use scroll-padding-block for consistent offsets within a scroll container. Use scroll-margin-block when individual targets need their own block-axis spacing.
scroll-margin-block Example
The following example demonstrates how logical block-start scroll margins affect the alignment of sections inside a scrollable panel.
HTML Example
<div class="scroll-block-demo">
<nav aria-label="Scroll block example">
<a href="#block-demo-one">Section One</a>
<a href="#block-demo-two">Section Two</a>
<a href="#block-demo-three">Section Three</a>
</nav>
<div class="scroll-panel">
<section id="block-demo-one">
<h3>Section One</h3>
<p>The first scroll target.</p>
</section>
<section id="block-demo-two">
<h3>Section Two</h3>
<p>The second scroll target.</p>
</section>
<section id="block-demo-three">
<h3>Section Three</h3>
<p>The third scroll target.</p>
</section>
</div>
</div>
CSS Example
.scroll-block-demo {
max-width: 600px;
}
.scroll-block-demo nav {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 15px;
}
.scroll-block-demo nav a {
padding: 8px 12px;
background-color: #2563eb;
color: #ffffff;
text-decoration: none;
}
.scroll-block-demo .scroll-panel {
height: 220px;
overflow-y: auto;
scroll-behavior: smooth;
border: 1px solid #cbd5e1;
}
.scroll-block-demo .scroll-panel section {
min-height: 220px;
padding: 20px;
box-sizing: border-box;
scroll-margin-block: 40px 0;
}
.scroll-block-demo .scroll-panel section:nth-child(odd) {
background-color: #eff6ff;
}
.scroll-block-demo .scroll-panel section:nth-child(even) {
background-color: #dbeafe;
}
@media (prefers-reduced-motion: reduce) {
.scroll-block-demo .scroll-panel {
scroll-behavior: auto;
}
}
Section One
The first scroll target.
Use the links above to navigate between sections.
Section Two
The second scroll target.
Notice the additional space above this section.
Section Three
The third scroll target.
The scroll margin changes alignment without changing layout spacing.
Click Section Two or Section Three. In the default horizontal writing mode, the 40-pixel block-start scroll margin provides additional space above the target when the browser aligns it within the panel.
Removing the Scroll Margin
.scroll-block-demo .scroll-panel section {
scroll-margin-block: 0;
}
The additional block-axis scroll alignment offsets are removed.
Increasing the Block-Start Margin
.scroll-block-demo .scroll-panel section {
scroll-margin-block: 80px 0;
}
The block-start alignment area extends 80 pixels beyond the target.
Using Separate Longhand Properties
.scroll-block-demo .scroll-panel section {
scroll-margin-block-start: 40px;
scroll-margin-block-end: 0;
}
This produces the same block-axis scroll margins as the original shorthand declaration.
Browser Compatibility Note
The final scroll position depends on the available scroll range and the browser's alignment behavior. Targets near the end of a scroll container may not reach their preferred alignment position.
Accessibility Considerations
Logical scroll margins can improve navigation by keeping important targets visible across different writing modes and screen sizes.
Keep Targets Visible
Provide enough scroll margin to prevent headings from being obscured by sticky headers or other interface elements.
Support Different Writing Modes
Logical properties allow scroll alignment offsets to follow the block direction of the content.
Preserve Semantic Navigation
Use standard anchor links and meaningful target IDs so navigation remains functional without JavaScript.
Maintain Keyboard Accessibility
Ensure links remain keyboard accessible and that focus indicators are visible after navigation.
Consider Focus Management
Adjusting scroll alignment does not automatically move keyboard focus. Scripted interfaces may need additional focus management.
Avoid Excessive Offsets
Very large scroll margins can leave unnecessary space between the viewport edge and the intended target.
Respect Reduced Motion Preferences
Scroll margins do not animate scrolling, but they are often combined with smooth scrolling. Use prefers-reduced-motion when enabling animated navigation.
Test Responsive Layouts
Check that scroll offsets remain appropriate when viewport dimensions, zoom levels, or header sizes change.
Important Consideration
The property affects scroll alignment only. It does not change reading order, semantic structure, or keyboard navigation behavior.
Common Mistakes
Using Percentage Values
.target {
scroll-margin-block: 10%;
}
Percentages are invalid. Use length values such as px, rem, or em.
Using auto
.target {
scroll-margin-block: auto;
}
The property does not accept auto.
Providing Three Values
.target {
scroll-margin-block: 10px 20px 30px;
}
The shorthand accepts only one or two values.
Using Commas
.target {
scroll-margin-block: 10px, 20px;
}
Separate the values with spaces, not commas.
Confusing Block and Inline Directions
Block-start and block-end are determined by writing mode. They do not always correspond to top and bottom.
Expecting Layout Spacing
Scroll margins do not add normal layout space between elements.
Applying the Property to the Wrong Element
Use scroll-margin-block on scroll targets. Use scroll-padding-block on scroll containers when a consistent container-side offset is needed.
Expecting Scroll Snapping Automatically
The property adjusts snap areas but does not activate scroll snapping by itself.
Ignoring Declaration Order
A later shorthand can override previously declared block-start and block-end scroll margins.
Using Excessively Large Values
Large offsets may cause unexpected target positions or may not be fully achievable within the available scroll range.
Important Considerations
Keep the following points in mind when using scroll-margin-block:
- It is a logical shorthand property.
- It sets
scroll-margin-block-startandscroll-margin-block-end. - The initial value of each longhand is
0. - The property is not inherited.
- It applies to all elements.
- It accepts one or two length values.
- One value applies to both block edges.
- Two values set block-start and block-end separately.
- Negative length values are permitted.
- Percentage values are invalid.
- The
autokeyword is invalid. - Values are separated by spaces.
- The block axis depends on the writing mode.
- In
horizontal-tb, block-start is top and block-end is bottom. - In
vertical-rl, block-start is right and block-end is left. - In
vertical-lr, block-start is left and block-end is right. - The property adjusts the target's scroll alignment area.
- It does not change normal layout spacing.
- It can affect anchor navigation and programmatic scrolling alignment.
- It can affect scroll snapping along the block axis.
- It does not enable scroll snapping by itself.
- It does not create a scroll container.
- It does not control scrolling animation speed.
- The
scroll-behaviorproperty controls smooth scrolling. - The
scroll-padding-blockproperty defines container-side offsets. - Physical and logical scroll margins can affect the same underlying edges.
- Normal CSS cascade rules determine which overlapping declaration takes precedence.
- The available scroll range can limit the final target position.
- Offsets should accommodate responsive header sizes.
- Logical properties are useful for multilingual and vertical-writing layouts.
- Test scroll alignment across browsers, viewport sizes, and writing modes.
Browser Support
The CSS scroll-margin-block property is Baseline Widely available and is supported in current versions of major browsers.
Support for logical scroll margin properties may be more limited in older browsers than support for physical scroll margin properties. Scrolling alignment can also vary according to the scrolling operation and available scroll range.
For current compatibility information, visit Can I Use? and search for scroll-margin-block.
When the property is unsupported, scrolling and anchor navigation generally remain functional, but the additional logical scroll margin may not be applied.
Specifications
The scroll-margin-block property is defined in CSS Scroll Snap Module Level 1.
CSS Scroll Snap Module Level 1
The specification defines logical scroll margin properties and how scroll margins affect the scroll snap area surrounding a target.
For additional documentation and examples, consult MDN Web Docs.
