CSS scroll snap

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

The CSS scroll snap module provides properties that let you control the panning and scrolling behavior by defining snap positions. Content can be snapped into position as the user scrolls overflowing content within a {{Glossary("scroll container")}}, providing paging and scroll positioning.

This module includes the scroll container scroll-padding properties to adjust the optimal viewing region of paging during scroll-into-view operations. It also includes scroll-margin and scroll-alignment, set on the scroll container's children, to adjust the children's visual area when that child is scrolled into view, as well as a property to force scrolling to stop on individual children.

Scroll snap in action

To view scroll snapping in the box below, scroll up-and-down and left-and-right through the grid of 45 numbered boxes in the scrollable viewport. Click "Play" in the example below to view or edit the source in the MDN Playground:

js hidden live-sample___scroll_snap
html hidden live-sample___scroll_snap
css hidden live-sample___scroll_snap

{{EmbedLiveSample("scroll_snap", "", "250px")}}

With scroll snap, one of the numbered boxes that you scroll to will snap into place. The initial CSS makes the numbered box snap into the center of the viewport. Use the sliders to change the block and inline snap positions.

Using snap properties, you can allow or block the scrolling past an element, a numbered box in this case. Select the "Prevent scrolling past boxes" checkbox to force all scrolling actions to be limited to scrolling to an adjacent box.

To compare scroll snapping to regular scrolling, check the "disable snapping" checkbox and try scrolling again.

Reference

Properties on containers

  • {{cssxref("scroll-snap-type")}}
  • {{cssxref("scroll-padding")}}
  • {{cssxref("scroll-padding-top")}}
  • {{cssxref("scroll-padding-right")}}
  • {{cssxref("scroll-padding-bottom")}}
  • {{cssxref("scroll-padding-left")}}
  • {{cssxref("scroll-padding-inline")}}
  • {{cssxref("scroll-padding-inline-start")}}
  • {{cssxref("scroll-padding-inline-end")}}
  • {{cssxref("scroll-padding-block")}}
  • {{cssxref("scroll-padding-block-start")}}
  • {{cssxref("scroll-padding-block-end")}}

Properties on children

  • {{cssxref("scroll-initial-target")}}
  • {{cssxref("scroll-margin")}}
  • {{cssxref("scroll-margin-top")}}
  • {{cssxref("scroll-margin-right")}}
  • {{cssxref("scroll-margin-bottom")}}
  • {{cssxref("scroll-margin-left")}}
  • {{cssxref("scroll-margin-inline")}}
  • {{cssxref("scroll-margin-inline-start")}}
  • {{cssxref("scroll-margin-inline-end")}}
  • {{cssxref("scroll-margin-block")}}
  • {{cssxref("scroll-margin-block-start")}}
  • {{cssxref("scroll-margin-block-end")}}
  • {{cssxref("scroll-snap-align")}}
  • {{cssxref("scroll-snap-stop")}}

Events

  • {{domxref("Element/scrollsnapchange_event", "scrollsnapchange")}} {{experimental_inline}}
  • {{domxref("Element/scrollsnapchanging_event", "scrollsnapchanging")}} {{experimental_inline}}

Interfaces

  • {{domxref("SnapEvent")}} {{experimental_inline}}
  • {{domxref("SnapEvent.snapTargetBlock")}} {{experimental_inline}}
  • {{domxref("SnapEvent.snapTargetInline")}} {{experimental_inline}}

Guides

  • Basic concepts of CSS scroll snap
  • : An overview and examples of CSS scroll snap features.
  • Using scroll snap events
  • : A guide to using the {{domxref("Element/scrollsnapchanging_event", "scrollsnapchanging")}} and {{domxref("Element/scrollsnapchange_event", "scrollsnapchange")}} scroll snap events that are fired when the browser determines a new snap target is pending or selected.

Related concepts

  • {{cssxref(":target")}} pseudo-class
  • {{cssxref("overflow")}} CSS property
  • Element {{domxref("Element.scroll", "scroll()")}} method
  • Element {{domxref("Element.scrollBy", "scrollBy()")}} method
  • Element {{domxref("Element.scrollIntoView", "scrollIntoView()")}} method
  • Element {{domxref("Element.scrollTo", "scrollTo()")}} method
  • Element {{domxref("Element.scroll_event", "scroll")}} event
  • Element {{domxref("Element.scrollend_event", "scrollend")}} event
  • [scrollbar](/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/scrollbar_role) ARIA role
  • {{Glossary("Scroll container")}} glossary term

Specifications

{{Specifications}}

See also

  • CSS overflow module
  • CSS scrollbars styling module
  • CSS scroll anchoring module
  • Keyboard-only scrolling areas on adrianroselli.com (2022)
  • Scroll snap examples on CodePen (2022)
  • Practical CSS scroll snapping/ on CSS-Tricks (2020)
  • CSS scroll snap on 12 Days of Web (2019)
  • Well-controlled scrolling with CSS scroll snap on web.dev (2018)