HTML & CSS · Senior · Question 22 of 75
How do you build a horizontal gallery with slides that "snap" into place without JavaScript?
- ✓Correct answer:
scroll-snap-type: x mandatoryon the track,scroll-snap-alignon slides - 2
position: stickyandleft: 0on every slide inside the track - 3
@keyframeswith an infinitetranslateXanimation on the track
Explanation
CSS Scroll Snap: a container with overflow-x: auto and scroll-snap-type: x mandatory, slides with scroll-snap-align. The browser settles the scroll on the nearest slide and supports touch, wheel and keyboard. scroll-padding accounts for fixed elements. JS is only needed for prev/next buttons and indicators.