CSS scroll-margin-right Property

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

It adjusts the area used to align a scroll target without changing its normal layout spacing. This property is useful for horizontal scroll snapping, JavaScript scrolling, and providing additional alignment space near the right edge of a scroll container.

scroll-margin-right Syntax

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

Basic Syntax

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

Available Length Values

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

CSS-Wide Keywords

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

Positive values expand the scroll alignment area beyond the right edge, while negative values contract it. Percentages and auto are invalid.

Property Values

Value Description
<length> Sets the right scroll margin using a CSS length such as px, rem, or em.
0 Applies no additional right scroll margin. This is the initial value.
Positive length Expands the scroll alignment area beyond the right edge.
Negative length Contracts the scroll alignment area at the right edge.
calc() Calculates a valid CSS length.
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 Right

Valid Values

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

Invalid Values

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

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

How It Works

The scroll-margin-right property expands or contracts the scroll snap area surrounding an element at its physical right edge.

The browser can use this adjusted area when determining a target's position during scroll snapping or programmatic scrolling.

Default Right Scroll Margin

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

No additional right scroll margin is applied.

Positive Right Scroll Margin

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

The target's scroll alignment area extends 40 pixels beyond its right edge.

Negative Right Scroll Margin

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

The alignment area contracts by 10 pixels at the right edge.

Using Relative Lengths

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

Relative units can help the offset scale with typography settings.

Scroll Margin Does Not Affect Layout

Unlike margin-right, the property does not change the element's layout position or create spacing between neighboring elements.

Available Scroll Range

The requested alignment may be limited by the scroll container's available range. For example, an item near the end of a horizontal gallery may not be able to align at the requested right-side offset.

Using scroll-margin-right With Scroll Snap

The scroll-margin-right property can adjust the alignment of 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: end;
  scroll-margin-right: 30px;
}

Each item's scroll snap area extends 30 pixels beyond its right edge. With end alignment in a left-to-right horizontal gallery, this can leave additional space between the item and the container's right edge.

Different Offsets for Individual Items

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

The featured item receives a larger right scroll margin, allowing a greater alignment offset when sufficient scroll range is available.

Combining With Scroll Padding

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

Scroll padding defines a container-side inset, while scroll margin expands the alignment area surrounding each item.

Important Consideration

The property does not enable scroll snapping. The container needs scroll-snap-type, and its items need an appropriate scroll-snap-align value.

Programmatic Scrolling

The scroll-margin-right property can affect how an element is aligned when JavaScript scrolls it into view.

Using scrollIntoView()

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

In left-to-right horizontal writing, the target can align approximately 40 pixels from the right edge, provided there are no additional offsets and the container has sufficient scroll range.

Using Start Alignment

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

In left-to-right horizontal writing, start alignment uses the left edge. A right scroll margin primarily affects the opposite edge, so it may not produce the same visible offset.

Respecting Reduced Motion

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

Consider reduced-motion preferences when adding animated scrolling to an interface.

Important Consideration

The property changes scroll alignment, not scrolling speed or animation. Use scroll-behavior or scrolling API options to control smooth scrolling.

Using the scroll-margin Shorthand

The scroll-margin shorthand sets the 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 right and left receive 30 pixels.

Three-Value Shorthand

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

Top receives 10 pixels, right and left 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 Right Margin

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

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

Declaration Order

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

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

Physical Versus Logical Scroll Margins

The scroll-margin-right property always refers to the physical right edge. Logical scroll margins adapt to writing mode and text direction.

Property Edge
scroll-margin-right Physical right edge
scroll-margin-left Physical left 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-end: 40px;
}

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

Right-to-Left Horizontal Writing

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

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

Physical Right Margin

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

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

Choosing Between Physical and Logical Properties

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

Overlapping Declarations

Physical and logical scroll margin properties can affect the same edge. The CSS cascade determines which declaration takes precedence.

scroll-margin-right Versus scroll-padding-right

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

Property Applied To Purpose
scroll-margin-right Scroll target Expands or contracts the target's alignment area at the right edge.
scroll-padding-right Scroll container Defines an inset at the right edge of the container's optimal viewing region.

Target-Side Offset

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

Container-Side Offset

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

Combining Both Properties

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

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

When to Use Each Property

Use scroll-padding-right for a consistent right-side inset across a scroll container. Use scroll-margin-right when individual targets need different right-side offsets.

scroll-margin-right Example

This example compares two horizontally scrolling panels. The second panel adds a 40-pixel right scroll margin to its target. Click the buttons to compare the alignment.

HTML Example

<div class="scroll-right-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 Right 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-right-demo {
  display: grid;
  gap: 20px;
}
.scroll-right-demo .scroll-panel {
  display: flex;
  gap: 12px;
  overflow-x: auto;
  padding-block: 10px;
  border: 1px solid #cbd5e1;
  background-color: #f8fafc;
}
.scroll-right-demo .item {
  flex: 0 0 150px;
  padding: 25px 10px;
  text-align: center;
  background-color: #e2e8f0;
}
.scroll-right-demo .target {
  background-color: #bfdbfe;
  border: 2px solid #2563eb;
}
.scroll-right-demo .with-margin .target {
  scroll-margin-right: 40px;
}
.scroll-right-demo button {
  margin-top: 10px;
  padding: 8px 12px;
  cursor: pointer;
}

JavaScript Example

document.querySelectorAll(".scroll-right-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: "end",
      block: "nearest"
    });
  });
});
Browser Result

Without Scroll Margin

Item 1
Item 2
Target
Item 4
Item 5

With Right Scroll Margin

Item 1
Item 2
Target
Item 4
Item 5

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

Increasing the Right Offset

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

The target requests a larger right-side alignment offset.

Removing the Right Offset

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

The additional right scroll margin is removed.

Browser Compatibility Note

The final scroll position depends on the scrolling operation and available scroll range. A target near the beginning or end of a container may not reach its requested alignment.

Accessibility Considerations

Right scroll margins can help keep targets visible when navigating horizontal scrolling interfaces.

Keep Targets Visible

Use appropriate offsets when fixed or overlaid controls could obscure content near the right edge.

Maintain Keyboard Accessibility

Buttons and links that trigger scrolling should remain keyboard accessible and have 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 scroll-margin-right always affects the physical right edge, consider logical scroll margins when supporting right-to-left or vertical writing modes.

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 targets remain visible.

Common Mistakes

Using Percentages

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

Percentages are invalid. Use a CSS length.

Using auto

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

The property does not accept auto.

Providing Multiple Values

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

This longhand property accepts only one length.

Confusing Scroll Margin With Normal Margin

scroll-margin-right adjusts scroll alignment, while margin-right affects normal layout spacing.

Applying the Property to the Scroll Container

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

Assuming Right Always Means Inline-End

In right-to-left horizontal writing, inline-end is the left edge. The physical right edge remains unchanged.

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-right declaration.

Confusing Scroll Margin With Smooth Scrolling

Scroll margin affects alignment, while scroll-behavior or scrolling API options control scrolling animation.

Important Considerations

Keep the following points in mind when using scroll-margin-right:

  • 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 right edge, regardless of writing mode or direction.
  • It adjusts the scroll alignment area without changing 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-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-right 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-right property is Baseline Widely available and is supported in current versions of major browsers.

Older browsers may have limited support. The visible effect also depends on the scrolling operation and available scroll range.

For current compatibility information, visit Can I Use? and search for scroll-margin-right.

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

Specifications

The scroll-margin-right 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-right