← Back to HTML & CSS, page 4

html-css-0072 · Medium mid

How do you build a swipeable carousel with CSS scroll snap, and what accessibility points matter?

Scroll snap uses scroll-snap-type on the scroll container and scroll-snap-align on children to stop at defined positions. Make the scroller focusable and named, add buttons or links for non-touch users, and avoid auto-advance.

Full answer

Scroll snap makes a scrolling box stop at chosen positions, which gives the feel of a slideshow with no JavaScript.

Two sides work together.

  • On the scroll container: overflow-x: auto (or scroll) and scroll-snap-type: x mandatory. The first word is the axis. mandatory always snaps to a point, while proximity snaps only when the scroll ends near one.
  • On each child: scroll-snap-align: start, center or end marks where the child should align. scroll-snap-stop: always stops fast swipes from skipping over several items.

scroll-padding on the container leaves room for things like a sticky header. scroll-margin on children does the same per item. Flex with flex: 0 0 100% makes each slide as wide as the container.

Accessibility needs care. The scroll area should be reachable and named. Add tabindex="0", a role="region" and an aria-label so keyboard users can focus it and use the arrow keys, because not every browser makes scrollable areas focusable by default. Do not hide the content that is off-screen. Provide visible previous and next buttons or links for people who cannot swipe. Avoid auto-advancing slides.

Each slide fills the 300px track. If you scroll the track to 200px and let go, it snaps to 300px, the start of slide 2. The numbered links give a non-swipe way to reach each slide, and the track can be focused and scrolled with the arrow keys.

Test with a mouse wheel, a touch screen and a keyboard. Behaviour at the edges differs a little between browsers.

html
<style>
  body { margin: 0; font-family: sans-serif; }
  .track {
    display: flex;
    width: 300px;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
  }
  .slide {
    flex: 0 0 100%;
    height: 120px;
    scroll-snap-align: start;
    scroll-snap-stop: always;
    display: grid; place-items: center;
    font-size: 24px;
  }
  .slide:nth-child(1) { background: #cde; }
  .slide:nth-child(2) { background: #fcd; }
  .slide:nth-child(3) { background: #dfc; }
</style>
<div class="track" id="track" tabindex="0" role="region" aria-label="Course highlights">
  <div class="slide" id="s1">Slide 1</div>
  <div class="slide" id="s2">Slide 2</div>
  <div class="slide" id="s3">Slide 3</div>
</div>
<p><a href="#s1">1</a> <a href="#s2">2</a> <a href="#s3">3</a></p>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • scroll-snap-type sets the axis and strictness on the container.
  • scroll-snap-align on children marks the snap points.
  • scroll-padding and scroll-margin offset the snap positions.
  • Add tabindex, a label and visible controls for accessibility.
  • Do not auto-advance slides without a pause control.

Common mistakes

  • Using mandatory snapping with slides taller than the container, which can trap users away from content.
  • Building a carousel that only works by swiping, with no buttons or links.
  • Auto-advancing slides with no pause control, which fails accessibility guidelines for moving content.

Follow-ups

  • What is the difference between mandatory and proximity?
  • How would you keep the snapped item from hiding behind a sticky header?

Tips

  • Explain the two sides: type on the container and align on the children.
  • Mention proximity as the gentler option for long content.