An Introduction to CSS Scroll Snap Points

The CSS Scroll Snap Module is a web customary that gives us some control over scrolling on a web page so that we can make users corkscrew to certain tools of a page rather than to usually anywhere on it.

Scrolling is one of a many achieved actions on a website. Browsers, over a years, have improved their scrolling performance to compare users’ flexible finger strength. And, developers have used scrolling creatively to grasp a improved or an out-of-the-box user experience.

However, when it comes to a association between coding and scrolling, usually JavaScript seemed to have any volume of control over a latter. This was so for a prolonged duration of time, though with a introduction of corkscrew snap points, CSS began to locate up.

Scrolling but corkscrew snap points

Typically, we don’t corkscrew really slow, generally on phones. The faster we scroll, a reduction control we have over where on a shade you’ll finish up when we stopped scrolling.

Imagine we are scrolling by an array of product picture on a website, or a gallery of photos, or online slides. What you’d cite in such applications is to see a whole product, photo, or slide each time we scroll. Not usually a part of a product image, photo, or slide.

For example, on a demo next we can see whenever a user stops scrolling, usually about half of a picture is visible during a bottom of a screen. However, many users would cite to see a final picture fully.

Scrolling but CSS snap pointsScrolling but CSS snap points

Scrolling with corkscrew snap points

This is where we move in CSS corkscrew snap points. The name is self-explanatory; it’s a CSS customary that allows us to snap equipment into place when scrolling.

There are five CSS properties that consecrate this standard:

  1. scroll-snap-type
  2. scroll-snap-points-x
  3. scroll-snap-points-y
  4. scroll-snap-coordinate
  5. scroll-snap-destination
Browser support

The properties need -webkit and -ms prefixes for a applicable browsers. As of essay this article, CSS corkscrew snap is not upheld in Chrome and Opera.

Note that a final 4 properties are expected to be forsaken by user agents in a nearby future. Instead, new properties, namely scroll-snap-align, scroll-snap-margin, and scroll-snap-padding, might be created, as tangible in this specification.

However, they will have a identical purpose as a former properties. Currently, a former set of properties will work usually fine.

Properties

You need to add a scroll-snap-type skill to a corkscrew container (the enclosure component whose children are superfluous while it’s scrolled). It specifies a strictness of a snap action. It can take 3 values:

  1. mandatory – when a scrolling is finished, a scrolling will snap during a applicable snap point
  2. proximity – reduction despotic than mandatory; it will depend on a settlement of a UA possibly a component will snap during a given snap point
  3. none – no gnawing is done

The scroll-snap-points-x and scroll-snap-points-y properties belong to a corkscrew container, too. They impute to points on a x- and y-axis where a snap points will exist. Their value is given by a repeat() function. For instance, if we wish to supplement snap points along a x-axis during a interlude of 100px we need to use a scroll-snap-points-x: repeat(100px) rule.

The scroll-snap-destination skill is also combined to a corkscrew container. It defines a coordinate on a container where a snap end lies. It is during this snap end where a container’s children will snap into place when scrolled.

You can use a scroll-snap-coordinate skill in and with scroll-snap-destination. You need to supplement it to a container’s child elements. It defines a coordinates of child elements, that will align with a end coordinates of their corkscrew enclosure when a user scrolls a screen.

Note we don’t have to use all a properties during once. Only scroll-snap-type is compulsory. Along with that, we can possibly conclude particular snap points or use a destination-coordinate combination.

Code example

Here is an instance formula dash for a typical corkscrew container, with scrolling in straight instruction some images inside. It outputs a demo we can find in a commencement of this post.

div
  img src='Pizza.png' alt='pizza'
  img src='Noodle.png' alt='noodle'
  img src='Burger.png' alt='burger'
  img src='Juice.png' alt='juice'
/div
div {
  width: 300px;
  height: 300px;
  overflow: auto;
  ...
}
div  img {
  width: 250px;
  height: 150px;
  ...
}

Now, we add some snap points to a corkscrew container:

div {
  width: 300px;
  overflow: auto;
  scroll-snap-points-y: repeat(150px);
  scroll-snap-type: mandatory;
}

Below, we can see how a outlay looks like with CSS snap points added. Notice whenever a scrolling stops while a bottom picture is usually partially visible, a full picture appears after a scrollport snaps into a gnawing indicate above it.

Scrolling but CSS snap pointsScrolling but CSS snap points

Add Comment