scroll-snap-type

Makes a scrolling container stop on chosen elements.

Initial value
none
Inherited
No
Animatable
no
Applies to
scroll containers
JavaScript
element.style.scrollSnapType = "x mandatory"
On this page

Example

#
CSS
.carousel {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
}

.carousel > * {
  flex: 0 0 80%;
  scroll-snap-align: center;
}
Preview

Definition and usage

#

The scroll-snap-type property enables scroll snapping in a container. After you release the finger or wheel, scrolling โ€œpullsโ€ to the nearest snap point instead of stopping at a random spot.

You give an axis (x, y, or both) and a strictness: mandatory always snaps to a point, while proximity snaps only when scrolling ended close to one.

You set the snap points on the children with the scroll-snap-align property. This is how carousels and galleries are built without JavaScript.

Syntax

#
Syntax
scroll-snap-type: none | [ x | y | block | inline | both ] [ mandatory | proximity ]?;

Values

#
  • none

    Scroll sideways: the container stops anywhere.
  • x mandatory

    Scroll sideways: it always snaps to the start of a card.
  • x proximity

    It snaps only when you stop close to a card.

Values in practice

#

none

Scroll sideways: the container stops anywhere.
.demo { scroll-snap-type: none; }

x mandatory

Scroll sideways: it always snaps to the start of a card.
.demo { scroll-snap-type: x mandatory; }

x proximity

It snaps only when you stop close to a card.
.demo { scroll-snap-type: x proximity; }

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • On phones, carousels with snapping are more comfortable than ones driven by arrows.
  • With mandatory, make sure items are not taller than the container, otherwise part of the content will be hard to reach.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.