CSS scroll-margin-left Property

The CSS scroll-margin-left property sets the scroll margin at the physical left edge of an element.

It adjusts the area used to align an element during scrolling without changing its normal layout spacing. The property is useful for horizontal scroll snapping, programmatic scrolling, and keeping scroll targets away from the left edge of a scroll container.

scroll-margin-left Syntax

The scroll-margin-left property accepts one CSS length value or a CSS-wide keyword.

Basic Syntax

selector {
  scroll-margin-left: 20px;
}

Available Length Values

.target {
  scroll-margin-left: 0;
}
.target {
  scroll-margin-left: 20px;
}
.target {
  scroll-margin-left: 2rem;
}
.target {
  scroll-margin-left: 1.5em;
}
.target {
  scroll-margin-left: -10px;
}
.target {
  scroll-margin-left: calc(1rem + 10px);
}

CSS-Wide Keywords

.target {
  scroll-margin-left: initial;
}
.target {
  scroll-margin-left: inherit;
}
.target {
  scroll-margin-left: unset;
}
.target {
  scroll-margin-left: revert;
}
.target {
  scroll-margin-left: revert-layer;
}

Positive lengths expand the scroll alignment area beyond the left edge. Negative lengths contract it. Percentages and auto are not valid values.

Property Values

Value Description
<length> Sets the left scroll margin using a CSS length such as px, rem, or em.
0 Applies no additional left scroll margin. This is the initial value.
Positive length Expands the scroll alignment area beyond the left edge.
Negative length Contracts the scroll alignment area at the left edge.
calc() Calculates a valid length using a CSS mathematical expression.
initial Resets the property to 0.
inherit Uses the computed value from the parent element.
unset Resets the property to 0 because it is 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
Inherited No
Applies to All elements
Computed value Absolute length
Animation type By computed value, as a length
Percentages Not accepted
Physical edge Left

Valid Values

.target {
  scroll-margin-left: 40px;
}
.target {
  scroll-margin-left: 3rem;
}
.target {
  scroll-margin-left: 0;
}
.target {
  scroll-margin-left: -5px;
}
.target {
  scroll-margin-left: calc(2rem + 15px);
}

Invalid Values

.target {
  scroll-margin-left: auto;
}
.target {
  scroll-margin-left: 10%;
}
.target {
  scroll-margin-left: 10px 20px;
}
.target {
  scroll-margin-left: smooth;
}
.target {
  scroll-margin-left: 2s;
}

The property accepts one CSS length, not percentages, multiple lengths, or scrolling behavior keywords.

How It Works

The scroll-margin-left property adjusts the scroll snap area surrounding an element by expanding or contracting its left edge.

The browser can use this adjusted area when determining where to position the element during scroll snapping, anchor navigation, or programmatic scrolling.

Default Left Scroll Margin

.target {
  scroll-margin-left: 0;
}

No additional scroll margin is applied at the left edge.

Positive Left Scroll Margin

.target {
  scroll-margin-left: 40px;
}

The scroll alignment area extends 40 pixels to the left of the target.

Negative Left Scroll Margin

.target {
  scroll-margin-left: -10px;
}

The scroll alignment area contracts by 10 pixels at the left edge.

Using Relative Lengths

.target {
  scroll-margin-left: 2rem;
}

Relative units can help offsets scale with typography and design settings.

Scroll Margin Does Not Affect Layout

Unlike margin-left, the scroll-margin-left property does not move the element or change the spacing between neighboring elements.

Available Scroll Range

The browser cannot always position a target at the requested offset. A target near a scroll container's boundaries may be limited by the available scroll range.

Using scroll-margin-left With Scroll Snap

The scroll-margin-left property can provide additional alignment space before items in a horizontal scroll snap container.

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-left: 30px;
}

For a left-to-right horizontal gallery, each item's snap area extends 30 pixels to the left of its border box.

Different Offsets for Individual Items

.gallery .item {
  scroll-margin-left: 20px;
}
.gallery .item.featured {
  scroll-margin-left: 60px;
}

The featured item receives a larger left scroll margin, allowing it to align farther from the container's left edge when space permits.

Combining With Scroll Padding

.gallery {
  scroll-padding-left: 20px;
}
.gallery .item {
  scroll-margin-left: 30px;
}

Scroll padding establishes an inset inside the container, while scroll margin expands the alignment area surrounding each target.

Important Consideration

The property does not enable scroll snapping by itself. The container must define scroll-snap-type, and the targets must define an appropriate scroll-snap-align value.

Programmatic Scrolling

JavaScript scrolling methods can use scroll margins when bringing an element into view.

Using scrollIntoView()

.target {
  scroll-margin-left: 40px;
}
document.querySelector(".target").scrollIntoView({
  behavior: "smooth",
  inline: "start",
  block: "nearest"
});

In a left-to-right horizontal layout, the target can align approximately 40 pixels from the left edge, assuming no other scroll offsets and sufficient scroll range.

Using the End Alignment

document.querySelector(".target").scrollIntoView({
  behavior: "smooth",
  inline: "end",
  block: "nearest"
});

End alignment generally uses the target's right edge in left-to-right horizontal writing. A left scroll margin primarily affects the opposite edge, so the visual effect may differ.

Respecting Reduced Motion

const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
document.querySelector(".target").scrollIntoView({
  behavior: reduceMotion ? "instant" : "smooth",
  inline: "start",
  block: "nearest"
});

Reduced-motion preferences should be considered when adding animated scrolling.

Important Consideration

The scroll-margin-left property does not control scrolling speed or animation. It only adjusts the target's scroll alignment area.

Using the scroll-margin Shorthand

The scroll-margin shorthand sets scroll margins for all four physical edges.

One-Value Shorthand

.target {
  scroll-margin: 20px;
}

All four scroll margins receive 20 pixels.

Two-Value Shorthand

.target {
  scroll-margin: 10px 30px;
}

Top and bottom receive 10 pixels, while left and right receive 30 pixels.

Three-Value Shorthand

.target {
  scroll-margin: 10px 20px 30px;
}

Top receives 10 pixels, left and right receive 20 pixels, and bottom receives 30 pixels.

Four-Value Shorthand

.target {
  scroll-margin: 10px 20px 30px 40px;
}

The values apply clockwise: top, right, bottom, and left.

Equivalent Longhand Declarations

.target {
  scroll-margin-top: 10px;
  scroll-margin-right: 20px;
  scroll-margin-bottom: 30px;
  scroll-margin-left: 40px;
}

Overriding the Left Margin

.target {
  scroll-margin: 20px;
  scroll-margin-left: 60px;
}

The left scroll margin becomes 60 pixels, while the other three edges remain at 20 pixels.

Declaration Order

.target {
  scroll-margin-left: 60px;
  scroll-margin: 20px;
}

The later shorthand resets the left scroll margin to 20 pixels along with the other three edges.

Physical Versus Logical Scroll Margins

The scroll-margin-left property always refers to the physical left edge. Logical scroll margin properties follow writing mode and text direction.

Property Edge
scroll-margin-left Physical left edge
scroll-margin-right Physical right edge
scroll-margin-inline-start Logical inline-start edge
scroll-margin-inline-end Logical inline-end edge

Left-to-Right Horizontal Writing

.target {
  writing-mode: horizontal-tb;
  direction: ltr;
  scroll-margin-inline-start: 40px;
}

In this writing mode, inline-start corresponds to the physical left edge.

Right-to-Left Horizontal Writing

.target {
  writing-mode: horizontal-tb;
  direction: rtl;
  scroll-margin-inline-end: 40px;
}

In this writing mode, inline-end corresponds to the physical left edge.

Physical Left Margin

.target {
  scroll-margin-left: 40px;
}

The left edge remains the same regardless of writing mode or direction.

Choosing Between Physical and Logical Properties

Use scroll-margin-left when the offset must remain on the physical left side. Use logical properties when the offset should follow the text direction or writing mode.

Overlapping Declarations

Physical and logical properties can affect the same edge. When they do, the CSS cascade determines which declaration applies.

scroll-margin-left Versus scroll-padding-left

Both properties influence horizontal scroll alignment, but they are applied to different elements.

Property Applied To Purpose
scroll-margin-left Scroll target Expands or contracts the target's scroll alignment area at its left edge.
scroll-padding-left Scroll container Defines a left inset within the container's optimal viewing region.

Target-Side Offset

.item {
  scroll-margin-left: 40px;
}

Container-Side Offset

.gallery {
  scroll-padding-left: 40px;
}

Combining Both Properties

.gallery {
  scroll-padding-left: 20px;
}
.gallery .item {
  scroll-margin-left: 30px;
}

For start alignment in a left-to-right horizontal gallery, these values can provide a combined 50-pixel offset between the target's left border edge and the container's left scrollport edge.

When to Use Each Property

Use scroll-padding-left when all targets need a consistent container-side inset. Use scroll-margin-left when individual targets need different offsets.

scroll-margin-left Example

The following example compares two horizontal scrolling panels. The second panel adds a 40-pixel left scroll margin to its target. Click the buttons to see how the target's alignment changes.

HTML Example

<div class="scroll-left-demo">
  <div class="scroll-example">
    <h3>Without Scroll Margin</h3>
    <div class="scroll-panel">
      <div class="item">Item 1</div>
      <div class="item">Item 2</div>
      <div class="item target">Target</div>
      <div class="item">Item 4</div>
      <div class="item">Item 5</div>
    </div>
    <button type="button">Scroll to Target</button>
  </div>
  <div class="scroll-example with-margin">
    <h3>With Left Scroll Margin</h3>
    <div class="scroll-panel">
      <div class="item">Item 1</div>
      <div class="item">Item 2</div>
      <div class="item target">Target</div>
      <div class="item">Item 4</div>
      <div class="item">Item 5</div>
    </div>
    <button type="button">Scroll to Target</button>
  </div>
</div>

CSS Example

.scroll-left-demo {
  display: grid;
  gap: 20px;
}
.scroll-left-demo .scroll-panel {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  padding-block: 10px;
  border: 1px solid #cbd5e1;
  background-color: #f8fafc;
}
.scroll-left-demo .item {
  flex: 0 0 150px;
  padding: 25px 10px;
  text-align: center;
  background-color: #e2e8f0;
}
.scroll-left-demo .target {
  background-color: #bfdbfe;
  border: 2px solid #2563eb;
}
.scroll-left-demo .with-margin .target {
  scroll-margin-left: 40px;
}
.scroll-left-demo button {
  margin-top: 10px;
  padding: 8px 12px;
  cursor: pointer;
}

JavaScript Example

document.querySelectorAll(".scroll-left-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"
    });
  });
});
Browser Result

Without Scroll Margin

Item 1
Item 2
Target
Item 4
Item 5

With Left Scroll Margin

Item 1
Item 2
Target
Item 4
Item 5

Click each button to align the target at the beginning of its horizontal scrolling area. In a left-to-right layout, the second panel requests approximately 40 pixels of space to the left of the target.

Increasing the Left Offset

.scroll-left-demo .with-margin .target {
  scroll-margin-left: 70px;
}

The target requests a larger left alignment offset.

Removing the Left Offset

.scroll-left-demo .with-margin .target {
  scroll-margin-left: 0;
}

The additional left scroll margin is removed.

Browser Compatibility Note

The final alignment depends on the browser, the scrolling operation, and the available scroll range. A target near the beginning or end of the scrollable content may not reach the requested offset.

Accessibility Considerations

Left scroll margins can improve navigation by keeping targets from aligning directly against the left edge of a scrolling interface.

Keep Targets Visible

Use appropriate offsets when fixed or overlaid interface elements could obscure content near the left edge.

Maintain Keyboard Accessibility

Scrolling controls should remain keyboard accessible and display visible focus indicators.

Consider Focus Management

Calling scrollIntoView() does not automatically move keyboard focus to the target. Additional focus management may be necessary for scripted navigation.

Support Different Writing Directions

Because this property always affects the physical left edge, consider using logical scroll margins when the interface must adapt to right-to-left or vertical writing.

Respect Reduced Motion Preferences

The property does not animate scrolling. When combining it with smooth scrolling, respect prefers-reduced-motion.

Test Responsive Layouts

Check scroll alignment at different viewport widths and zoom levels to ensure the target remains visible.

Common Mistakes

Using Percentages

.target {
  scroll-margin-left: 10%;
}

Percentages are invalid. Use a CSS length.

Using auto

.target {
  scroll-margin-left: auto;
}

The property does not accept auto.

Providing Multiple Values

.target {
  scroll-margin-left: 10px 20px;
}

This longhand property accepts only one length.

Confusing Scroll Margin With Normal Margin

scroll-margin-left affects scroll alignment, while margin-left affects normal layout spacing.

Applying the Property to the Scroll Container

Apply scroll-margin-left to the target. Use scroll-padding-left on the scroll container for container-side alignment spacing.

Assuming Left Always Means Inline-Start

In right-to-left horizontal writing, inline-start is the right edge. The physical left edge does not change.

Expecting Automatic Scroll Snapping

The property does not activate scroll snapping by itself.

Ignoring Declaration Order

A later scroll-margin shorthand can override an earlier scroll-margin-left declaration.

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-left:

  • It is a physical scroll margin longhand property.
  • Its initial value is 0, and it is not inherited.
  • It accepts one CSS length, including negative and calculated lengths.
  • Percentages and auto are invalid.
  • It always affects the physical left edge, regardless of writing mode or direction.
  • It changes the target's scroll alignment area without changing normal layout spacing.
  • It can influence scroll snapping, anchor navigation, and programmatic scrolling.
  • It does not create a scroll container, enable scroll snapping, or control smooth scrolling.
  • The scroll-margin shorthand controls all four physical scroll margins.
  • The scroll-margin-inline-start property follows the logical inline-start edge.
  • The scroll-margin-inline-end property follows the logical inline-end edge.
  • The scroll-padding-left property provides container-side alignment spacing.
  • Physical and logical scroll margin declarations can affect the same edge.
  • The available scroll range may limit the final alignment position.
  • Test scrolling interfaces across different viewport sizes and writing directions.

Browser Support

The CSS scroll-margin-left property is Baseline Widely available and is supported in current versions of major browsers.

Older browsers may have limited support. 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-left.

When unsupported, scrolling generally remains functional, but the additional left alignment offset may not be applied.

Specifications

The scroll-margin-left property is defined in CSS Scroll Snap Module Level 1.

CSS Scroll Snap Module Level 1

The specification defines how scroll margins expand or contract the scroll snap area surrounding a target.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: scroll-margin-left