πŸŽ‰ 75% of content is free forever β€” Unlock Premium from $10/mo β†’
CW
Search courses…
πŸ’Ό Servicesℹ️ Aboutβœ‰οΈ ContactView Pricing Plansfrom $10

Scroll Snap

Web Styles🟒 Free Lesson

Advertisement

Scroll Snap

Snap points, horizontal scrolling, carousel patterns, and smooth scrolling.

Overview

Scroll snap creates smooth, controlled scrolling experiences.

Key Concepts

  • scroll-snap-type β€” Container snap behavior
  • scroll-snap-align β€” Child snap points
  • scroll-padding β€” Container padding for snap
  • scroll-margin β€” Element margin for snap
  • smooth scrolling β€” CSS and JS scrolling

Code Examples

/* Horizontal scroll snap */
.horizontal-scroll {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  gap: 1rem;
  padding: 1rem;
}

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

/* Vertical scroll snap */
.vertical-scroll {
  height: 100vh;
  overflow-y: auto;
  scroll-snap-type: y mandatory;
}

.vertical-scroll > * {
  height: 100vh;
  scroll-snap-align: start;
}

/* Carousel */
.carousel {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.carousel::-webkit-scrollbar {
  display: none;
}

.carousel-item {
  flex: 0 0 100%;
  scroll-snap-align: center;
}

/* Snap padding */
.scroll-container {
  scroll-padding: 100px 0;
}

.scroll-item {
  scroll-margin: 20px;
}

/* Smooth scrolling */
html {
  scroll-behavior: smooth;
}

/* Scroll to element */
.section {
  scroll-margin-top: 80px; /* Account for fixed header */
}

/* Full-page sections */
.fullpage {
  height: 100vh;
  scroll-snap-align: start;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Image gallery */
.gallery {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  gap: 0.5rem;
}

.gallery img {
  flex: 0 0 auto;
  height: 300px;
  scroll-snap-align: center;
}

/* Tab panels */
.tabs {
  scroll-snap-type: x mandatory;
  display: flex;
  overflow-x: auto;
}

.tab-panel {
  flex: 0 0 100%;
  scroll-snap-align: start;
}

Practice

Build a horizontal scrolling carousel with scroll snap.

⭐

Premium Content

Scroll Snap

Unlock this lesson and 900+ advanced tutorials with a Premium plan.

🎯End-to-end Projects
πŸ’ΌInterview Prep
πŸ“œCertificates
🀝Community Access

Already a member? Log in

Need Expert Web Development Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement