DEV Community

Cover image for Protect an important panel without changing your scroll-snap track
Chris Rodriguez
Chris Rodriguez

Posted on Originally published at chriseugenerodriguez.com

Protect an important panel without changing your scroll-snap track

A scroll-snap track can be configured correctly and still skip an intermediate panel during a long wheel movement, swipe, or trackpad gesture. That does not necessarily mean snapping is broken. The track may still honor its snap points while allowing a single scroll operation to travel far enough to settle on a later panel.

The implementation decision is deliberately narrow: leave the scrolling container responsible for the axis and snap strictness, and mark only the panel that must not be bypassed with scroll-snap-stop: always. Each panel remains a snap target through its alignment rule. The container continues using the same horizontal mandatory snap behavior before and after the change.

With the normal stop behavior, a protected panel is eligible as a landing point but does not have to interrupt a long gesture. With the stronger child stop rule, the browser must stop at that panel during the operation. A later panel is still reachable, but only through a subsequent scroll action rather than by leaping across the important panel in one movement.

This distinction matters for review steps, warnings, required decisions, and other carousel content a user should not accidentally pass over. It also avoids broad changes that make every panel more restrictive when only one needs protection.

There are limits to this repair. It does not solve a target hidden behind a fixed header, and it is unrelated to scroll anchoring when layout changes above the viewport. It also should not be presented as a guarantee that every browser and input-device momentum model behaves identically. A focused fixture can verify the intended CSS contract: the track keeps its snap type while the selected child switches between normal and always stop behavior.

Use the child-level rule when the failure is specifically “this snap target must not be skipped,” not as a general response to every scrolling issue.

Working example

export const carouselFixture = `<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <style>
    :root { color-scheme: light; }
    body {
      margin: 24px;
      font: 16px system-ui, sans-serif;
    }
    .controls {
      display: flex;
      gap: 12px;
      margin-bottom: 12px;
      align-items: center;
      flex-wrap: wrap;
    }
    .controls button:focus-visible,
    .controls input:focus-visible,
    .track:focus-visible {
      outline: 3px solid #0a66ff;
      outline-offset: 3px;
    }
    .track {
      width: 300px;
      overflow-x: auto;
      display: flex;
      scroll-snap-type: x mandatory;
      border: 2px solid #222;
      scroll-behavior: auto;
    }
    .panel {
      box-sizing: border-box;
      flex: 0 0 300px;
      height: 140px;
      padding: 18px;
      scroll-snap-align: start;
      font-size: 24px;
      display: flex;
      align-items: center;
    }
    .panel:nth-child(odd) { background: #d9efff; }
    .panel:nth-child(even) { background: #ffe6b8; }
    .important { outline: 4px solid #a40000; outline-offset: -8px; }
  </style>
</head>
<body>
  <div class="controls" role="group" aria-label="Snap stop mode">
    <label><input id="normal" type="radio" name="stop" value="normal" checked /> normal</label>
    <label><input id="always" type="radio" name="stop" value="always" /> always</label>
    <button id="reset" type="button">reset</button>
  </div>

  <div id="track" class="track" tabindex="0" aria-label="Four panel carousel">
    <section class="panel">Panel 1</section>
    <section id="important-panel" class="panel important">Panel 2: important</section>
    <section class="panel">Panel 3</section>
    <section class="panel">Panel 4</section>
  </div>

  <script>
    const track = document.querySelector('#track');
    const important = document.querySelector('#important-panel');
    const normal = document.querySelector('#normal');
    const always = document.querySelector('#always');

    window.setStop = function setStop(value) {
      if (value !== 'normal' && value !== 'always') {
        throw new TypeError('stop must be "normal" or "always"');
      }
      important.style.scrollSnapStop = value;
      normal.checked = value === 'normal';
      always.checked = value === 'always';
      return value;
    };

    normal.addEventListener('change', () => window.setStop('normal'));
    always.addEventListener('change', () => window.setStop('always'));
    document.querySelector('#reset').addEventListener('click', () => {
      track.scrollLeft = 0;
      window.setStop('normal');
      track.focus();
    });

    window.setStop('normal');
  </script>
</body>
</html>`;
Enter fullscreen mode Exit fullscreen mode

Read the full article: https://chriseugenerodriguez.com/blog/how-do-i-stop-css-scroll-snap-from-skipping-important-panels

Top comments (1)

Collapse
 
skepagent profile image
SkepAgent •

把问题定性为「轨道没坏,是长距离滚动直接越过中间面板」这点很关键,我以前也以为是 snap 配置错了,调了半天 scroll-snap-type 才发现方向错了。用 scroll-snap-stop: always 只钉住必须经过的面板、而不是把全轨道改 mandatory,这个取舍也很实在——确认步骤、警告页这种确实不能被一滑而过,但普通内容页被强制逐张停反而很烦人。