1. Introduction
This section is not normative.
Popular UX paradigms for scrollable content frequently employ paging through content, or sectioning into logical divisions. This is especially true for touch interactions where it is quicker and easier for users to quickly pan through a flatly-arranged breadth of content rather than delving into a heirarchical structure through tap navigation. For example, it is easier for a user to view many photos in a photo album by panning through a photo slideshow view rather than tapping on individual photos in an album.
However, given the imprecise nature of scrolling inputs like touch panning and mousewheel scrolling, it is difficult for web developers to guarantee a well-controlled scrolling experience, in particular creating the effect of paging through content. For instance, it is easy for a user to land at an awkward scroll position which leaves a page partially on-screen when panning.
To this end, we introduce scroll snap positions which enforce the scroll positions that a scroll container’s scrollport may end at after a scrolling operation has completed.
1.1. Module interactions
This module extends the scrolling user interface features defined in [CSS21] section 11.1.
None of the properties in this module apply to the ::first-line and ::first-letter pseudo-elements.
1.2. Values
This specification follows the CSS property definition conventions from [CSS21]. Value types not defined in this specification are defined in CSS Level 2 Revision 1 [CSS21]. Other CSS modules may expand the definitions of these value types: for example [CSS3VAL], when combined with this module, expands the definition of the <length> value type as used in this specification.
2. Motivating Examples
img { /* Specifies that the center of each photo should align with the center of the scroll container in the X axis when snapping */ scroll-snap-align: center none; } .photoGallery { width: 500px; overflow-x: auto; overflow-y: hidden; white-space: nowrap; /* Requires that the scroll position always be at a snap position when the scrolling operation completes. */ scroll-snap-type: mandatory; }
<div class="photoGallery"> <img src="img1.jpg"> <img src="img2.jpg"> <img src="img3.jpg"> <img src="img4.jpg"> <img src="img5.jpg"> </div>
.page { /* Defines the top of each page as the edge that should be used for snapping */ scroll-snap-align: none start; } .docScroller { width: 500px; overflow-x: hidden; overflow-y: auto; /* Specifies that each element’s snap area should align with a 100px offset from the top edge. */ scroll-snap-padding: 100px 0 0; /* Encourages scrolling to end at a snap position when the operation completes, if it is near a snap position */ scroll-snap-type: proximity; }
<div class="docScroller"> <div class="page">Page 1</div> <div class="page">Page 2</div> <div class="page">Page 3</div> <div class="page">Page 4</div> </div>
3. Overview
This module introduces control over scroll snap positions, which are scroll positions that produce particular alignments of content within a scroll container. Using the scroll-snap-type property on the relevant scroll container, the author can request a particular bias for the scrollport to land on a snap position after scrolling operations.
Snap positions are specified as a particular alignment (scroll-snap-align) of an element’s scroll snap area (its border bounding box, as modified by scroll-snap-margin) within the scroll container’s snapport (its scrollport, as reduced by scroll-snap-padding). This is conceptually equivalent to specifying the alignment of an alignment subject within an alignment container. A scroll position that satisfies the specified alignment is a snap position.
The act of adjusting the scroll position of a scroll container’s scrollport such that it is aligned to a snap position is called snapping, and a scroll container is said to be snapped to a snap position if its scrollport’s scroll position is that snap position and there is no active scrolling operation. The CSS Scroll Snap Module intentionally does not specify nor mandate any precise animations or physics used to enforce snap positions; this is left up to the user agent.
Snap positions only affect the nearest ancestor scroll container on the element’s containing block chain.
4. Capturing Scroll Snap Areas: Properties on the scroll container
4.1. Scroll Snapping Rules: the scroll-snap-type property
Name: | scroll-snap-type |
---|---|
Value: | none | [ x | y | block | inline | both ] [ mandatory | proximity ]? ]! | point |
Initial: | none |
Applies to: | all elements |
Inherited: | no |
Percentages: | n/a |
Media: | interactive |
Computed value: | as specified |
Animatable: | no |
The scroll-snap-type property specifies whether a scroll container is a scroll snap container, how strictly it snaps, and which axes are considered.
The strictness values (none, proximity, mandatory) specify how strictly snap positions are enforced on the scroll container (by forcing an adjustment to the scroll position). If no strictness value is specified, proximity is assumed. Values are defined as follows:
Currently considering renaming the mandatory/proximity.
- none
- If specified on a scroll container, the scroll container must not snap.
- mandatory
-
If specified on a scroll container, the scroll container is required to be snapped to a snap position when there are no active scrolling operations. If a reachable snap position exists then the scroll container must snap at the termination of a scroll (if none exist then no snapping occurs).
If the content changes such that the scroll container would no longer be snapped (e.g. content is added, moved, deleted, resized) to the same snap position it was snapped to before the content change, the scroll container must be re-snapped. If the same snap position it was snapped to before the content change still exists (e.g. its associated element was not deleted) and is reachable, the scroll container must be re-snapped to that same snap position after the content change.
- proximity
-
If specified on a scroll container, the scroll container may snap to a snap position at the termination of a scroll, at the discretion of the UA given the parameters of the scroll.
If the content changes such that the scroll container would no longer be snapped (e.g. content is added, moved, deleted, resized) to the same snap position it was snapped to before the content change and that same snap position still exists (e.g. its associated element was not deleted) and is reachable, the scroll container must be re-snapped to that same snap position after the content change.
The text for mandatory and proximity resnapping requirements has some overlap -- try to rewrite this more concisely and without repetition without altering functionality
A box captures snap positions if it is a scroll container or has a value other than none for scroll-snap-type. If a box’s nearest snap-position capturing ancestor on its containing block chain is a scroll container with a non-none value for scroll-snap-type, that is the box’s scroll snap container. Otherwise, the box has no scroll snap container, and its snap positions do not trigger snapping.
The axis values specify what axis(es) are affected by snap positions, and whether snap positions are evaluated independently per axis, or together as a 2D point. Values are defined as follows:
- x
- The scroll container axis-snaps to snap positions in its horizontal axis only.
- y
- The scroll container axis-snaps to snap positions in its vertical axis only.
- block
- The scroll container axis-snaps to snap positions in its block axis only.
- inline
- The scroll container axis-snaps to snap positions in its inline axis only.
- both
- The scroll container axis-snaps to snap positions in both of its axes independently (potentially snapping to different elements in each axis).
- point
- The scroll container point-snaps to snap positions in both axes simultaneously, treating each element’s snap position as a single 2D position (rather than potentially snapping to different elements in each axis).
4.2. Scroll Snapport: the scroll-snap-padding property
Name: | scroll-snap-padding |
---|---|
Value: | [ <length> | <percentage> ]{1,4} |
Initial: | 0 |
Applies to: | scroll containers |
Inherited: | no |
Percentages: | relative to the corresponding dimension of the scroll container’s scrollport |
Media: | interactive |
Computed value: | as specified, with lengths made absolute |
Animatable: | as length, percentage, or calc |
The scroll-snap-padding property defines the scroll snapport-- the area of the scrollport that is used as the alignment container for the scroll snap areas when calculating snap positions. Values are interpreted as for padding, and specify inward offsets from each edge of the scrollport.
body { overflow-x: auto; overflow-y: hidden; scroll-snap-type: mandatory; scroll-snap-padding: 0 500px 0 0; } .toolbar { position: fixed; height: 100%; width: 500px; right: 0; } img { scroll-snap-align: center none; }
This property is a shorthand property that sets all of the scroll-snap-padding-* longhands in one declaration.
5. Aligning Scroll Snap Areas: Properties on the elements
5.1. Scroll Snapping Area: the scroll-snap-margin property
Name: | scroll-snap-margin |
---|---|
Value: | <length>{1,4} |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentages: | n/a |
Media: | interactive |
Computed value: | as specified, with lengths made absolute |
Animatable: | as length |
The scroll-snap-margin property defines the scroll snap area that is used for snapping this box to the snapport. The <length> values give outsets (interpreted as for margin or border-image-outset). The scroll snap area is the rectangular bounding box of the transformed border box, plus the specified outsets, axis-aligned in the scroll container’s coordinate space.
Note: This ensures that the scroll snap area is always rectangular and axis-aligned to the scroll container’s coordinate space.
This property is a shorthand property that sets all of the scroll-snap-margin-* longhands in one declaration.
5.2. Scroll Snapping Alignment: the scroll-snap-align property
Name: | scroll-snap-align |
---|---|
Value: | [ none | start | end | center ]{1,2} |
Initial: | none |
Applies to: | all elements |
Inherited: | no |
Percentages: | n/a |
Media: | interactive |
Computed value: | two keywords |
Animatable: | no |
The scroll-snap-align property specifies the box’s snap position as an alignment of its snap area (as the alignment subject) within its snap container’s snapport (as the alignment container). The two values specify the snapping alignment in the inline axis and block axis, respectively. If only one value is specified, the second value defaults to the same value.
Values are defined as follows:
- none
- This box does not define a snap position in the specified axis.
- start
- Start alignment of this box’s scroll snap area within the scroll container’s snapport is a snap position in the specified axis.
- end
- End alignment of this box’s scroll snap area within the scroll container’s snapport is a snap position in the specified axis.
- center
- Center alignment of this box’s scroll snap area within the scroll container’s snapport is a snap position in the specified axis.
5.2.1. Scoping Valid Snap Positions to Visible Boxes
Since the purpose of scroll snapping is to align content within the viewport for optimal viewing: in all cases, the specified alignment creates a valid snap position only if at least part of the snap area is within the snapport. For example, a snap area is top-aligned to the snapport if its top edge is coincident with the snapport’s top edge; however, this alignment is nonetheless not a valid snap position if the entire snap area is outside the snapport.
Why limit snapping to only when the element is visible?
As the WebKit implementers point out, extending a snap edge infinitely across the canvas only allows for snapping gridded layouts, and produces odd behavior for the user when off-screen elements do not align with on-screen elements. (If this requirement is onerous for implementers however, we can default to a gridded behavior and introduce a switch to get smarter behavior.)5.2.2. Snapping Boxes that Overflow the Scrollport
If the snap area is larger than the snapport in a particular axis, then any scroll position in which the snap area covers the snapport is a valid snap position in that axis. The UA may use the specified alignment as a more precise target for certain scroll operations (e.g. explicit paging).
Since the snap area is larger than the snapport, while the area fully fills the viewport, the container can be scrolled arbitrarily and will not try to snap back to its aligned position. However, if the container is scrolled such that the area no longer fully fills the viewport in an axis, the area resists outward scrolling until it is scrolled sufficiently to trigger snapping to a different snap position.
Deal with case of overlapping snap areas, some of which are smaller and others larger than the viewport.
5.2.3. Unreachable Snap Positions
If a snap position is unreachable as specified, such that aligning to it would require scrolling the scroll container’s viewport past the edge of its scrollable overflow region, the used snap position for this snap area is the position resulting from scrolling as much as possible in each relevant axis toward the desired snap position.
5.3. Scroll Snap Limits: the scroll-snap-stop property
Name: | scroll-snap-stop |
---|---|
Value: | normal | always |
Initial: | normal |
Applies to: | all elements |
Inherited: | no |
Percentages: | n/a |
Media: | interactive |
Computed value: | as specified |
Animatable: | no |
This property specifies whether the scroll container must stop at each snap position it passes, or may pass multiple snap positions before coming to rest. Values are defined as follows:
- normal
- The scroll container may pass by a snap position defined by this element during the execution of a scrolling operation.
- always
- The scroll container must not pass by a snap position defined by this element during the execution of a scrolling operation.
It’s been proposed to rename normal, but not to what.
6. Snapping Mechanics
The precise model algorithm to select a snap position to snap to is intentionally left mostly undefined, so that user agents can take into account sophisticated models of user intention and interaction and adjust how they respond over time, to best serve the user.
This section defines some useful concepts to aid in discussing scroll-snapping mechanics, and provides some guidelines for what an effective scroll-snapping strategy might look like. User agents are encouraged to adapt this guidance and apply their own best judgement when defining their own snapping behavior. It also provides a small number of behavior requirements, to ensure a minimum reasonable behavior that authors can depend on when designing their interfaces with scroll-snapping in mind.
6.1. Types of Scrolling Methods
When a page is scrolled, the action is performed with an intended end position and/or an intended direction. Each combination of these two things defines a distinct category of scrolling, which can be treated slightly differently:
-
intended end position
-
Common examples of scrolls with only an intended end position include:
-
a panning gesture, released without momentum
-
manipulating the scrollbar “thumb” explicitly
-
programmatically scrolling via APIs such as
scrollTo()
-
tabbing through the document’s focusable elements
-
navigating to an anchor within the page
-
-
intended direction and end position
-
Common examples of scrolls with both an intended direction and end position include:
-
a “fling” gesture, interpreted with momentum
-
programmatically scrolling via APIs such as
scrollBy()
The intended end point of the scroll prior to intervention from features such as snap points is its natural end-point.
-
-
intended direction
-
Common examples of scrolls with only an intended direction include:
-
pressing an arrow key on the keyboard
-
a swiping gesture interpreted as a fixed (rather than inertial) scroll
-
Additionally, because page layouts usually align things vertically and/or horizontally, UAs sometimes axis-lock a scroll when its direction is sufficiently vertical or horizontal. An axis-locked scroll is bound to only scroll along that axis. This prevents less-precise input mechanisms from drifting in the non-primary axis.
6.2. Axis vs Point-Snapping
This feature is planned to be removed in the next publication in order to reduce the feature-set of Level 1. It is included here for future reference in defining Level 2.
There are two distinct snapping behaviors that a scroll container might engage in:
-
axis-snapping
-
If a scroll container is axis-snapping, its descendants indicate a desired scroll position in each axis of the scroll container independently, with no dependent preference for what the other axis’s scroll position should be.
Note: This is the “default” type of snap behavior that most scroll containers will want to use, and so the scroll-snap-type property intentionally defaults to it.
Note: An element in an axis-snapping scroll container can declare two snap positions, one in each axis. If one of the element’s snap positions is chosen in one axis, this has no bearing on the other dimension’s snap position—
it might be chosen, or a different element’s snap position might be chosen for that axis, or that axis might not snap at all. -
point-snapping
-
If a scroll container is point-snapping, its descendants indicate a desired scroll position in both axes of the scroll container simultaneously—
in other words, some point in the descendant must be aligned to a corresponding point in the scroll container. This type of snapping behavior is intended for “two-dimensional” panning-type layouts, such as cities on a map (using proximity 2D snap positions to snap a city to the center of the display when it gets close), or a tiled image gallery (using mandatory 2D snap positions to force each image to be centered on the screen). In both of these cases, it would look weird if the horizontal scrolling was aligned to one element while the vertical was aligned to a different element (which is the behavior you’d get if the scroll container was axis-snapping).
6.3. Choosing Snap Positions
A scroll container can have many snap areas scattered throughout its scrollable overflow region. A naïve algorithm for selecting a snap position can produce behavior that is unintuitive for users, so care is required when designing a selection algorithm. Here are a few pointers that can aid in the selection process:
-
Snap positions should be chosen to minimize the distance between the end-point (or the natural end-point) and the final snapped scroll position, subject to the additional constraints listed in this section.
-
Point-snapping is all-or-nothing; if the snap position of an element is chosen to align to, the scroll container must set its scroll position according to the element’s snap positions in both axises; the scroll container must not “partially align” to the element by taking its snap position in one axis and aligning the other axis according to something else.
-
If a scroll is axis-locked and the scroll container is axis-snapping, any snap positions in the other axis should be ignored during the scroll. (However, snap positions in the other axis can still effect the final scroll position.)
If a scroll is axis-locked and the scroll container is point-snapping, snap positions should be penalized in the selection process according to the amount of other-axis scrolling they would cause.
-
In order to prevent a far-offscreen element from having difficult-to-understand effects on the scroll position, snap positions should be ignored if their elements are far outside of the “corridor” that the snapport defines as it moves through the scrollable overlow region, or a hypothetical “corridor” in the direction of a scroll with only an intended direction, or the snapport after an scroll with only an intended end position.
-
User agents must ensure that a user can “escape” a snap position, regardless of the scroll method. For example, if the snap type is mandatory and the next snap position is more than two screen-widths away, a naïve “always snap to nearest” selection algorithm might “trap” the user if their end position was only one screen-width away. Instead, a smarter algorithm that only returned to the starting snap position if the end-point was a fairly small distance from it, and otherwise ignored the starting snap position, would give better behavior.
(This implies that a scroll with only an intended direction must always ignore the starting snap positions.)
-
If a page is navigated to a fragment that defines a target element (one that would be matched by :target, or the target of
scrollIntoView()
), and that element defines some snap positions, the user agent should snap to one of that element’s snap positions. The user agent may do this even when the scroll container has scroll-snap-type: none.
Appendix A: Longhands
Physical Longhands for scroll-snap-padding
Name: | scroll-snap-padding-top, scroll-snap-padding-right, scroll-snap-padding-bottom, scroll-snap-padding-left |
---|---|
Value: | <length> | <percentage> |
Initial: | 0 |
Applies to: | scroll containers |
Inherited: | no |
Percentages: | relative to the scroll container’s scrollport |
Media: | interactive |
Computed value: | as specified, with lengths made absolute |
Animatable: | as length, percentage, or calc |
These longhands of scroll-snap-padding specify the top, right, bottom, and left edges of the snapport, respectively.
Flow-relative Longhands for scroll-snap-padding
Name: | scroll-snap-padding-inline-start, scroll-snap-padding-block-start, scroll-snap-padding-inline-end, scroll-snap-padding-block-end |
---|---|
Value: | <length> | <percentage> |
Initial: | 0 |
Applies to: | scroll containers |
Inherited: | no |
Percentages: | relative to the scroll container’s scrollport |
Media: | interactive |
Computed value: | as specified, with lengths made absolute |
Animatable: | as length, percentage, or calc |
These longhands of scroll-snap-padding specify the block-start, inline-start, block-end, and inline-end edges of the snapport, respectively.
Name: | scroll-snap-padding-block, scroll-snap-padding-inline |
---|---|
Value: | [ <length> | <percentage> ]{1,2} |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentages: | relative to the scroll container’s scrollport |
Media: | interactive |
Computed value: | as specified, with lengths made absolute |
Animatable: | as length, percentage, or calc |
These shorthands of scroll-snap-padding-block-start + scroll-snap-padding-block-end and scroll-snap-padding-inline-start + scroll-snap-padding-inline-end are longhands of scroll-snap-padding, and specify the block-axis and inline-axis edges of the snapport, respectively.
If two values are specified, the first gives the start value and the second gives the end value.
Physical Longhands for scroll-snap-margin
Name: | scroll-snap-margin-top, scroll-snap-margin-right, scroll-snap-margin-bottom, scroll-snap-margin-left |
---|---|
Value: | <length> |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentages: | n/a |
Media: | interactive |
Computed value: | as specified, with lengths made absolute |
Animatable: | as length |
These longhands of scroll-snap-margin specify the top, right, bottom, and left edges of the scroll snap area, respectively.
Flow-relative Longhands for scroll-snap-margin
Name: | scroll-snap-margin-block-start, scroll-snap-margin-inline-start, scroll-snap-margin-block-end, scroll-snap-margin-inline-end |
---|---|
Value: | <length> |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentages: | n/a |
Media: | interactive |
Computed value: | as specified, with lengths made absolute |
Animatable: | as length |
These longhands of scroll-snap-margin specify the block-start, inline-start, block-end, and inline-end edges of the scroll snap area, respectively.
Name: | scroll-snap-margin-block, scroll-snap-margin-inline |
---|---|
Value: | <length>{1,2} |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentages: | n/a |
Media: | interactive |
Computed value: | as specified, with lengths made absolute |
Animatable: | as length |
These shorthands of scroll-snap-margin-block-start + scroll-snap-margin-block-end and scroll-snap-margin-inline-start + scroll-snap-margin-inline-end are longhands of scroll-snap-margin, and specify the block-axis and inline-axis edges of the scroll snap area, respectively.
If two values are specified, the first gives the start value and the second gives the end value.
7. Privacy and Security Considerations
This specification does not expose any information whatsoever that is not already exposed to the DOM directly; it just makes scrolling slightly more functional. There are no new privacy or security considerations.
8. Acknowledgements
Many thanks to David Baron, Simon Fraser, Håkon Wium Lie, Edward O’Connor, François Remy, Majid Valpour, and most especially Robert O’Callahan for their proposals and recommendations, which have been incorporated into this document.