1. Introduction
We think scroll snapping is a great idea, and fully support exposing this functionality through CSS. However, a major weakness of the current spec is the way it conceives snapping on a coordinate model rather than a box model. This requires a lot of manual calculations in figuring out the correct coordinates from the box model; and also makes sensible scroll-snap settings dependent on the relative sizes of the viewport and the snappable contents, causing problems for users are unexpectedly large and/or small screens (a problem commonly ignored by many authors).
This proposal builds off of roc’s model, using an area-snapping model to intelligently handle adaptation to multiple screen sizes. It also adds group alignment as a built-in concept, rather than requiring authors to build one in JavaScript.
2. Use Cases
-
Snapping to 0.25rem above the top of each heading
:root { scroll-snap-type: proximity; } h1, h2, h3, h4, h5, h6 { scroll-snap-align: start; scroll-snap-margin: 0.25em; }
-
Snapping to the center of each photo
:root { scroll-snap-type: mandatory; } img { scroll-snap-align: center; }
-
Snapping each flow chart entry to within the viewport when it falls near the edge:
:root { scroll-snap-type: proximity; } li { scroll-snap-align: start; }
-
Snapping each city on a map to the center of the viewport, but only once it gets near the center in both dimensions:
:root { scroll-snap-type: proximity; } .city { scroll-snap-align: center; }
<div class="slides"> <div class="slide">...</div> <div class="slide">...</div> <div class="slide details"> <div class="slide">...</div> <div class="slide">...</div> </div> <div class="slide">...</div> </div> <style> .slides { display: flex; flex-flow: row; scroll-snap-type: mandatory; overflow-x: scroll; width: 100vw; height: 100vh; } .slide { scroll-snap-align: start; width: 100vw; min-height: 100vh; } .slide.details { display: flex; flex-flow: column; scroll-snap-type: mandatory; overflow-y: scroll; } </style>
3. Overview of Change
On the scroll container:
Spec | Proposal | Priority |
---|---|---|
scroll-snap-type: none | mandatory | proximity | scroll-snap-type: none | [ mandatory | proximity ] || [ x | y | block | inline | both | point] | High priority |
''scroll-snap-destination: <position>'' | ''scroll-snap-padding: [ <length> | <percentage> ]{1,4}'' |
On the children:
Spec | Proposal | Priority |
---|---|---|
''scroll-snap-coordinate: <position>#'' | scroll-snap-align: [ none | start | end | center ]{1,2} | High priority |
n/a | ''scroll-snap-margin: <length>{1,4}'' | High priority |
4. Scroll Snapping Model
This module introduces control over scroll snap positions, which are scroll positions that produce particular alignments of content within a scrollable viewport. Using the scroll-snap-type property on the relevant scroll container, the author can request a particular bias for the viewport to land on a valid snap position after scrolling operations.
Snap positions are specified as a particular alignment (scroll-snap-align) of a box’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 valid 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.
5. Capturing Scroll Snap Areas: Properties on the scroll container
5.1. Scroll Snapping Rules: the scroll-snap-type property
Name: | scroll-snap-type |
---|---|
Value: | none | [ proximity | mandatory ] || [ x | y | block | inline | both | point ] |
Initial: | none |
Applies to: | all elements |
Inherited: | no |
Percentages: | n/a |
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.
We’re considering splitting this into subproperties. Current proposed names are scroll-snap for the current grammar, scroll-snap-affinity for the proximity/mandatory distinction, and scroll-snap-axis for the x/y/etc distinction.
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). Values are defined as follows:
- none
-
If specified on a scroll container,
the scroll container must not snap:
all scroll positions are equally valid.
If specified on a non-scroll container, this value has no effect.
- proximity
-
If specified on a scroll container,
the scroll container may snap to a valid snap position at the termination of a scroll,
at the discretion of the UA given the parameters of the scroll.
If specified on a non-scroll container, this value “traps” descendant boxes’ snap positions, preventing them from affecting any ancestor scroll containers.
- mandatory
-
If specified on a scroll container,
the scroll container is required to be snapped to a valid snap position when there are no active scrolling operations.
That is, it must snap to a valid snap position at the termination of a scroll, if any such exist.
(If none exist, then no snapping occurs.)
If specified on a non-scroll container, this value “traps” descendant boxes’ snap positions, preventing them from affecting any ancestor scroll containers.
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.
Authors should use mandatory snap positions with consideration of varyingly-sized screens and (if applicable) varying-sized content. In particular, although access to snapped elements larger than the viewport is handled by the UA, if authors assign mandatory snapping to non-adjacent siblings, content in between can become inaccessible in cases where it is longer than the screen.h1, h2, h3, h4, h5, h6 { scroll-snap-align: start; } /* snap headings - but not section content */
or snapping the section elements.
section { scroll-snap-align: start; } /* snap entire section - including content */
If the author chooses mandatory snapping of the headings, and one section is longer than the viewport, then the reader will have difficulty accessing the content that overflows the screen, because mandatory snapping does not allow the scroll position to rest on the content between the snapped headings.
However, if the author chooses mandatory snapping of the section element (which contains all the content of the section) then the UA can allow the reader to scroll freely through the entire section in the cases where the content is longer than the screen.
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 axises independently (potentially snapping to different elements in each axis).
- point
- The scroll container point-snaps to snap positions in both axises simultaneously, treating each element’s snap position as a single 2D position (rather than potentially snapping to different elements in each axis).
If no axis value is specified, then the axis is automatically computed:
-
If the scroll container is only scrollable in one axis (only one axis has its overflow set to auto or scroll) it axis-snaps in the scrollable axis only.
-
Otherwise, it axis-snaps in its block axis only.
If the content or layout of the scroll container changes
(e.g. content is added, moved, deleted, resized),
the UA must re-evaluate, and potentially re-snap if necessary,
the resulting scroll position
once the positions of the content have restabilized.
This can result in no snapping in some cases
(e.g. if there are no nearby snap positions for a proximity-snapping scroll container).
However, if there was a previously-snapped snap position (associated with the same element)
that still exists after such changes,
the UA must remain snapped to it—
5.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 |
Computed value: | as specified, with lengths made absolute |
Animatable: | as length, percentage, or calc |
The scroll-snap-padding property defines the scroll snapport—
map { overflow: scroll; scroll-snap-type: proximity; scroll-snap-padding: 3em 0 0 0; } toolbar { position: absolute; margin: 0.5em; top: 0; left: 0; right: 0; height: 2em; } city { scroll-snap-align: center; }
This property is a shorthand property that sets all of the scroll-snap-padding-* longhands in one declaration.
6. Aligning Scroll Snap Areas: Properties on the scrolling content
6.1. Scroll Snapping Margin: the scroll-snap-margin property
Name: | scroll-snap-margin |
---|---|
Value: | <length>{1,4} |
Initial: | 0 |
Applies to: | all elements |
Inherited: | no |
Percentages: | n/a |
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 viewport. 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.
6.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 |
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 snapping alignment in the inline axis and block axis, respectively. If only one value is specified, the second value defaults to the same value.
section { scroll-snap-align: start; }
The following example aligns the center of each city to the center of the scroll container’s snapport, snapping only when the city is centered in both axes:
.map { scroll-snap-type: proximity point; } .map .city { scroll-snap-align: center; }
The following example aligns the center of each photo to the center of the scroll container’s snapport, snapping independently in each axis:
.photos { scroll-snap-type: mandatory both; } img { scroll-snap-align: center; }
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 valid snap position in the specified axis.
- end
- End alignment of this box’s scroll snap area within the scroll container’s snapport is a valid snap position in the specified axis.
- center
- Center alignment of this box’s scroll snap area within the scroll container’s snapport is a valid snap position in the specified axis.
If the element’s scroll container is point-snapping, and this property does not specify a valid snap position in both axises (that is, it contains none), the element does not contribute any snap positions at all.
For all of these values, the block or inline axis is relative to the element’s parent’s writing mode.
Is this the correct writing mode to compute against? Or should it be the scroll container’s writing mode?
Why no <length> or <position> values?
The values here represent alignments
(in the sense of align-self and justify-self),
so are consistent with that syntax.
We chose to use this simpler syntax without lengths or percentages
because the scroll-snap-margin concept already provides length offsets—
6.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.)6.2.2. Snapping Boxes that Overflow the Scrollport
If the snap area is larger than the snapport in a particular axis, and there are no other snap areas within the snapport that would provide a snap position aligning the overflowing snap area within the snapport, 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. inertial scrolling or 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 you fling out or pull it sufficiently to trigger snapping to a different snap position.
6.2.3. Unreachable Snap Areas
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 area, 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.
6.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 |
Computed value: | as specified |
Animatable: | no |
This property specifies whether the snap position absorbs all remaining inertia during an inertial scroll, or allows the inertial scroll to pass multiple snap positions before coming to rest. Values are defined as follows:
- normal
- A snap position defined by this element does not interfere with the inertia of an inertial scroll that is passing across it, unless it is the landing snap position.
- always
-
A snap position defined by this element,
when encountered by an inertial scroll,
absorbs all remaining inertia from an inertial scroll,
forcing a stop at this snap position,
exactly as if the scroll had enough inertia to reach the snap position,
but not enough to escape it.
Note: This means that if all snap positions in a scroller have scroll-snap-stop: always, an inertial scroll can only move one snap position per inertial scroll action.
7. 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.
7.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:
-
explicit scrolling
-
A scroll is explicit if it has an intended end position, but no intended direction.
This includes methods such as:
-
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
-
-
inertial scrolling
-
A scroll is inertial if it has both an intended end position and an intended direction.
This includes methods such as:
-
a "fling" gesture, released with momentum (the "intended" end position might be implicitly determined by the UA’s scrolling physics, but the strength of the user’s fling still expresses a weak intention about where the scroll should end up)
-
a mousewheel scroll
-
programmatically scrolling via APIs such as
scrollBy()
The scroll position that an inertial scroll would naturally land on without further intervention is the natural end-point.
-
-
directional scrolling
-
A scroll is directional if it has an intended direction, but no intended end point.
This includes methods such as:
-
pressing an arrow key on the keyboard
-
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, for example, a nearly horizontal fling gesture from gradually drifting up or down as well, because it is very difficult to fling in a precisely horizontal line.
7.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 axises 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).
7.3. Choosing Snap Positions
A scroll container can have many snap areas scattered throughout its scrollable area. A naive 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.
-
Snap positions should be ignored if their elements are far outside of the “corridor” that the snapport defines as it moves through the scrollable area during an inertial scroll, or a hypothetical “corridor” in the direction of a directional scroll, or the snapport after an explicit scroll. (This is to prevent a far-offscreen element from having difficult-to-understand effects on the scroll 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 would “trap” the user if they were panning with a touch gesture; a sufficiently large distance would even trap fling scrolling! 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 directional scroll 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), 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: | n/a |
Computed value: | as specified, with lengths made absolute |
Percentage: | relative to the corresponding dimension of the scroll container’s scrollport |
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-padding-block-end |
---|---|
Value: | <length> | <percentage> |
Initial: | 0 |
Applies to: | scroll containers |
Inherited: | no |
Percentages: | relative to the corresponding dimension of the scroll container’s scrollport |
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 corresponding dimension of the scroll container’s scrollport |
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 |
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 |
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 | 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.
8. 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.
9. Acknowledgements
Many thanks to David Baron, Simon Fraser, Håkon Wium Lie, Edward O’Connor, François Remy, Majid Valpour, potentially some anonymous Microsoft engineers (?), and most especially Robert O’Callahan for their proposals and recommendations, which have been incorporated into this document.