{"id":281999,"date":"2019-02-04T08:15:29","date_gmt":"2019-02-04T15:15:29","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=281999"},"modified":"2019-05-29T07:31:37","modified_gmt":"2019-05-29T14:31:37","slug":"scroll-snap-type","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/s\/scroll-snap-type\/","title":{"rendered":"scroll-snap-type"},"content":{"rendered":"
The This is useful if you want to stop the browser at specific points on the page. For example: A user browsing a photo gallery probably doesn’t want to scroll halfway past an image — they’d likely prefer to have the image “snap” to the right position as they scroll. Scroll snapping gives developers a pure-CSS way to handle this behavior.<\/p>\n <\/p>\n See the Pen scroll-snap-type example This browser support data is from Caniuse<\/a>, which has more detail. A number indicates that browser supports the feature at that version and up.<\/p><\/div>scroll-snap-type<\/code> property is part of the CSS Scroll Snap Module<\/a>. Scroll snapping refers to "locking" the position of the viewport to specific elements on the page as the window (or a scrollable container) is scrolled. Think of it like putting a magnet on top of an element that sticks to the top of the viewport and forces the page to stop scrolling right there.<\/p>\n
scroll-snap-type<\/code> is a required property on any scrollable container you want to add scroll snapping to. It answers three questions for a scroll container:<\/p>\n
\n
.scroll-container {\r\n \/* Always force (mandatory) scrolling to a snap point on the y-axis *\/\r\n scroll-snap-type: y mandatory;\r\n}<\/code><\/pre>\n
Syntax<\/h3>\n
scroll-snap-type: none | [ x | y | block | inline | both ] [ mandatory | proximity ]<\/code><\/pre>\n
Values<\/h3>\n
scroll-snap-type<\/code> accepts the following values:<\/p>\n
\n
none<\/code> disables scroll snapping.<\/li>\n
x<\/code> enables scroll snapping along the x-axis only.<\/li>\n
y<\/code> enables scroll snapping along the y-axis only.<\/li>\n
block<\/code> enables scroll snapping along the block-axis only.<\/li>\n
inline<\/code> enables scroll snapping along the inline-axis only.<\/li>\n
both<\/code> enables scroll snapping along both axes (which you can think of as
x<\/code> and<\/em>
y<\/code>, or
inline<\/code> and<\/em>
block<\/code>.<\/li>\n
mandatory<\/code> is a strictness value which tells the browser to always<\/em> go to a snap position when there isn’t scrolling happening.<\/li>\n
proximity<\/code> is a strictness value which tells the browser to go to a snap position if a scrolling action gets pretty close<\/em> to a snap position. If it’s not pretty close,<\/em> then the browser shouldn’t snap and scrolling will behave normally.<\/li>\n<\/ul>\n
Example<\/h3>\n
\n<\/a> by CSS-Tricks (@css-tricks<\/a>) on CodePen<\/a>.<\/p>\nBrowser Support<\/h3>\n
Desktop<\/h4>