scroll-snap-type CSS property sets how strictly snap points are enforced on the scroll container in case there is one.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

Specifying any precise animations or physics used to enforce those snap points is not covered by this property but instead left up to the user agent.

/* Keyword values */
scroll-snap-type: none;
scroll-snap-type: x;
scroll-snap-type: y;
scroll-snap-type: block;
scroll-snap-type: inline;
scroll-snap-type: both;
/* Optional mandatory | proximity*/
scroll-snap-type: x mandatory;
scroll-snap-type: y proximity;
scroll-snap-type: both mandatory;
/* etc */
/* Global values */
scroll-snap-type: inherit;
scroll-snap-type: initial;
scroll-snap-type: unset;
					

句法

none
When the visual viewport of this scroll container is scrolled, it must ignore snap points.
x

The scroll container snaps to snap positions in its horizontal axis only.

y

The scroll container snaps to snap positions in its vertical axis only.

block

The scroll container snaps to snap positions in its block axis only.

inline

The scroll container snaps to snap positions in its inline axis only.

both

The scroll container snaps to snap positions in both of its axes independently (potentially snapping to different elements in each axis).

mandatory

The visual viewport of this scroll container will rest on a snap point if it isn't currently scrolled. That means it snaps on that point when the scroll action finished, if possible. If content is added, moved, deleted or resized the scroll offset will be adjusted to maintain the resting on that snap point.

proximity

The visual viewport of this scroll container may come to rest on a snap point if it isn't currently scrolled considering the user agent's scroll parameters. If content is added, moved, deleted or resized the scroll offset may be adjusted to maintain the resting on that snap point.

形式定义

初始值 none
适用于 所有元素
继承 no
计算值 如指定
动画类型 discrete

形式句法

none | [ x | y | block | inline | both ] [ mandatory | proximity ]?
					

范例

Snapping in different axes

HTML

<div class="holster">
<div class="container x mandatory-scroll-snapping" dir="ltr">
  <div>X Mand. LTR</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>
<div class="container x proximity-scroll-snapping" dir="ltr">
  <div>X Prox. LTR</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>
<div class="container y mandatory-scroll-snapping" dir="ltr">
  <div>Y Mand. LTR</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>
<div class="container y proximity-scroll-snapping" dir="ltr">
  <div>Y Prox. LTR</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>
<div class="container x mandatory-scroll-snapping" dir="rtl">
  <div>X Mand. RTL</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>
<div class="container x proximity-scroll-snapping" dir="rtl">
  <div>X Prox. RTL</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>
<div class="container y mandatory-scroll-snapping" dir="rtl">
  <div>Y Mand. RTL</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>
<div class="container y proximity-scroll-snapping" dir="rtl">
  <div>Y Prox. RTL</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>
</div>
					

CSS

/* setup */
html, body, .holster {
  height: 100%;
}
.holster {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-flow: column nowrap;
  font-family: monospace;
}
.container {
  display: flex;
  overflow: auto;
  outline: 1px dashed lightgray;
  flex: none;
}
.container.x {
  width: 100%;
  height: 128px;
  flex-flow: row nowrap;
}
.container.y {
  width: 256px;
  height: 256px;
  flex-flow: column nowrap;
}
/* scroll-snap */
.x.mandatory-scroll-snapping {
  scroll-snap-type: x mandatory;
}
.y.mandatory-scroll-snapping {
  scroll-snap-type: y mandatory;
}
.x.proximity-scroll-snapping {
  scroll-snap-type: x proximity;
}
.y.proximity-scroll-snapping {
  scroll-snap-type: y proximity;
}
.container > div {
  text-align: center;
  scroll-snap-align: center;
  flex: none;
}
.x.container > div {
  line-height: 128px;
  font-size: 64px;
  width: 100%;
  height: 128px;
}
.y.container > div {
  line-height: 256px;
  font-size: 128px;
  width: 256px;
  height: 100%;
}
/* appearance fixes */
.y.container > div:first-child {
  line-height: 1.3;
  font-size: 64px;
}
/* coloration */
.container > div:nth-child(even) {
  background-color: #87EA87;
}
.container > div:nth-child(odd) {
  background-color: #87CCEA;
}
					

Results

规范

规范 状态 注释
CSS Scroll Snap Module Level 1
The definition of 'scroll-snap-type' in that specification.
候选推荐 初始定义

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
scroll-snap-type Chrome 完整支持 69 Edge 完整支持 12 Prefixed 注意事项
完整支持 12 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -ms-
注意事项 Edge supports an earlier draft of CSS Scroll Snap without axis values.
Firefox 完整支持 68
完整支持 68
不支持 39 — 68 注意事项
注意事项 An earlier draft of CSS Scroll Snap without axis values.
IE 完整支持 10 Prefixed 注意事项
完整支持 10 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -ms-
注意事项 Internet Explorer supports an earlier draft of CSS Scroll Snap without axis values.
Opera 完整支持 56 Safari 完整支持 11
完整支持 11
完整支持 9 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Older Safari versions support an earlier draft of CSS Scroll Snap without axis values.
WebView Android 完整支持 69 Chrome Android 完整支持 69 Firefox Android 完整支持 68
完整支持 68
不支持 39 — 68 注意事项
注意事项 An earlier draft of CSS Scroll Snap without axis values.
Opera Android 完整支持 48 Safari iOS 完整支持 11
完整支持 11
完整支持 9 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -webkit-
注意事项 Older Safari versions support an earlier draft of CSS Scroll Snap without axis values.
Samsung Internet Android 完整支持 10.0

图例

完整支持

完整支持

见实现注意事项。

见实现注意事项。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

元数据

  • 最后修改: