CSS Overscroll Behavior
In this page:
selector {
overscroll-behavior: auto | contain | none;
}
overscroll-behavior से Scroll Chaining रोकना
Default रूप से, एक बार जब आप किसी nested element (जैसे modal या sidebar) को उसके edge तक scroll कर लेते हैं, तो आगे का scroll input parent page तक chain हो जाता है और उसे भी scroll करता रहता है। overscroll-behavior: contain उस hand-off को रोक देता है, scroll gesture को उसी element के अंदर trapped रखते हुए जहाँ से यह शुरू हुआ था।
- chat windows और modals पर overscroll-behavior: contain set करें ताकि लंबी message list scroll करते users गलती से कभी उसके पीछे के page को scroll न करें।
- Accepted values:
- auto — normal scroll chaining (default)
- contain — parent तक chaining रोकता है
- none — chaining और bounce जैसे browser effects दोनों रोकता है
- overscroll-behavior-x और overscroll-behavior-y से per axis set किया जा सकता है
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Stopping Scroll Chaining with overscroll-behavior
<style>
.modal {
overflow-y: auto;
height: 100px;
background: #f0f0f0;
border: 1px solid #ccc;
padding: 8px;
overscroll-behavior: contain;
}
</style>
<div class="modal">
<p>Long scrollable content that won't chain to the page.</p>
<p>Paragraph 1 of filler content to make this box scrollable.</p>
<p>Paragraph 2 of filler content to make this box scrollable.</p>
<p>Paragraph 3 of filler content to make this box scrollable.</p>
<p>Paragraph 4 of filler content to make this box scrollable.</p>
<p>Paragraph 5 of filler content to make this box scrollable.</p>
<p>Paragraph 6 of filler content to make this box scrollable.</p>
<p>Paragraph 7 of filler content to make this box scrollable.</p>
<p>Paragraph 8 of filler content to make this box scrollable.</p>
<p>Paragraph 9 of filler content to make this box scrollable.</p>
<p>Paragraph 10 of filler content to make this box scrollable.</p>
<p>...it won't scroll the page behind it.</p>
</div>
overscroll-behavior-x और -y से Axis-Specific Control
overscroll-behavior-x और overscroll-behavior-y आपको सिर्फ एक axis पर scroll chaining contain करने देते हैं, दूसरे axis को normally chain करने के लिए free छोड़ते हुए।
यह horizontally-scrolling carousels के लिए खासतौर पर उपयोगी है, जहाँ आप horizontal chaining रोकना चाहते हैं लेकिन फिर भी page को हमेशा की तरह vertically scroll होने देना चाहते हैं।
- horizontal image carousels पर overscroll-behavior-x: contain इस्तेमाल करें ताकि images के through swipe करना mobile पर कभी vertical page scroll trigger न करे।
- Accepted values:
- auto — default scroll chaining (initial value)
- contain — ancestors तक कोई chaining नहीं, bounce effects बनाए रखता है
- none — कोई chaining नहीं और कोई bounce effect नहीं
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Axis-Specific Control with overscroll-behavior-x and -y
<style>
.carousel {
display: flex;
gap: 8px;
overflow-x: auto;
background: #f0f0f0;
border: 1px solid #ccc;
padding: 8px;
overscroll-behavior-x: contain;
}
.carousel .card {
flex: 0 0 120px;
height: 80px;
border-radius: 4px;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
}
</style>
<div class="carousel">
<div class="card" style="background:#e74c3c">1</div>
<div class="card" style="background:#3498db">2</div>
<div class="card" style="background:#2ecc71">3</div>
<div class="card" style="background:#f1c40f">4</div>
<div class="card" style="background:#9b59b6">5</div>
<div class="card" style="background:#e67e22">6</div>
<div class="card" style="background:#1abc9c">7</div>
</div>
overflow-anchor से Layout Jumps रोकना
overflow-anchor scroll anchoring control करता है, एक browser feature जो viewport के ऊपर का content size बदलने पर (जैसे कोई image load पूरी होना) आपकी visual scroll position को automatically stable रखता है।
इसे none set करना इस automatic correction को disable कर देता है, जो कभी-कभी chat apps के लिए उपयोगी है जो जानबूझकर scroll को bottom पर pin करते हैं।
- लगभग हर case में overflow-anchor को उसकी default value auto पर छोड़ें, क्योंकि scroll anchoring ही वह चीज़ है जो page load होते समय jarring jumps को रोकती है।
- Accepted values:
- auto — scroll anchoring चालू (initial value)
- none — scroll anchoring बंद
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
scroll-margin से Snap Targets को Offset करना
scroll-margin किसी element के चारों ओर invisible space जोड़ता है जो सिर्फ यह असर करता है कि browser कहाँ scroll रोकता है जब वह element एक scroll-snap target बनता है -- यह element की visible layout position को कभी नहीं बदलता जैसे एक normal margin बदल देती।
यह किसी snapped section और उसे overlap कर सकने वाले किसी fixed header के बीच एक gap छोड़ने के लिए उपयोगी है।
- scroll-margin-top को अपने fixed header की height के बराबर set करें ताकि scroll-snapped sections header के नीचे साफ़ तरीके से रुकें, उसके नीचे छुपने के बजाय।
- Accepted values:
- length — जैसे 80px
- एक से चार values — top, right, bottom, left order
- 0 — कोई margin नहीं (initial value)
- scroll-margin-top और दूसरे side longhands
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Offsetting Snap Targets with scroll-margin
<style>
.container {
height: 150px;
overflow-y: scroll;
scroll-snap-type: y mandatory;
border: 1px solid #ccc;
}
.container section {
scroll-snap-align: start;
scroll-margin-top: 60px;
height: 120px;
padding: 10px;
box-sizing: border-box;
}
</style>
<div class="container">
<section id="s1" style="background:lightblue">Section 1 — stops cleanly below a 60px fixed header</section>
<section id="s2" style="background:lightgreen">Section 2 — snaps here next</section>
<section id="s3" style="background:lightyellow">Section 3 — last snap target</section>
</div>
scroll-padding से Scroll Container को Offset करना
scroll-padding scroll-margin जैसा ही काम करता है लेकिन इसके अंदर के target elements के बजाय खुद scrolling container पर set किया जाता है, scroll-snap calculations के लिए इस्तेमाल होने वाले effective viewport को shrink करते हुए।
यह container-side equivalent है, उपयोगी जब किसी list में हर snap target को container edges से एक जैसा consistent offset चाहिए।
- अंदर के हर individual snap target पर scroll-margin-top set करने के बजाय scroll container पर scroll-padding-top इस्तेमाल करें।
- Accepted values:
- length — जैसे 80px
- percentage — scrollport के relative
- auto — कोई padding नहीं (initial value)
- एक से चार values — top, right, bottom, left order
- inherit | initial | unset | revert — हर property द्वारा accepted CSS-wide keywords
उदाहरण: Offsetting the Scroll Container with scroll-padding
<style>
.container {
scroll-padding-top: 60px;
overflow-y: scroll;
scroll-snap-type: y mandatory;
height: 150px;
background: #f0f0f0;
border: 1px solid #ccc;
}
.container section {
scroll-snap-align: start;
height: 120px;
padding: 10px;
box-sizing: border-box;
}
</style>
<div class="container">
<section style="background:lightblue">Section 1 — container reserves 60px at the top via scroll-padding</section>
<section style="background:lightgreen">Section 2</section>
<section style="background:lightyellow">Section 3</section>
</div>
- यह भूल जाना कि overscroll-behavior सिर्फ यह असर करता है कि कोई scrollable element अपने edge तक पहुँचने पर क्या होता है, general scrolling को नहीं।
- contain के बजाय overscroll-behavior: none इस्तेमाल करना, जो कुछ users के भरोसा किए जाने वाले native pull-to-refresh और bounce effects को भी disable कर देता है।
- margin के बजाय scroll-margin लागू करना और उम्मीद करना कि यह layout spacing बदल देगा, जबकि यह सिर्फ scroll-snap stopping position को असर करता है।
- overscroll-behavior (और इसके -x/-y variants) scroll chaining रोकता है, इसलिए किसी nested box को scroll करना उसके पीछे के page को भी scroll नहीं करता।
- overflow-anchor उस jarring jump को रोकता है जो तब होता है जब आपकी current scroll position के ऊपर content load होता है।
- scroll-margin और scroll-padding क्रमशः scroll-snap targets और scroll containers के चारों ओर invisible offset space जोड़ते हैं।
overscroll-behavior और overflow-anchor हर modern Chromium और Firefox browsers में supported हैं; overscroll-behavior के लिए Safari support हाल ही में आया है, इसलिए हमेशा specifically iOS Safari पर test करें।
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: