CSS Portal

CSS scroll-margin-inline Property

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The scroll-margin-inline property controls the amount of logical inline-axis space that the user agent preserves when an element is brought into view by scrolling (for example via fragment navigation, programmatic scrolling, or scroll snapping). Rather than changing an element’s layout margin in the normal flow, this margin is applied only for the purposes of determining the element’s final position relative to the scrolling container’s inline edge when it is scrolled into view. Because it’s a logical inline property, the direction that counts as “inline” depends on the element’s writing mode and text direction (so left/right in horizontal text and top/bottom in vertical text).

In practical terms, scroll-margin-inline is useful whenever you need a consistent offset from the inline edge of a scroll container without altering the element’s visual layout. For example, in a horizontally scrolling carousel you can ensure the snapped item doesn’t sit flush against the container edge but leaves a comfortable breathing space; on RTL pages it will automatically apply the offset at the start/end appropriate to that direction. It’s also handy for anchor-targeted navigation in compositions where horizontal spacing matters, or when you want a different inline offset when elements are brought into view than their normal static margins would provide.

This property interacts closely with scroll snapping and the scroll-port padding model. When a container uses scroll snapping, the element’s scroll margin influences where that element will land when snapped; it effectively shifts the snap position away from the edge by the specified inline margin. For related behavior that affects the container’s viewport rather than the individual element, consider scroll-padding-inline. For controlling how snapping is applied on the container itself, see scroll-snap-type, and if you want to set inline and block scroll margins in one declaration, look at the related shorthand scroll-margin.

Note that because scroll-margin-inline only participates in scroll positioning logic, it will not influence normal layout computations such as element stacking, collisions, or intrinsic sizing. It’s a targeted tool for fine-tuning the user experience of scroll and snap interactions, letting you adjust the apparent arrival point of an element without changing how that element affects other boxes on the page.

Definition

Initial value
See individual properties
Applies to
all elements
Inherited
no
Computed value
See individual properties
Animatable
by computed value type
JavaScript syntax
object.style.scrolMarginInline

Interactive Demo

1
2
3
Scroll »

Syntax

scroll-margin-inline: >length<{1,2} 

Values

  • >length<An outset from the corresponding edge of the scroll container.

Example

<div class="controls">
<a href="#box1">Go to Box 1</a>
<a href="#box2">Go to Box 2 (with scroll-margin-inline)</a>
<a href="#box3">Go to Box 3</a>
</div>

<div class="scroller" tabindex="0">
<section id="box1" class="card">Box 1</section>
<section id="box2" class="card offset">Box 2 - scroll-margin-inline: 80px</section>
<section id="box3" class="card">Box 3</section>
</div>
/* Layout */
body {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial;
    margin: 24px;
    color: #222;
}

.controls {
    display: flex;
    gap: 12px;
    margin-bottom: 16px;
}

.controls a {
    text-decoration: none;
    background: #0069ff;
    color: white;
    padding: 8px 12px;
    border-radius: 6px;
    font-weight: 600;
}

/* Horizontal scroller */
.scroller {
    display: flex;
    gap: 16px;
    overflow-x: auto;
    padding: 12px;
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    scroll-snap-type: x mandatory; /* enable scroll snapping */
    scroll-behavior: smooth;      /* smooth scrolling for anchor links */
}

.card {
    flex: 0 0 320px;
    height: 160px;
    background: linear-gradient(180deg, #fff, #f3f7ff);
    border: 1px solid #d6e1ff;
    border-radius: 8px;
    padding: 16px;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    scroll-snap-align: start; /* snap to the start edge */
}

/* Demonstrate scroll-margin-inline: when an element is scrolled into view
   its inline offset from the container's edge will include this margin. */
.offset {
    scroll-margin-inline: 80px; /* push the element 80px away from the inline edge when scrolled into view */
    background: linear-gradient(180deg, #fff7ed, #fff3e0);
    border-color: #ffd7a6;
}

/* Make the scroller visually larger on small viewports for testing */
@media (max-width: 480px) {
    .card {
        flex: 0 0 260px;
        height: 140px;
    }
}

Browser Support

The following information will show you the current browser support for the CSS scroll-margin-inline property. Hover over a browser icon to see the version that first introduced support for this CSS property.

This property is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 1st January 2026

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!