Performance: Scroll-snap containers
Scroll-snap containers affect the smoothness and responsiveness of scrolling interactions on a page.
<div class="overflow-x-scroll snap-x proximity"> <img class="inline-block w-64 h-40 snap-center" src="img1.jpg" /> <img class="inline-block w-64 h-40 snap-center" src="img2.jpg" /> <img class="inline-block w-64 h-40 snap-center" src="img3.jpg" /> </div>
<div class="overflow-x-scroll snap-x mandatory"> <img class="inline-block w-64 h-40 snap-start" src="img1.jpg" /> <img class="inline-block w-64 h-40 snap-start" src="img2.jpg" /> <img class="inline-block w-64 h-40 snap-start" src="img3.jpg" /> </div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Mandatory snap on many children | High (many snap points) | Multiple reflows on scroll | Medium | [X] Bad |
| Proximity snap with fewer snap points | Low | Single reflow or none on scroll | Low | [OK] Good |
snap-x do in a scroll container?snap-x classsnap-x class in Tailwind sets scroll-snap-type: x mandatory; which means horizontal snapping.snap-y, disabling snapping uses snap-none, and mandatory snapping on all sides is not a valid Tailwind class.snap-start, snap-center, and snap-end to align child elements at the start, center, or end of the snap point.snap-start is correct.<div class="overflow-x-auto snap-x"> <div class="snap-center w-64 h-40 bg-blue-400">Item 1</div> <div class="snap-center w-64 h-40 bg-green-400">Item 2</div> <div class="snap-center w-64 h-40 bg-red-400">Item 3</div> </div>What will happen when you scroll horizontally inside the container?
overflow-x-auto (horizontal scroll), snap-x (horizontal mandatory snap).snap-center, so snapping aligns each item centered in the viewport.<div class="overflow-y-auto snap-x"> <div class="snap-start h-40 bg-yellow-300">Item A</div> <div class="snap-start h-40 bg-yellow-500">Item B</div> </div>What is the main issue preventing scroll snapping?
overflow-y-auto) but uses snap-x which is for horizontal snapping.snap-x with vertical scrolling (overflow-y-auto). -> Option Doverflow-y-auto and vertical snap with snap-y.snap-y snap-mandatory is correct.snap-start.overflow-y-auto snap-y snap-mandatory, Children: snap-start [OK]Pass the test and this pattern moves unknown