Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS Scroll‑Snap | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 33 of 50

·css

CSS Scroll‑Snap

6 mins

Course progress66%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • `scroll-snap-stop:always` forces the browser to stop at each snap point.
  • Works on both horizontal and vertical axes.
  • Provide a fallback for browsers that lack support.

Scroll‑snap gives you native carousel‑like behavior without any JavaScript. The browser automatically stops scrolling at defined snap points.

Core Properties

PropertyDescription
scroll-snap-typeSets the axis (x, y, or both) and the strictness (mandatory vs proximity).
scroll-snap-alignDetermines which part of the child element aligns to the container’s snap point (start, center, end).
scroll-snap-stopnormal (default) or always – forces the scroll to always stop at each snap point.

Example – Horizontal Image Carousel

<div class="scroller">
  <img src="1.jpg" class="item" />
  <img src="2.jpg" class="item" />
  <img src="3.jpg" class="item" />
</div>
.scroller {
  display:flex;
  overflow-x:auto;
  scroll-snap-type:x mandatory;
  gap:1rem;
  padding:1rem;
}
.item {
  flex:0 0 80%;          /* each image takes 80% of viewport */
  scroll-snap-align:start;
  border-radius:8px;
}

Users can swipe through images; the browser snaps each image to the start edge.


Enhancing the Experience

  • Add scroll-behavior:smooth; for a gentle glide.
  • Use scroll-snap-stop:always; if you want hard stops on each item.
  • Provide fallback navigation arrows for browsers that don’t support scroll‑snap.

Accessibility Considerations

  • Ensure focus order matches visual order. When you hide scrollbars, users navigating with keyboards still need a way to reach each item (e.g., tabindex="0").
  • Respect prefers-reduced-motion – you may want to disable auto‑scroll or snap animation for users who prefer minimal motion.

Browser Support

All modern browsers (Chrome, Edge, Safari, Firefox) support scroll-snap. For older browsers, a simple overflow:auto still works, albeit without snapping.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonCSS Clip‑Path & MaskingContinue to lesson 34CSS Counters – Automatic NumberingBack to CSS Basics to Advanced curriculum