Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

scroll-margin — spacing when scrolling to an element

scroll-margin defines offsets around an element for scroll-into-view operations and scroll snapping, without changing layout spacing.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

scroll-margin: <length>{1,4};

scroll-margin expands the snap area from the bounding rectangle of an element’s transformed border box. It does not create layout spacing like margin. Set it on the target element; scroll-padding belongs on the scrolling container.

It accepts 1–4 lengths: all sides; top/bottom and left/right; top, left/right, bottom; top, right, bottom, left. Negative values are allowed, but percentages and auto are not. Initially 0, it is not inherited.

It also affects fragment navigation and scrollIntoView() without snapping enabled. It does not enable snapping itself. Scroll boundaries can prevent the requested spacing.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>scroll-margin example</title>
<style>
  #scroller { height: 120px; overflow: auto; }
  .spacer { height: 200px; }
  #target { height: 40px; scroll-margin: 24px 0 0; background: gold; }
</style>
<button type="button" id="show">Show target</button>
<div id="scroller">
  <div class="spacer"></div>
  <div id="target">Target</div>
  <div class="spacer"></div>
</div>
<pre id="result"></pre>
<script>
  const scroller = document.getElementById('scroller');
  const target = document.getElementById('target');
  document.getElementById('show').addEventListener('click', () => {
    target.scrollIntoView({ block: 'start', behavior: 'instant' });
    document.getElementById('result').textContent =
      `scrollTop: ${scroller.scrollTop}`;
  });
</script>
</html>

Result

scrollTop: 176

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources