CSS propertyInteraction and more
scroll-snap-align
Specifies which edge an element snaps by in a scrolling container.
- Initial value
none- Inherited
- No
- Animatable
- no
- Applies to
- all elements (children of a scroll container)
- JavaScript
element.style.scrollSnapAlign = "center"
Example
#CSS
.sections {
height: 100dvh;
overflow-y: auto;
scroll-snap-type: y mandatory;
}
.sections > section {
scroll-snap-align: start;
}Example HTML
<div style="height: 120px; overflow-y: auto; scroll-snap-type: y mandatory; border-radius: 10px;">
<section class="box" style="height: 120px; scroll-snap-align: start; border-radius: 0;">Section 1: scroll down</section>
<section class="box" style="height: 120px; scroll-snap-align: start; border-radius: 0;">Section 2</section>
<section class="box" style="height: 120px; scroll-snap-align: start; border-radius: 0;">Section 3</section>
</div>Preview
Definition and usage
#The scroll-snap-align property is set on the children of a container with scroll-snap-type. It decides whether, after scrolling, the element aligns its start, centre, or end to the container’s edge.
start suits lists and sections read from the top, center suits photo carousels, and end suits rarer cases such as a chat scrolled from the bottom.
You can change the gap between the container’s edge and the snap point with the scroll-padding property on the container.
Syntax
#Syntax
scroll-snap-align: none | start | center | end;Values
#Values in practice
#start
Scroll: the card aligns to the left edge.
.item { scroll-snap-align: start; }center
Scroll: the card aligns to the centre.
.item { scroll-snap-align: center; }end
Scroll: the card aligns to the right edge.
.item { scroll-snap-align: end; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Combine
scroll-snap-alignwithscroll-paddingon the container if a header covers the top of a snapped section. - Full-screen section snapping can be tiring on long pages. Use it sparingly.
Related entries
#Practise it in lessons
#Spotted a mistake or missing an example? Write to us.