# How do you replace an old slideshow script with CSS scroll snap?

Source: https://codelifter.com/forum/posts/how-do-you-replace-an-old-slideshow-script-with-css-scroll-snap/
Site: CodeLifter Forum, category Script Shelf
Published: 2026-11-21. Updated: 2026-11-21. Replies: 1.

## Question by CodeLifter editors (staff), 2026-11-21

Put the slides side by side in a horizontally scrolling container, add `scroll-snap-type: x mandatory` to the container and `scroll-snap-align: start` to each slide. Swiping, trackpads and scroll keys then land on whole slides with no script. Keep a few lines of JavaScript only for previous and next buttons, and drop the timer.

## What did the old slideshow scripts do?

The classic cut-and-paste slideshow kept an array of image paths, swapped an `<img>` element's `src` every few seconds with `setInterval`, and sometimes faded between images by changing opacity in a loop. It had three lasting problems: only one image existed in the page at a time, so nothing else was visible to search engines or screen readers; the timer kept running whether anyone was looking or not; and the only controls, if any, were two image links.

The W3C's [carousels tutorial](https://www.w3.org/WAI/tutorials/carousels/) lists what a slideshow needs today: users "must be able to pause carousel movement", "all functionality, including navigating between carousel items, must be operable by keyboard", changes "must be communicated to all users, including screen reader users", and focus must be managed sensibly. Scroll snap gets you most of the way there because it is ordinary scrolling.

## How does scroll snap work?

MDN's [basic concepts of scroll snap](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Scroll_snap/Basic_concepts) explains that it lets you "define the snap positions where a scroll container's scrollport may end or 'snap to' after a scrolling operation has completed." Two properties do the work:

| Property | Set on | What it does |
| --- | --- | --- |
| `scroll-snap-type` | The scrolling container | The axis (`x`, `y`, `block`, `inline`, `both`) and strictness (`mandatory` or `proximity`) |
| `scroll-snap-align` | Each slide | Where the slide lines up: `start`, `end`, `center` or `none` |
| `scroll-padding` | The container | An offset, for example to clear a fixed header |
| `scroll-snap-stop` | Each slide | Stops a fast swipe from skipping past slides |

The container must actually scroll: MDN notes that it needs "a defined size" and `overflow` enabled, and "if the content doesn't overflow its container, there is nothing to scroll."

## What does the replacement look like?

The markup puts every slide in the page:

```html
<section class="slideshow" aria-label="Workshop photos">
  <div class="track" tabindex="0">
    <figure><img src="bench.jpg" alt="Workbench with hand tools"><figcaption>The bench</figcaption></figure>
    <figure><img src="lathe.jpg" alt="Wood lathe mid-turn"><figcaption>The lathe</figcaption></figure>
    <figure><img src="shelf.jpg" alt="Finished bowls on a shelf"><figcaption>Finished work</figcaption></figure>
  </div>
  <button type="button" class="prev">Previous</button>
  <button type="button" class="next">Next</button>
  <p class="status visually-hidden" aria-live="polite" aria-atomic="true"></p>
</section>
```

The CSS replaces the timer and the fade loop:

```css
.track {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
}
.track > figure {
  flex: 0 0 100%;
  margin: 0;
  scroll-snap-align: start;
}
@media (prefers-reduced-motion: no-preference) {
  .track { scroll-behavior: smooth; }
}
```

The `tabindex="0"` lets keyboard users focus the track and scroll it. The smooth scrolling sits inside a `prefers-reduced-motion` query, a media feature MDN marks as available across browsers since January 2020, so people who have asked their system for less motion get an instant jump instead.

## Do you still need any JavaScript?

Only for the buttons and the announcement. The W3C tutorial's [functionality page](https://www.w3.org/WAI/tutorials/carousels/functionality/) recommends real `<button>` elements, a "polite" live region that receives text such as "Item x of y", and says "do not move keyboard focus when the previous or next buttons are used."

```js
const track = document.querySelector(".track");
const slides = track.children;
const status = document.querySelector(".status");

function go(step) {
  const width = track.clientWidth;
  const current = Math.round(track.scrollLeft / width);
  const target = Math.min(Math.max(current + step, 0), slides.length - 1);
  track.scrollBy({ left: (target - current) * width });
  status.textContent = `Photo ${target + 1} of ${slides.length}`;
}
document.querySelector(".prev").addEventListener("click", () => go(-1));
document.querySelector(".next").addEventListener("click", () => go(1));
```

`scrollBy()` is left without a `behavior` option on purpose. MDN's [scrollBy() page](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollBy) says that when it is omitted, the default `auto` follows "the computed value of the `scroll-behavior` CSS property on the element", so the reduced-motion rule above controls the buttons too. The snap points then settle the final position.

## What should you avoid?

- **`mandatory` with tall content.** MDN warns: "Never use `mandatory` if the content inside one of your child elements will overflow the parent container", because the overflow can become unreachable. Use `proximity` for slides of uneven size.
- **A missing `scroll-snap-align`.** With `mandatory`, MDN says a child set to `none` or left unset cannot be scrolled into view.
- **`scroll-snap-stop: always` by default.** MDN notes it can stop people "from quickly scrolling to their desired content."
- **Autoplay.** If you must rotate slides, provide a visible pause button; the W3C tutorial makes pausing a requirement, not an extra.

The [image rollover thread](/forum/posts/what-happened-to-the-old-image-rollover-script/) covers the other half of most old gallery scripts, and the [Script Shelf](https://codelifter.com/scripts/) records which original slideshow scripts were retired.

Checked on 12 November 2026.

## Short version

- Put all slides in the page inside one horizontally scrolling container.
- `scroll-snap-type: x mandatory` on the container and `scroll-snap-align: start` on each slide replace the old script.
- Use JavaScript only for previous and next buttons and a polite "Photo x of y" announcement.
- Wrap `scroll-behavior: smooth` in `prefers-reduced-motion: no-preference`.
- Drop the autoplay timer, or give it a pause button.

## Reply 1 by CodeLifter editors (staff), 2026-11-21

**How do you add dots or numbered buttons that jump to a slide?** Give each slide an `id`, render a list of buttons, and scroll the matching slide into view. The W3C carousels tutorial's [functionality page](https://www.w3.org/WAI/tutorials/carousels/functionality/) describes exactly this pattern: numbered buttons for each item, with the current one "highlighted both visually, and by using text that is visually hidden (for screen readers)", such as "(Current Slide)" inside the button.

It also gives the focus rule for this case, which differs from previous and next: "When users select an item with those navigation buttons, the focus should be set on the selected item." Slides cannot take focus by default, so add `tabindex="-1"` to each slide and call `slide.focus()` after scrolling. In a scroll-snap version that looks like:

```js
button.addEventListener("click", () => {
  slide.scrollIntoView({ block: "nearest", inline: "start" });
  slide.focus({ preventScroll: true });
});
```

Update the "(Current Slide)" text and any `aria-current` marker when the slide changes. If people can also swipe, keep the dots honest by recalculating the current index from `scrollLeft` after scrolling settles, rather than trusting the last button pressed. The basic concepts guide on [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Scroll_snap/Basic_concepts) confirms that `scroll-snap-align: start` decides where each slide lands, so the index arithmetic stays simple.

---
Cite as: CodeLifter Forum, "How do you replace an old slideshow script with CSS scroll snap?", https://codelifter.com/forum/posts/how-do-you-replace-an-old-slideshow-script-with-css-scroll-snap/, accessed 2026-11-21.
