Scroll snap and scroll padding
Definition and purpose
Scroll snapping gives a scroll container preferred resting positions relative to its children. scroll-snap-type defines the axis and strictness, while scroll-snap-align defines how a child aligns within the snapport.
Define vertical snap targets
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #b { width: 200px; height: 100px; overflow-y: auto; } #b > div { height: 100px; } #b { scroll-snap-type: y proximity; } #b > div { scroll-snap-align: start; } </style> </head> <body> <div id="b" tabindex="0" aria-label="Scrollable samples"> <div id="a">First</div> <div id="t">Second</div> <div>Third</div> </div> </body></html>The second item can align its start with the scrollport start.
Syntax and mental model
Use a constrained scroll container with overflow. y proximity allows discretion near targets; y mandatory requires a snap position at the end of scrolling when one is available. scroll-padding reduces the optimal viewing region without adding physical padding; scroll-margin expands the target snap area. These properties also affect scroll-into-view operations. Prefer proximity for variable-height reading content; test keyboard access, large items and reduced motion. A snap position may be clamped at the actual scroll boundary.
Reserve a viewing inset
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #b { width: 200px; height: 100px; overflow-y: auto; } #b > div { height: 100px; } #b { scroll-snap-type: y mandatory; scroll-padding-top: 20px; } #b > div { scroll-snap-align: start; } </style> </head> <body> <div id="b" tabindex="0" aria-label="Scrollable samples"> <div id="a">First</div> <div id="t">Second</div> <div>Third</div> </div> </body></html>At the second item, the nominal scroll offset is 80px (100 minus 20), not 100px. The physical padding remains zero.
Common mistakes
Do not use scroll-padding as layout padding. Avoid mandatory snap on sparse targets if it prevents access to content between them. Test actual scrolling rather than just checking computed declarations.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Top layer and transformed ancestors, Adaptive layout project, Animation and transition precedence. Next lesson: Overscroll and scroll anchoring.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Scroll snap and scroll padding — knowledge check
Five questions on scroll snap and scroll padding.
Where is scroll-snap-type set?
Which property aligns individual targets?
What does scroll-padding change?
Why consider proximity for reading content?
Second target starts at 100px with 20px top scroll-padding. Nominal start snap offset?