CSS scroll-margin-inline Property
The CSS scroll-margin-inline property is a shorthand that sets the scroll margins at an element's logical inline-start and inline-end edges.
It adjusts the area used to align an element during scrolling without changing its layout spacing. Because the inline axis follows the writing mode and text direction, the property is useful for horizontal scrolling, scroll snapping, and layouts that support multiple languages.
scroll-margin-inline Syntax
The scroll-margin-inline shorthand accepts one or two CSS length values.
Basic Syntax
selector {
scroll-margin-inline: 20px;
}
One-Value Syntax
.target {
scroll-margin-inline: 30px;
}
Both inline-start and inline-end receive a 30-pixel scroll margin.
Two-Value Syntax
.target {
scroll-margin-inline: 20px 40px;
}
The first value sets inline-start to 20 pixels, and the second sets inline-end to 40 pixels.
Additional Length Examples
.target {
scroll-margin-inline: 2rem;
}
.target {
scroll-margin-inline: 1em 3em;
}
.target {
scroll-margin-inline: 0 50px;
}
.target {
scroll-margin-inline: -10px 20px;
}
.target {
scroll-margin-inline: calc(1rem + 10px);
}
CSS-Wide Keywords
.target {
scroll-margin-inline: initial;
}
.target {
scroll-margin-inline: inherit;
}
.target {
scroll-margin-inline: unset;
}
.target {
scroll-margin-inline: revert;
}
.target {
scroll-margin-inline: revert-layer;
}
Each length may be positive, zero, or negative. Percentages and auto are not valid values.
Property Values
| Value | Description |
|---|---|
One <length> |
Sets the same scroll margin at both logical inline edges. |
Two <length> values |
Sets inline-start first and inline-end second. |
0 |
Applies no additional inline scroll margin. This is the initial value of both longhands. |
| Positive length | Expands the scroll alignment area beyond the corresponding inline edge. |
| Negative length | Contracts the scroll alignment area at the corresponding inline edge. |
calc() |
Calculates a valid length using a CSS mathematical expression. |
initial |
Resets both longhand properties to 0. |
inherit |
Inherits both computed longhand values from the parent element. |
unset |
Resets both longhands to 0 because they are not inherited. |
revert |
Reverts both longhands according to CSS cascade origin rules. |
revert-layer |
Reverts both longhands according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Property type | Shorthand |
| Longhand properties | scroll-margin-inline-start, scroll-margin-inline-end |
| Initial value | 0 for each longhand |
| Inherited | No |
| Applies to | All elements |
| Computed value | Absolute length for each longhand |
| Animation type | Each longhand animates as a length |
| Percentages | Not accepted |
Valid Values
.target {
scroll-margin-inline: 20px;
}
.target {
scroll-margin-inline: 20px 40px;
}
.target {
scroll-margin-inline: 2rem 1rem;
}
.target {
scroll-margin-inline: -10px 30px;
}
.target {
scroll-margin-inline: calc(1rem + 5px) 2rem;
}
Invalid Values
.target {
scroll-margin-inline: auto;
}
.target {
scroll-margin-inline: 10%;
}
.target {
scroll-margin-inline: 10px 20px 30px;
}
.target {
scroll-margin-inline: smooth;
}
.target {
scroll-margin-inline: 1s;
}
The shorthand accepts no more than two length values. Percentages, auto, and scrolling behavior keywords are invalid.
How It Works
The scroll-margin-inline property expands or contracts the scroll alignment area around an element at its inline-start and inline-end edges.
The browser can use this adjusted area when aligning the element during scroll snapping, anchor navigation, or programmatic scrolling.
Equal Inline Margins
.target {
scroll-margin-inline: 30px;
}
The scroll alignment area extends 30 pixels beyond both inline edges.
Different Inline Margins
.target {
scroll-margin-inline: 20px 60px;
}
The inline-start edge receives 20 pixels, while the inline-end edge receives 60 pixels.
Negative Inline Margins
.target {
scroll-margin-inline: -10px 20px;
}
The alignment area contracts at inline-start and expands at inline-end.
No Layout Changes
Scroll margins do not change an element's width, height, normal margins, or spacing between neighboring elements.
Available Scroll Range
The final scroll position depends on the scroll container's available range. Targets near its boundaries may not reach the requested alignment.
Writing Modes and Text Direction
The logical inline-start and inline-end edges depend on the writing mode and text direction.
Left-to-Right Horizontal Writing
.target {
writing-mode: horizontal-tb;
direction: ltr;
scroll-margin-inline: 20px 40px;
}
Inline-start corresponds to the left edge, and inline-end corresponds to the right edge.
Right-to-Left Horizontal Writing
.target {
writing-mode: horizontal-tb;
direction: rtl;
scroll-margin-inline: 20px 40px;
}
Inline-start corresponds to the right edge, and inline-end corresponds to the left edge.
Vertical Writing
.target {
writing-mode: vertical-rl;
direction: ltr;
scroll-margin-inline: 20px 40px;
}
In vertical writing with left-to-right inline progression, inline-start corresponds to the top and inline-end to the bottom.
Writing Mode Reference
| Writing Mode and Direction | Inline-Start | Inline-End |
|---|---|---|
horizontal-tb, ltr |
Left | Right |
horizontal-tb, rtl |
Right | Left |
vertical-rl, ltr |
Top | Bottom |
vertical-rl, rtl |
Bottom | Top |
vertical-lr, ltr |
Top | Bottom |
vertical-lr, rtl |
Bottom | Top |
Why Logical Properties Matter
Logical scroll margins allow the same CSS declarations to adapt to different text directions without manually switching between physical left, right, top, and bottom properties.
Using scroll-margin-inline With Scroll Snap
The scroll-margin-inline property is useful for horizontal scroll snapping, where each target can have additional alignment space on its inline edges.
Horizontal Scroll Snap Example
.gallery {
display: flex;
gap: 20px;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.gallery .item {
flex: 0 0 200px;
scroll-snap-align: start;
scroll-margin-inline: 20px;
}
Each item receives 20 pixels of scroll margin at both inline edges.
Different Start and End Margins
.gallery .item {
scroll-margin-inline: 40px 10px;
}
In left-to-right horizontal writing, each item's snap area extends 40 pixels to the left and 10 pixels to the right.
Combining With Scroll Padding
.gallery {
scroll-padding-inline: 20px;
}
.gallery .item {
scroll-margin-inline: 10px;
}
Scroll padding adjusts the container's optimal viewing region, while scroll margin adjusts the alignment area around each target.
Important Consideration
The shorthand does not enable scroll snapping. The container must define scroll-snap-type, and the targets must define scroll-snap-align.
Inline-Start and Inline-End Longhands
The scroll-margin-inline shorthand controls two longhand properties.
Equivalent Longhand Declarations
.target {
scroll-margin-inline-start: 20px;
scroll-margin-inline-end: 40px;
}
These declarations are equivalent to scroll-margin-inline: 20px 40px.
One-Value Shorthand
.target {
scroll-margin-inline: 30px;
}
Both longhands receive 30 pixels.
Overriding One Edge
.target {
scroll-margin-inline: 20px;
scroll-margin-inline-end: 60px;
}
Inline-start remains 20 pixels, while inline-end becomes 60 pixels.
Declaration Order
.target {
scroll-margin-inline-end: 60px;
scroll-margin-inline: 20px;
}
The later shorthand resets both inline margins to 20 pixels.
Important Consideration
Use the shorthand when both inline edges need related values. Use a longhand when only one edge requires an adjustment.
Logical Versus Physical Scroll Margins
Logical scroll margins follow writing direction, while physical scroll margins remain attached to fixed sides of an element.
| Property | Edge |
|---|---|
scroll-margin-inline-start |
Logical inline-start edge |
scroll-margin-inline-end |
Logical inline-end edge |
scroll-margin-left |
Physical left edge |
scroll-margin-right |
Physical right edge |
Physical Scroll Margins
.target {
scroll-margin-left: 20px;
scroll-margin-right: 40px;
}
These values always apply to the left and right edges.
Logical Scroll Margins
.target {
scroll-margin-inline: 20px 40px;
}
The values follow inline-start and inline-end, which can change with writing mode and direction.
Equivalent in Left-to-Right Horizontal Writing
.target {
writing-mode: horizontal-tb;
direction: ltr;
scroll-margin-inline: 20px 40px;
}
In this writing mode, the declaration corresponds to a 20-pixel left scroll margin and a 40-pixel right scroll margin.
Important Consideration
Physical and logical declarations can affect the same underlying edge. Normal CSS cascade rules determine which value applies when declarations overlap.
scroll-margin-inline Versus scroll-padding-inline
Both properties influence inline-axis scroll alignment, but they apply to different elements.
| Property | Applied To | Purpose |
|---|---|---|
scroll-margin-inline |
Scroll target | Expands or contracts the target's alignment area at its inline edges. |
scroll-padding-inline |
Scroll container | Defines insets at the container's logical inline edges. |
Target-Side Offset
.item {
scroll-margin-inline: 30px;
}
Container-Side Offset
.gallery {
scroll-padding-inline: 30px;
}
Combining Both Properties
.gallery {
scroll-padding-inline: 20px;
}
.gallery .item {
scroll-margin-inline: 10px;
}
Both offsets can influence where an item aligns during scrolling.
When to Use Each Property
Use scroll-padding-inline when the container needs consistent alignment insets. Use scroll-margin-inline when individual targets need their own scroll alignment spacing.
scroll-margin-inline Example
The following example compares two horizontal scroll panels. Both can scroll an item into view, but the second panel applies a 40-pixel inline-start margin and a 20-pixel inline-end margin to its target.
HTML Example
<div class="scroll-inline-demo">
<div class="scroll-example">
<h3>Without Scroll Margin</h3>
<div class="scroll-panel">
<div class="item">Item 1</div>
<div class="item target">Target</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
</div>
<button type="button">Scroll to Target</button>
</div>
<div class="scroll-example with-margin">
<h3>With Inline Scroll Margin</h3>
<div class="scroll-panel">
<div class="item">Item 1</div>
<div class="item target">Target</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
</div>
<button type="button">Scroll to Target</button>
</div>
</div>
CSS Example
.scroll-inline-demo {
display: grid;
gap: 20px;
}
.scroll-inline-demo .scroll-panel {
display: flex;
gap: 12px;
overflow-x: auto;
padding-block: 10px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
.scroll-inline-demo .item {
flex: 0 0 150px;
padding: 25px 10px;
text-align: center;
background-color: #e2e8f0;
}
.scroll-inline-demo .target {
background-color: #bfdbfe;
border: 2px solid #2563eb;
}
.scroll-inline-demo .with-margin .target {
scroll-margin-inline: 40px 20px;
}
.scroll-inline-demo button {
margin-top: 10px;
padding: 8px 12px;
cursor: pointer;
}
JavaScript Example
document.querySelectorAll(".scroll-inline-demo .scroll-example").forEach((example) => {
const button = example.querySelector("button");
const target = example.querySelector(".target");
button.addEventListener("click", () => {
target.scrollIntoView({
behavior: window.matchMedia("(prefers-reduced-motion: reduce)").matches ? "instant" : "smooth",
inline: "start",
block: "nearest"
});
});
});
Without Scroll Margin
With Inline Scroll Margin
Click each button to scroll its target into view. In left-to-right horizontal writing, the second panel requests additional space to the left of the target. The exact position depends on the available scroll range.
Using Equal Inline Margins
.scroll-inline-demo .with-margin .target {
scroll-margin-inline: 30px;
}
Both inline edges receive a 30-pixel scroll margin.
Removing the Scroll Margins
.scroll-inline-demo .with-margin .target {
scroll-margin-inline: 0;
}
The target returns to the default inline scroll margins.
Browser Compatibility Note
The final scroll position can vary with browser behavior, scrolling direction, and the available scroll range. When a target is near the beginning or end of the scrollable content, the requested offset may be limited.
Accessibility Considerations
Inline scroll margins can improve navigation by helping users see the intended target within horizontally or vertically scrolling interfaces.
Keep Scroll Targets Visible
Use suitable offsets when sticky or overlaid interface elements could obscure content at the inline edges.
Maintain Keyboard Accessibility
Buttons, links, and other controls used for scrolling should remain keyboard accessible and display visible focus indicators.
Consider Focus Management
Scrolling an element into view does not automatically move keyboard focus. Scripted interfaces may need additional focus handling.
Support Different Text Directions
Logical scroll margins help accommodate left-to-right, right-to-left, and vertical writing without relying on fixed physical sides.
Respect Reduced Motion Preferences
The property does not animate scrolling, but smooth scrolling used alongside it should respect prefers-reduced-motion.
Test Responsive Layouts
Check scroll alignment at different viewport widths and zoom levels, particularly when the number or size of visible items changes.
Common Mistakes
Using Percentages
.target {
scroll-margin-inline: 10%;
}
Percentages are invalid. Use CSS length values.
Using auto
.target {
scroll-margin-inline: auto;
}
The shorthand does not accept auto.
Providing Too Many Values
.target {
scroll-margin-inline: 10px 20px 30px;
}
The shorthand accepts one or two lengths, not three or four.
Assuming the First Value Always Means Left
The first value controls inline-start. In right-to-left horizontal writing, inline-start is the right edge.
Expecting Layout Spacing
Scroll margins do not add space between neighboring elements or change their dimensions.
Applying the Property to the Scroll Container
Apply scroll-margin-inline to the target. Use scroll-padding-inline on the container for container-side spacing.
Expecting Automatic Scroll Snapping
The shorthand adjusts alignment areas but does not enable scroll snapping.
Ignoring Declaration Order
A later shorthand declaration can override earlier inline-start or inline-end longhand declarations.
Confusing Scroll Margin With Smooth Scrolling
Scroll margin affects alignment. The scroll-behavior property or scrolling API options control scrolling animation.
Important Considerations
Keep the following points in mind when using scroll-margin-inline:
- It is a shorthand for
scroll-margin-inline-startandscroll-margin-inline-end. - One length sets both inline margins; two lengths set inline-start and inline-end separately.
- Both longhands have an initial value of
0and are not inherited. - Positive, zero, and negative lengths are valid.
- Percentages and
autoare invalid. - The physical edges corresponding to inline-start and inline-end depend on writing mode and direction.
- It changes scroll alignment areas without affecting normal layout spacing.
- It can influence scroll snapping and programmatic scrolling.
- It does not create a scroll container, enable scroll snapping, or control smooth scrolling.
- The
scroll-padding-inlineproperty provides container-side alignment insets. - Later shorthand declarations can reset previously specified longhand values.
- Available scroll range and browser behavior can affect the final alignment position.
- Test scrolling interfaces with different writing modes, directions, and viewport sizes.
Browser Support
The CSS scroll-margin-inline property is Baseline Widely available and is supported in current versions of major browsers.
Older browsers may have more limited support for logical scroll margin properties. The visible effect can also depend on the scrolling operation and available scroll range.
For current compatibility information, visit Can I Use? and search for scroll-margin-inline.
When unsupported, scrolling generally remains functional, but the additional inline scroll alignment offsets may not be applied.
Specifications
The scroll-margin-inline property is defined in CSS Scroll Snap Module Level 1.
CSS Scroll Snap Module Level 1
The specification defines the logical scroll margin properties and how they affect the scroll snap area surrounding a target.
For additional documentation and examples, consult MDN Web Docs.
