CSS Scroll Snap Carousel

Create scroll-snap carousels and galleries using CSS scroll-snap properties for touch-friendly mobile interactions.

CssData Fetching

Code

.carousel {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  gap: 1rem;
}

.carousel > * {
  scroll-snap-align: start;
  flex: 0 0 300px;
}

Create scroll-snap carousels and galleries using CSS scroll-snap properties for touch-friendly mobile interactions.

Code

.carousel {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  gap: 1rem;
}

.carousel > * {
  scroll-snap-align: start;
  flex: 0 0 300px;
}

Explanation

scroll-snap-type and scroll-snap-align snap a scroll container to slide boundaries, giving a carousel feel without JavaScript. Expected behavior: after the user stops scrolling, the container settles on the nearest snap point. Caveat: mandatory snapping can trap touch momentum, so test with proximity before switching to mandatory.

Interactive Tools for This Topic