CSS propertyInteraction and more
scroll-snap-type
Makes a scrolling container stop on chosen elements.
- Initial value
none- Inherited
- No
- Animatable
- no
- Applies to
- scroll containers
- JavaScript
element.style.scrollSnapType = "x mandatory"
Example
#CSS
.carousel {
display: flex;
overflow-x: auto;
scroll-snap-type: x mandatory;
}
.carousel > * {
flex: 0 0 80%;
scroll-snap-align: center;
}Example HTML
<div style="display: flex; gap: 10px; overflow-x: auto; scroll-snap-type: x mandatory; padding-bottom: 6px;">
<div class="box" style="flex: 0 0 80%; height: 70px; scroll-snap-align: center;">Module 1</div>
<div class="box" style="flex: 0 0 80%; height: 70px; scroll-snap-align: center;">Module 2</div>
<div class="box" style="flex: 0 0 80%; height: 70px; scroll-snap-align: center;">Module 3</div>
</div>Preview
Definition and usage
#The scroll-snap-type property enables scroll snapping in a container. After you release the finger or wheel, scrolling โpullsโ to the nearest snap point instead of stopping at a random spot.
You give an axis (x, y, or both) and a strictness: mandatory always snaps to a point, while proximity snaps only when scrolling ended close to one.
You set the snap points on the children with the scroll-snap-align property. This is how carousels and galleries are built without JavaScript.
Syntax
#Syntax
scroll-snap-type: none | [ x | y | block | inline | both ] [ mandatory | proximity ]?;Values
#- Scroll sideways: the container stops anywhere.
- Scroll sideways: it always snaps to the start of a card.
- It snaps only when you stop close to a card.
Values in practice
#none
Scroll sideways: the container stops anywhere.
.demo { scroll-snap-type: none; }x mandatory
Scroll sideways: it always snaps to the start of a card.
.demo { scroll-snap-type: x mandatory; }x proximity
It snaps only when you stop close to a card.
.demo { scroll-snap-type: x proximity; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- On phones, carousels with snapping are more comfortable than ones driven by arrows.
- With
mandatory, make sure items are not taller than the container, otherwise part of the content will be hard to reach.
Related entries
#Practise it in lessons
#Spotted a mistake or missing an example? Write to us.