Scroll Snap - Carousels and Galleries

What You'll Learn

  • Enable scroll snapping with scroll-snap-type
  • Control snap alignment with scroll-snap-align (start, center, end)
  • Understand mandatory vs proximity snap strictness
  • Create horizontal carousels and vertical section scrolling
  • Force sequential viewing with scroll-snap-stop
  • Build image galleries, card carousels, and full-page scrolling
  • Combine scroll-snap with scroll-padding for perfect positioning

Introduction to Scroll Snap

CSS Scroll Snap provides native carousel and gallery functionality without JavaScript. Create smooth, touch-friendly scrolling interfaces that automatically snap to specific scroll positions - perfect for image galleries, product showcases, and full-page presentations.

Scroll snap is performant, touch-optimized, and works seamlessly with native scrolling physics. Users can swipe or scroll naturally, and the content snaps into place when they release.

scroll-snap-type: Enable Snap Scrolling

Apply scroll-snap-type to a scroll container to enable snap behavior. Specify the axis (x, y, or both) and strictness (mandatory or proximity).

/* Horizontal carousel - always snaps */ .carousel { scroll-snap-type: x mandatory; display: flex; overflow-x: auto; gap: 1rem; } /* Vertical sections - always snaps */ .fullpage { scroll-snap-type: y mandatory; overflow-y: auto; height: 100vh; } /* Gentle snapping - only when close */ .gallery { scroll-snap-type: x proximity; } /* Snap both directions */ .grid { scroll-snap-type: both mandatory; }

scroll-snap-align: Snap Position

Apply scroll-snap-align to child elements to define where they should align within the container when snapped.

/* Snap to start (left edge for horizontal) */ .item { scroll-snap-align: start; flex: 0 0 300px; } /* Snap to center (most common for galleries) */ .gallery-item { scroll-snap-align: center; flex: 0 0 90%; } /* Snap to end (right edge for horizontal) */ .card { scroll-snap-align: end; } /* No snapping for specific items */ .no-snap { scroll-snap-align: none; }

Choosing the Right Alignment

/* Use start for: */ .list-items { scroll-snap-align: start; /* Cards, lists, sections where alignment to the left/top makes sense */ } /* Use center for: */ .featured-images { scroll-snap-align: center; /* Image galleries, hero carousels, featured content */ } /* Use end for: */ .timeline-events { scroll-snap-align: end; /* Timeline layouts, right-aligned content */ }

Horizontal Carousels

Create horizontal scrolling carousels for images, cards, or products with just a few lines of CSS.

/* Container with horizontal snapping */ .carousel { scroll-snap-type: x mandatory; display: flex; overflow-x: auto; gap: 1rem; /* Hide scrollbar (optional) */ scrollbar-width: none; /* Firefox */ -ms-overflow-style: none; /* IE/Edge */ } .carousel::-webkit-scrollbar { display: none; /* Chrome/Safari */ } /* Items that snap to center */ .carousel-item { scroll-snap-align: center; flex: 0 0 80%; /* 80% width for peek effect */ max-width: 600px; }

Vertical Section Scrolling

Create full-page vertical scrolling experiences where each section snaps into view.

/* Full-page vertical snapping */ html { scroll-snap-type: y mandatory; } section { scroll-snap-align: start; min-height: 100vh; /* Full viewport height */ display: flex; align-items: center; justify-content: center; }

scroll-snap-stop: Force Sequential Viewing

Use scroll-snap-stop: always to prevent skipping snap points when scrolling quickly. Perfect for onboarding flows where users should see each step.

/* Force stopping at each snap point */ .onboarding-step { scroll-snap-align: center; scroll-snap-stop: always; /* Can't skip this item */ } /* Allow skipping (default) */ .optional-item { scroll-snap-align: center; scroll-snap-stop: normal; }

Image Gallery with Center Snap

Create a centered image gallery where each image snaps to the center of the viewport.

.gallery { scroll-snap-type: x mandatory; display: flex; overflow-x: auto; gap: 1rem; padding: 2rem; } .gallery-image { scroll-snap-align: center; flex: 0 0 85%; /* Slightly less than full width */ max-width: 800px; aspect-ratio: 16 / 9; border-radius: 12px; object-fit: cover; }

Card Carousel

Build product showcases or content cards with scroll snapping for a polished carousel experience.

.card-carousel { scroll-snap-type: x mandatory; display: flex; overflow-x: auto; gap: 2rem; padding: 2rem; } .card { scroll-snap-align: center; flex: 0 0 320px; background: white; border-radius: 12px; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1); transition: transform 0.3s ease; } .card:hover { transform: translateY(-8px); }

Combining with scroll-padding

Use scroll-padding with scroll snap to account for fixed headers or create custom spacing around snapped elements.

/* Add padding for fixed header */ .scroll-container { scroll-snap-type: y mandatory; scroll-padding-top: 80px; /* Height of fixed header */ overflow-y: auto; } /* Horizontal padding for peek effect */ .horizontal-scroll { scroll-snap-type: x mandatory; scroll-padding-inline: 5%; /* Space on left and right */ overflow-x: auto; }

Key Takeaways