CSS Scroll Snap
In this page:
.container {
scroll-snap-type: x | y mandatory | proximity;
}
.item {
scroll-snap-align: start | center | end;
}
Scroll Snap क्या है?
Scroll Snap किसी scrollable container को visitor के scroll करने पर automatically predefined positions पर settle होने देता है, सिर्फ CSS इस्तेमाल करके किसी native carousel या slideshow का smooth, clicking-into-place feel बनाते हुए।
- Scroll Snap horizontally scrolling tool cards या image galleries के लिए बहुत अच्छा काम करता है, जहाँ हर item अपना खुद का साफ़, centered stop deserve करता है।
- Accepted values:
- scroll-snap-type — scroll container पर
- scroll-snap-align — हर child पर
- scroll-padding — snap area के लिए offset
- scroll-snap-stop — items को skip होने से रोकता है
उदाहरण: What Is Scroll Snap?
<style>
.container {
overflow-x: scroll;
scroll-snap-type: x mandatory;
display: flex;
gap: 10px;
}
.item {
scroll-snap-align: center;
display: inline-block;
width: 200px;
height: 80px;
background: lightblue;
flex-shrink: 0;
}
.item:nth-child(2) {
background: lightgreen;
}
</style>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
</div>
scroll-snap-type
scroll-snap-type खुद scrollable container पर set होता है, यह define करते हुए कि कौन-सा axis (x, y, या दोनों) snapping में participate करता है और snapping mandatory है या proximity।
- किसी horizontal carousel के लिए scroll-snap-type: x mandatory इस्तेमाल करें, और किसी full-screen vertical section scroller के लिए scroll-snap-type: y mandatory।
- Accepted values:
- none — default, कोई snapping नहीं
- x | y | both — snap करने के लिए axis
- block | inline — logical axes
- mandatory — हमेशा किसी point पर snap करता है
- proximity — सिर्फ पास होने पर snap करता है
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: scroll-snap-type
<style>
.container {
display: flex;
gap: 10px;
overflow-x: scroll;
scroll-snap-type: x mandatory;
background: #f0f0f0;
border: 1px solid #ccc;
padding: 10px;
}
.item {
scroll-snap-align: start;
flex-shrink: 0;
width: 200px;
height: 80px;
background: lightblue;
}
.item:nth-child(2) { background: lightgreen; }
.item:nth-child(3) { background: lightyellow; }
</style>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
scroll-snap-align
scroll-snap-align किसी snap container के children पर set होता है, exactly मार्क करते हुए कि जब browser उस पर settle हो तो हर एक को कहाँ align होना चाहिए, सबसे common रूप से start, center, या end।
- किसी carousel के लिए center एक natural choice है जहाँ scrolling रुकने पर हर item को visual focus जैसा महसूस होना चाहिए।
- Accepted values:
- start — item की start edge को snap करता है
- center — item के center को snap करता है
- end — item की end edge को snap करता है
- none — default, कोई snap position नहीं
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: scroll-snap-align
<style>
.container {
display: flex;
gap: 10px;
overflow-x: scroll;
scroll-snap-type: x mandatory;
background: #f0f0f0;
border: 1px solid #ccc;
padding: 10px;
}
.item {
scroll-snap-align: center;
flex-shrink: 0;
width: 200px;
height: 80px;
background: lightblue;
}
.item:nth-child(2) { background: lightgreen; }
</style>
<div class="container">
<div class="item">Marks where this child aligns when scrolling stops</div>
<div class="item">2</div>
</div>
Mandatory बनाम Proximity Snapping
Mandatory snapping हर scroll के बाद हमेशा किसी snap point पर आकर रुकता है, कोई exception नहीं। Proximity snapping सिर्फ तभी snap करता है जब scroll किसी snap point के पास रुकता है, free scrolling को कहीं और भी रुकने देते हुए।
- लंबे, free-scrolling content के लिए proximity इस्तेमाल करें जहाँ snapping एक gentle nudge जैसा महसूस होना चाहिए, कोई hard rule नहीं।
- Accepted values:
- scroll-snap-type: x mandatory — हमेशा किसी snap point पर settle होता है
- scroll-snap-type: x proximity — सिर्फ किसी point के पास snap करता है
- mandatory — tall items के साथ users को trap कर सकता है
- proximity — ज़्यादा gentle, browser तय करता है
उदाहरण: Mandatory vs Proximity Snapping
<style>
.row {
display: flex;
gap: 10px;
overflow-x: scroll;
padding: 10px;
margin-bottom: 12px;
background: #f0f0f0;
border: 1px solid #ccc;
}
.mandatory { scroll-snap-type: x mandatory; }
.proximity { scroll-snap-type: x proximity; }
.item {
scroll-snap-align: center;
flex-shrink: 0;
width: 200px;
height: 80px;
background: lightblue;
}
.item:nth-child(2) { background: lightgreen; }
</style>
<div class="row mandatory">
<div class="item">Always comes to rest on a snap point</div>
<div class="item">2</div>
</div>
<div class="row proximity">
<div class="item">Only snaps if it stops near one</div>
<div class="item">2</div>
</div>
scroll-padding और scroll-snap-stop
scroll-padding snap area को offset करता है, तब उपयोगी जब एक sticky header किसी snapped section के top को वरना cover कर देता। scroll-snap-stop: always browser को sequence में हर snap point पर रुकने पर मजबूर करता है, तेज़ scroll या swipe के दौरान कई को skip करने के बजाय।
- scroll-padding-top को अपने sticky header की height के बराबर set करें ताकि snapped content कभी उसके नीचे hidden न रहे।
- Accepted values:
- scroll-padding: <length> — scroll port edge को offset करता है
- scroll-snap-stop: normal — default, snap points skip कर सकता है
- scroll-snap-stop: always — हर snap point पर रुकना चाहिए
- scroll-padding-top — per-side variants मौजूद हैं
उदाहरण: scroll-padding and scroll-snap-stop
<style>
.container {
display: flex;
gap: 10px;
overflow-x: scroll;
scroll-snap-type: x mandatory;
scroll-padding-left: 60px;
background: #f0f0f0;
border: 1px solid #ccc;
padding: 10px;
}
.item {
scroll-snap-align: start;
scroll-snap-stop: always;
flex-shrink: 0;
width: 200px;
height: 80px;
background: lightblue;
}
.item:nth-child(2) { background: lightgreen; }
.item:nth-child(3) { background: lightyellow; }
</style>
<div class="container">
<div class="item">Offsets past a sticky header, stops at every item</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
- container पर scroll-snap-type set करना लेकिन उसके children पर scroll-snap-align भूल जाना, जिसका नतीजा बिल्कुल कोई visible snapping न होना है।
- सिर्फ एक या दो children वाले container पर mandatory snapping इस्तेमाल करना, जो scrolling को अजीब तरह से restrictive महसूस करा सकता है।
- snap container पर overflow: auto (या scroll) भूल जाना, इसके बिना उसके अंदर असल में scroll और snap करने के लिए कुछ नहीं है।
- scroll-snap-type किसी scrollable container को x, y, या दोनों axis के साथ एक snap container में बदल देता है।
- scroll-snap-align मार्क करता है कि हर child को कहाँ snap होना चाहिए, सबसे common रूप से start या center।
- Mandatory snapping हमेशा किसी snap point पर settle होता है, जबकि proximity snapping सिर्फ तभी ऐसा करता है जब scrolling किसी के पास रुके।
CSS Scroll Snap properties हर modern browser द्वारा supported हैं।
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Gradients
- CSS Shadows
- CSS Filters
- CSS Blend Modes
- CSS Transforms
- CSS 2D Transforms
- CSS 3D Transforms
- CSS Transitions
- CSS Animations
- CSS Image Styling
- CSS Image Effects
- CSS Hover Overlays
- CSS Image Modal
- CSS Image Centering
- CSS Image Shapes
- CSS Buttons
- CSS Columns
- CSS Clip Path
- CSS Shapes
- CSS Scroll Snap
- CSS Architecture: BEM
- CSS Custom Properties