Skip to content
R.

How do I stop CSS scroll snap from skipping important panels?

Use `scroll-snap-stop: always` on the child panels that must not be skipped, while leaving the container's snap axis and strictness unchanged.

About 14 min readComments

A scroll snap carousel can have valid snap points and still fail in a very specific way: a fast gesture lands on a later panel without stopping at the one you meant to protect. That is not the same as broken snapping, and it is not solved by moving a fixed header or by changing the scroll axis.

The narrower repair is to keep the container's `scroll-snap-type` unchanged and give the important child `scroll-snap-stop: always`. That tells the browser to treat that panel as a required stop during the scroll operation, while the rest of the track keeps its normal snapping behavior.

What actually goes wrong when a panel is skipped?

A scroll snap carousel can appear correct in careful dragging and still fail during a faster wheel, swipe, or trackpad gesture. In that case, the browser is not ignoring snapping altogether. It is honoring the snap container, but the scroll operation continues far enough that an intermediate snap target never becomes the landing point. That is a different failure from “the container does not snap.”

The important distinction is that the reader is not trying to redesign the whole motion model. They are trying to protect one panel that should not be passed over. The example therefore centers on a four-panel track where the second panel is visually marked as important. If the track can jump from panel 1 to panel 3 on a long gesture, the bug is visible. If it only ever lands on the intended panel, the fix is doing real work.

That framing matters because the correct repair is local. The scroll container still owns the axis and strictness. The child panel only changes whether a scroll operation may continue past it. That is why the article talks about a skipped panel rather than a broken snap container or a fixed-header overlap problem.

  • The problem is a skipped intermediate panel, not broken snapping.
  • The container keeps `scroll-snap-type: x mandatory` in both states.
  • Only the child panel that matters gets `scroll-snap-stop: always`.
main.mjs / javascript
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>`;

SourcesMDN: Scroll snap guide (opens a new tab)MDN: scroll-snap-type (opens a new tab)

Keep the container contract separate from the child stop rule

The repair only makes sense if the container and child roles stay separate. In the supplied fixture, the track is a horizontal scroll container with `scroll-snap-type: x mandatory`. That choice establishes the direction and the strictness of snapping. It does not, by itself, guarantee that every eligible child will interrupt a long scroll gesture. The container answers a different question: where can snapping happen, and how strongly should the browser enforce it?

`scroll-snap-align` is the other half of the base contract. Each panel needs a snap position or the browser has nothing useful to land on. But the panel still may be skipped if the scroll motion travels past it before settling. That is why the child in the example gets `scroll-snap-stop: always`. The browser still sees the same snap positions, but the marked panel becomes a required stop during that scroll operation.

This division of responsibility is what makes the fix understandable. The track keeps the same axis and strictness in both normal and repaired states. The only thing that changes is the stop value on the protected child. If the wrong panel is skipped, changing the container to a different snap mode would hide the symptom without explaining it.

  • `scroll-snap-type` belongs on the scrolling container.
  • `scroll-snap-align` still marks each panel as a snap target.
  • `scroll-snap-stop` changes whether a target can be passed during one operation.

SourcesMDN: scroll-snap-type (opens a new tab)MDN: Scroll snap guide (opens a new tab)

Why the difference shows up only on a fast gesture

The supplied fixture compares two behaviors that look almost the same in code but differ during one large scroll operation. In normal mode, the protected panel is still a snap target, yet it does not force the browser to halt there. A long wheel movement or fling can therefore continue far enough to settle on a later panel. That is why the bug can remain invisible when the track is tested with a slow, careful drag.

When the same child receives `scroll-snap-stop: always`, the browser must stop there during that operation. The user can still reach later panels on a subsequent scroll, but the path can no longer leap across the important one in a single motion. That is exactly the behavior you want when the skipped panel is a warning, a review step, a mandatory decision, or any other panel the user should not accidentally bypass.

This also explains why the fix is not a decorative styling change. The panels can look identical in both states, and the important panel can remain in the same place. What changes is the route the browser is allowed to take through the track. The comparison is about scroll travel, not spacing, color, or a different layout algorithm.

  • Normal mode allows the fast gesture to continue past the protected panel.
  • `always` mode makes the important panel an intermediate landing point.
  • The same content and viewport make the comparison meaningful.

SourcesMDN: scroll-snap-type (opens a new tab)MDN: Scroll snap guide (opens a new tab)

What this fix is not: scroll anchoring, fixed headers, or a general scroll bug

It is easy to drift into nearby explanations when a carousel behaves oddly. Scroll anchoring is about keeping the current view stable when content changes above it. That can matter in scroll containers, but it is not the same as protecting a snap target from being skipped. The same is true of fixed-header overlap. A panel buried under a sticky bar is a visibility problem; a panel skipped during a fast gesture is a travel problem.

MDN's scroll anchoring guide helps separate those two ideas. A scroll container can stabilize its position across layout changes and still use snapping for navigation. One mechanism keeps the viewport from jumping when content moves. The other determines where scrolling may land. In this article, the layout does not change, so anchoring is not the answer the reader needs.

The example keeps the sizes and spacing steady for that reason. If the track still skips the important panel after the child has `scroll-snap-stop: always`, the next thing to investigate is browser support or the way the gesture is being generated. It is not a signal to start adjusting unrelated overflow or anchoring properties just to make the symptom disappear.

  • Scroll anchoring and scroll snapping solve different problems.
  • Anchoring protects the current view from layout changes above it.
  • Snap-stop protects a marked snap target from being passed during a scroll operation.

SourcesMDN: Scroll anchoring guide (opens a new tab)MDN: Scroll snap guide (opens a new tab)

How the fixture verifies the rule without overclaiming

The supplied browser fixture and tests keep the proof boundary narrow. They verify that the container still reports `x mandatory`, that the protected child can be switched between `normal` and `always`, and that the same directional `scrollBy()` call lands at 900 pixels in normal mode and stops at the protected panel at 300 pixels in always mode. That is enough to prove the CSS contract in the example. It is not enough to prove every browser, every input device, or every momentum model will behave identically.

The tests also guard against a false fix. They do not change the container axis when they switch the child stop value. They do not invent a third mode. They check that the reset path returns the fixture to the explicit normal fallback and zero scroll offset, which helps keep the example honest as a reproducible teaching asset rather than a one-off screenshot state.

That boundary is the right one for a debugging article. The reader needs a small, controlled example that distinguishes the broken and repaired behaviors. A local regression test can do that. It cannot replace a browser compatibility matrix, and it should not be described as if it could. The useful conclusion is narrower: if a panel must not be skipped, mark that child with `scroll-snap-stop: always` and keep the track's snap contract unchanged.

  • Test the protected panel with a large gesture, not only a careful drag.
  • Confirm the container still reports the intended snap type.
  • Treat the browser result as a controlled observation, not a universal guarantee.
main.test.mjs / javascript
import test from 'node:test';
import assert from 'node:assert/strict';
import { chromium } from 'playwright';
import { carouselFixture } from './main.mjs';

async function withPage(run) {
  const browser = await chromium.launch({ headless: true, args: ['--no-sandbox'] });
  try {
    const context = await browser.newContext({ viewport: { width: 900, height: 500 } });
    await context.route('**/*', (route) => route.abort());
    const page = await context.newPage();
    await page.setContent(carouselFixture);
    await run(page);
  } finally {
    await browser.close();
  }
}

test('keyboard focus can toggle the protected panel to scroll-snap-stop: always', async () => {
  await withPage(async (page) => {
    await page.locator('#normal').focus();
    await page.keyboard.press('ArrowRight');

    const state = await page.evaluate(() => ({
      activeId: document.activeElement && 'id' in document.activeElement ? document.activeElement.id : null,
      stop: getComputedStyle(document.querySelector('#important-panel')).scrollSnapStop,
      snapType: getComputedStyle(document.querySelector('#track')).scrollSnapType,
    }));

    assert.equal(state.activeId, 'always');
    assert.equal(state.stop, 'always');
    assert.equal(state.snapType, 'x mandatory');
  });
});

test('setStop rejects unsupported values instead of inventing a third mode', async () => {
  await withPage(async (page) => {
    const error = await page.evaluate(() => {
      try {
        window.setStop('skip');
        return null;
      } catch (err) {
        return { name: err.name, message: err.message };
      }
    });

    assert.deepEqual(error, {
      name: 'TypeError',
      message: 'stop must be "normal" or "always"',
    });

    const stop = await page.evaluate(() => getComputedStyle(document.querySelector('#important-panel')).scrollSnapStop);
    assert.equal(stop, 'normal');
  });
});

test('reset returns the fixture to the explicit normal fallback and clears scroll offset', async () => {
  await withPage(async (page) => {
    await page.evaluate(() => {
      window.setStop('always');
      const track = document.querySelector('#track');
      track.scrollLeft = 180;
    });

    await page.locator('#reset').click();
    await page.waitForFunction(() => {
      const track = document.querySelector('#track');
      const panel = document.querySelector('#important-panel');
      return track.scrollLeft === 0 && panel.style.scrollSnapStop === 'normal';
    });

    const state = await page.evaluate(() => ({
      left: document.querySelector('#track').scrollLeft,
      stop: getComputedStyle(document.querySelector('#important-panel')).scrollSnapStop,
      normalChecked: document.querySelector('#normal').checked,
      alwaysChecked: document.querySelector('#always').checked,
    }));

    assert.equal(state.left, 0);
    assert.equal(state.stop, 'normal');
    assert.equal(state.normalChecked, true);
    assert.equal(state.alwaysChecked, false);
  });
});

test('the same directional scroll travels to the last panel in normal mode and stops at panel 2 in always mode', async () => {
  await withPage(async (page) => {
    const positions = await page.evaluate(() => {
      const track = document.querySelector('#track');
      const move = () => {
        track.scrollBy({ left: 900, behavior: 'instant' });
        return track.scrollLeft;
      };
      window.setStop('normal');
      const normal = move();
      track.scrollTo({ left: 0, behavior: 'instant' });
      window.setStop('always');
      const always = move();
      const subsequent = move();
      return { normal, always, subsequent };
    });
    assert.deepEqual(positions, { normal: 900, always: 300, subsequent: 900 });
  });
});

SourcesMDN: Scroll snap guide (opens a new tab)MDN: scroll-snap-type (opens a new tab)

Why the smallest repair is the right one

A tempting repair is to make the entire carousel more restrictive or to change the snap strictness so the browser seems less willing to travel. That can suppress the symptom, but it also changes the behavior of every panel. The article's question is narrower than that. Only one panel must not be skipped, so only that panel should receive the stronger stop rule.

This is also why `scroll-snap-stop: always` is preferable to adding more spacing or hoping the browser lands more reliably on the middle panel. Spacing changes the geometry. Snap-stop changes the movement contract. If the panel is important because the user must see it, the contract should express that requirement directly. The code then documents the intent instead of relying on an incidental layout shape.

The example is useful precisely because it stays small. One scroll container, four panels, one protected child, and one changed property are enough to show the mechanism. If you need more than that in a real interface, the next question is usually whether the panel should be skipped at all, whether the UI should be broken into separate steps, or whether the protected content needs a different interaction pattern entirely.

  • A broad fix on the whole track can hide the real issue.
  • Selective stop rules are easier to audit and easier to remove later.
  • The repair should match the actual failure, not the closest-sounding property.

SourcesMDN: Scroll snap guide (opens a new tab)MDN: Scroll anchoring guide (opens a new tab)

Share LinkedIn Email Subscribe

Discussion

Leave a comment

Comments appear after review. No email needed.

Image preview

Follow the blog

Choose email updates or your favorite RSS reader.

The weekly email

New articles on code, design, and AI. Fridays, only when there is something new.

By subscribing, you agree to weekly blog emails.
Confirm in your inbox. Unsubscribe anytime. Privacy

Use OpenRSS

Preview the feed, then choose a reader to subscribe.

Open in OpenRSS (opens a new tab)

Already have a reader?

Paste this link into your reader's Add feed option.

Prefer a reader? RSS does not require an email address. View XML feed