CSS Scroll Snap Points Module Level 1

Editor’s Draft, 5 March 2014

This version:
http://dev.w3.org/csswg/css-snappoints/
Editor’s Draft:
http://dev.w3.org/csswg/css-snappoints/
Feedback:
www-style@w3.org with subject line “[css-snappoints] … message topic …”(archives)
Test Suite:
None Yet
Editors:
Matt Rakow (Microsoft)
Jacob Rossi (Microsoft)
Issue Tracking:
http://wiki.csswg.org/spec/css-snappoints

Abstract

This module contains features to control panning and scrolling behavior with "snap points". CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, in speech, etc.

Status of this document

This is a public copy of the editors’ draft. It is provided for discussion only and may change at any moment. Its publication here does not imply endorsement of its contents by W3C. Don’t cite this document other than as work in progress.

The (archived) public mailing list www-style@w3.org (see instructions) is preferred for discussion of this specification. When sending e-mail, please put the text “css-snappoints” in the subject, preferably like this: “[css-snappoints] …summary of comment…

This document was produced by the CSS Working Group (part of the Style Activity).

This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

Table of Contents

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 offset which leaves a page partially on-screen when panning. To this end, we introduce scroll snap points which enforce the scroll offsets that a scroll container 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

In this example, a series of images arranged in a scroll container are used to build a photo gallery. The scroll container is sized to the same size of the images contained therein. Using mandatory snap points, scrolling will always complete with a snap point aligned to the edge of the scroll container. By aligning a snap point at the edge of each image, this creates a photo viewer were users can scroll through the images one at a time.
img {
    width:500px;
}
.photoGallery {
    width: 500px;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    /* Sets up points to which scrolling
       will snap along x-axis */
    scroll-snap-points-x: repeat(100%);
    /* Requires that scrolling always end at a snap point when
       the operation completes (hard snap) */
    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>
The layout of the scroll container’s contents in the example. Snap points are set along the x-axis, starting at 0px and repeating at intervals of 100% of the scroll container’s width.
This example also builds a photo gallery as in example 1. However, in this example the scroll container is larger than the photos contained within (such that multiple images may be seen simultaneously), and the image sizes vary (such that a repeating snap interval would not be effective). Using mandatory element-based snap points, scrolling will always complete with an image centered in the scroll container.
    img {
        /* Defines the center of each photo as the
           coordinate that should be used for snapping */
        scroll-snap-coordinate: 50% 50%;
    }
    .photoGallery {
        width: 500px;
        overflow-x: auto;
        overflow-y: hidden;
        white-space: nowrap;
        /* Denotes that snap points will be defined
           by the elements contained within. */
        scroll-snap-points-x: elements;
        /* Specifies that each element’s snap coordinate should
           align with the center of the scroll container */
        scroll-snap-destination: 50% 50%;
        /* Requires that scrolling always end at a snap point
           when the operation completes (hard snap). */
        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>
The layout of the scroll container’s contents in the example. The snap-destination is horizontally and vertically centered within the scroll container (represented by a red X), and each element has its snap coordinate horizontally and vertically centered within the element (represented by yellow plus signs).
This example builds a paginated document that aligns each page near to (but not exactly on) the edge of the scroll container. This allows the previous page to "peek" in from above in order to make the user aware that they are not yet at the top of the document. Using proximity snap points instead of mandatory snap points allows the user to stop halfway through a page (rather than forcing them to snap one page at a time). However, if a scrolling operation would finish near a snap point, then the scroll will be adjusted to align the page as specified.
    .page {
        /* Defines the top center of each page as the
           coordinate that should be used for snapping */
        scroll-snap-coordinate: 50% 0;
    }
    .docScroller {
        width: 500px;
        overflow-x: hidden;
        overflow-y: auto;
        /* Denotes that snap points will be defined
           by the elements contained within. */
        scroll-snap-points-y: elements;
        /* Specifies that each element’s snap coordinate should
           align with the center of the scroll container, offset
           a short distance from the top edge. */
        scroll-snap-destination: 50% 100px;
         /* Encourages scrolling to end at a snap point when the
            operation completes, if it is near a snap point */
        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>
The layout of the scroll container’s contents in the example. The snap-destination is horizontally centered and offset 100px from the top edge with respect to the scroll container (represented by a red X), and each element has its snap coordinate horizontally centered and top-aligned with respect to the element (represented by yellow plus signs).

3 Definitions

scroll container
An element which provides a scrolling user interface as described in [CSS21], particularly in the section on overflow.

4 Scroll Snap Types: the scroll-snap-type property

The scroll-snap-type property is used to define the physics characteristics of panning and scrolling, if any. It defines how and when snap points are enforced on the scroll container it is applied to.
Name:scroll-snap-type
Value:none | mandatory | proximity
Initial:none
Applies to:scroll containers
Inherited:no
Media:interactive
Computed value:specified value
Percentages:n/a
Animatable:no
none
This scroll container must ignore snap points, if any, when scrolled.
mandatory
This scroll container must come to rest on a snap point at the termination of a scroll, if possible.
proximity
This scroll container may come to rest on a snap point at the termination of a scroll at the discretion of the UA given the parameters of the scroll.

Could this be defined on a per-snap-point basis? What does it mean to mix types of snap points within a single scroller? Alternatively, should this be definable separately for each axis?

Need to consider the "snap-area" behavior described by fantasai. Maybe define this as repelling snap...lines? guardrails? bumpers? boundaries? limits? at the boundary?

If we do some behavior like snap-area, what happens when near an edge -- try to align with the edge or not?

What happens in zooming scenarios, e.g. if the images become larger than the viewport? Snapping makes less sense in this case. Is this addressable with a snap-area type of effect?

Consider naming conventions like in Grid Layout for grouping properties on the container vs. items.

Specify behavior of mandatory in a broader range of scenarios that induce scrolling or change content -- e.g. element is removed or moved. Does the scroller scroll to the nearest valid snap point?

5 Scroll Snap Points: the scroll-snap-points-x and scroll-snap-points-y properties

The scroll-snap-points-x and scroll-snap-points-y properties are used to define the positioning of snap points within the content of the scroll container they are applied to, or to declare the scroll container as snapping to elements.
Name:scroll-snap-points-x, scroll-snap-points-y
Value:[ <length># ,]? repeat(<length>) | <length># | elements
Initial:repeat(100%)
Applies to:scroll containers
Inherited:no
Media:interactive
Computed value:specified value, with lengths made absolute
Percentages:relative to same axis of the padding-box of the scroll container
Animatable:no
[ <length># ,]? repeat(<length>)
Defines a list of starting snap points and an interval at which additional snap points are defined, starting from the final snap point of the list. The first <length># list is the set of explicitly defined snap points, and the repeat(<length>) function defines the interval to repeat thereafter.
<length>+
Defines one or more discrete snap points for the scroll container, measured as lengths from the container’s relevant start edge.
elements
Declares that the snap points for this scroll container will be determined by the elements contained within. In particular, by the combination of the element boxes' positions in layout and their corresponding scroll-snap-coordinate properties, in conjunction with this element’s scroll-snap-destination property.

With the split-axis approach, the list and interval can result in unexpected behavior in 2d scrollers. For example, if you only want two diagonally-positioned snap points at (x1, y1) and (x2, y2), you will instead get (x1, y1), (x1, y2), (x2, y1), and (x2, y2).

Is the "elements" value needed? Is it important to avoid mixing list/interval with element snap points?

If we do stick with a value like "elements", is there a better name for it (e.g. if we can snap to non-elements like fragments).

Express as position instead of length unit?

Is the list format required, if we have element snapping? If yes, need a scenario justification. If no, remove.

6 Scroll Snap Destination: the scroll-snap-destination property

The scroll-snap-destination property is used to define the x and y coordinate within the scroll container which element snap points will align with when the elements value is used for the scroll-snap-points-* property.
Name:scroll-snap-destination
Value:<length>{2}
Initial:0px
Applies to:scroll containers
Inherited:no
Media:interactive
Computed value:specified value, with lengths made absolute
Percentages:relative to width and height of the padding-box of the scroll container
Animatable:yes
<length>{2}
Specifies the offset of the snap destination from the leading edge of the scroll container. The first value gives the x coordinate of the snap destination, the second value its y coordinate.

Clarify "leading edge" -- want this to respond appropriately to writing modes

7 Scroll Snap Coordinate: the scroll-snap-coordinate property

The scroll-snap-coordinate property is used to define the x and y coordinate within the element which will align with the nearest ancestor scroll container’s snap-destination when the elements value is used for the scroll-snap-points-* property of the nearest ancestor scroll container’s respective axis.

How does this work with fragmentation?

Consider alternative naming besides "coordinate".

Name:scroll-snap-coordinate
Value:none | <length>{2}#
Initial:none
Applies to:all elements
Inherited:no
Media:interactive
Computed value:specified value, with lengths made absolute
Percentages:refer to the element box used by the box-sizing property
Animatable:yes
none
Specifies that this element does not contribute a snap point.
<length>{2}#
Specifies the offset of the snap coordinates from the leading edge of the element box used by the box-sizing property. For each pairing, the first value gives the x coordinate of the snap coordinate, the second value its y coordinate.

Don’t tie this to box-sizing.

Specify interaction with transforms.

8 CSSOM

Need to define how to read back points.

How to navigate directly to a snap point (e.g. with a hash or similar) -- e.g. enable direct linking to a specific page in a document

Supplementary APIs (e.g. snap-to-next, snap-to-previous, jump-to-N)

Acknowledgments

Many thanks to lots of people for their proposals and recommendations, some of which are incorporated into this document.

Conformance

Document conventions

Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.

All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]

Examples in this specification are introduced with the words "for example" or are set apart from the normative text with class="example", like this:

This is an example of an informative example.

Informative notes begin with the word "Note" and are set apart from the normative text with class="note", like this:

Note, this is an informative note.

Conformance classes

Conformance to this specification is defined for three conformance classes:

style sheet
A CSS style sheet.
renderer
A UA that interprets the semantics of a style sheet and renders documents that use them.
authoring tool
A UA that writes a style sheet.

A style sheet is conformant to this specification if all of its statements that use syntax defined in this module are valid according to the generic CSS grammar and the individual grammars of each feature defined in this module.

A renderer is conformant to this specification if, in addition to interpreting the style sheet as defined by the appropriate specifications, it supports all the features defined by this specification by parsing them correctly and rendering the document accordingly. However, the inability of a UA to correctly render a document due to limitations of the device does not make the UA non-conformant. (For example, a UA is not required to render color on a monochrome monitor.)

An authoring tool is conformant to this specification if it writes style sheets that are syntactically correct according to the generic CSS grammar and the individual grammars of each feature in this module, and meet all other conformance requirements of style sheets as described in this module.

Partial implementations

So that authors can exploit the forward-compatible parsing rules to assign fallback values, CSS renderers must treat as invalid (and ignore as appropriate) any at-rules, properties, property values, keywords, and other syntactic constructs for which they have no usable level of support. In particular, user agents must not selectively ignore unsupported component values and honor supported values in a single multi-value property declaration: if any value is considered invalid (as unsupported values must be), CSS requires that the entire declaration be ignored.

Experimental implementations

To avoid clashes with future CSS features, the CSS2.1 specification reserves a prefixed syntax for proprietary and experimental extensions to CSS.

Prior to a specification reaching the Candidate Recommendation stage in the W3C process, all implementations of a CSS feature are considered experimental. The CSS Working Group recommends that implementations use a vendor-prefixed syntax for such features, including those in W3C Working Drafts. This avoids incompatibilities with future changes in the draft.

Non-experimental implementations

Once a specification reaches the Candidate Recommendation stage, non-experimental implementations are possible, and implementors should release an unprefixed implementation of any CR-level feature they can demonstrate to be correctly implemented according to spec.

To establish and maintain the interoperability of CSS across implementations, the CSS Working Group requests that non-experimental CSS renderers submit an implementation report (and, if necessary, the testcases used for that implementation report) to the W3C before releasing an unprefixed implementation of any CSS features. Testcases submitted to W3C are subject to review and correction by the CSS Working Group.

Further information on submitting testcases and implementation reports can be found from on the CSS Working Group’s website at http://www.w3.org/Style/CSS/Test/. Questions should be directed to the public-css-testsuite@w3.org mailing list.

References

Normative References

[CSS21]
Bert Bos; et al. Cascading Style Sheets Level 2 Revision 1 (CSS 2.1) Specification. 7 June 2011. W3C Recommendation. URL: http://www.w3.org/TR/2011/REC-CSS2-20110607
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. URL: http://www.ietf.org/rfc/rfc2119.txt

Informative References

[CSS3VAL]
Håkon Wium Lie; Tab Atkins; Elika J. Etemad. CSS Values and Units Module Level 3. 30 July 2013. W3C Candidate Recommendation. (Work in progress.) URL: http://www.w3.org/TR/2013/CR-css3-values-20130730/

Index

Property index

NameValueInitialApplies toInh.%agesMediaAnimatableComputed value
scroll-snap-typenone | mandatory | proximitynonescroll containersnon/ainteractivenospecified value
scroll-snap-points-x[ <length># ,]? repeat(<length>) | <length># | elementsrepeat(100%)scroll containersnorelative to same axis of the padding-box of the scroll containerinteractivenospecified value, with lengths made absolute
scroll-snap-points-y[ <length># ,]? repeat(<length>) | <length># | elementsrepeat(100%)scroll containersnorelative to same axis of the padding-box of the scroll containerinteractivenospecified value, with lengths made absolute
scroll-snap-destination<length>{2}0pxscroll containersnorelative to width and height of the padding-box of the scroll containerinteractiveyesspecified value, with lengths made absolute
scroll-snap-coordinatenone | <length>{2}#noneall elementsnorefer to the element box used by the box-sizing propertyinteractiveyesspecified value, with lengths made absolute

Issues Index

Could this be defined on a per-snap-point basis? What does it mean to mix types of snap points within a single scroller? Alternatively, should this be definable separately for each axis?
Need to consider the "snap-area" behavior described by fantasai. Maybe define this as repelling snap...lines? guardrails? bumpers? boundaries? limits? at the boundary?
If we do some behavior like snap-area, what happens when near an edge -- try to align with the edge or not?
What happens in zooming scenarios, e.g. if the images become larger than the viewport? Snapping makes less sense in this case. Is this addressable with a snap-area type of effect?
Consider naming conventions like in Grid Layout for grouping properties on the container vs. items.
Specify behavior of mandatory in a broader range of scenarios that induce scrolling or change content -- e.g. element is removed or moved. Does the scroller scroll to the nearest valid snap point?
With the split-axis approach, the list and interval can result in unexpected behavior in 2d scrollers. For example, if you only want two diagonally-positioned snap points at (x1, y1) and (x2, y2), you will instead get (x1, y1), (x1, y2), (x2, y1), and (x2, y2).
Is the "elements" value needed? Is it important to avoid mixing list/interval with element snap points?
If we do stick with a value like "elements", is there a better name for it (e.g. if we can snap to non-elements like fragments).
Express as position instead of length unit?
Is the list format required, if we have element snapping? If yes, need a scenario justification. If no, remove.
Clarify "leading edge" -- want this to respond appropriately to writing modes
How does this work with fragmentation?
Consider alternative naming besides "coordinate".
Don’t tie this to box-sizing.
Specify interaction with transforms.
Need to define how to read back points.
How to navigate directly to a snap point (e.g. with a hash or similar) -- e.g. enable direct linking to a specific page in a document
Supplementary APIs (e.g. snap-to-next, snap-to-previous, jump-to-N)