CSS Scroll Snap Module Level 2

Editor’s Draft,

More details about this document
This version:
https://drafts.csswg.org/css-scroll-snap-2/
Implementation Report:
https://wpt.fyi/results/css/css-scroll-snap
Issue Tracking:
CSSWG Issues Repository
Editors:
Matt Rakow (Microsoft)
Jacob Rossi (Microsoft)
Tab Atkins-Bittner (Google)
Elika J. Etemad / fantasai (Invited Expert)
Adam Argyle (Google)
Suggest an Edit for this Spec:
GitHub Editor

Abstract

This module contains features to control panning and scrolling behavior with “snap positions”.

CSS is a language for describing the rendering of structured documents (such as HTML and XML) on screen, on paper, 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.

Please send feedback by filing issues in GitHub (preferred), including the spec code “css-scroll-snap” in the title, like this: “[css-scroll-snap] …summary of comment…”. All issues and comments are archived. Alternately, feedback can be sent to the (archived) public mailing list www-style@w3.org.

This document is governed by the 2 November 2021 W3C Process Document.

A test suite and an implementation report will be produced during the CR period.

1. Introduction

This section is not normative.

This is currently a draft spec over Scroll Snap 1.

Scroll experiences don’t always start at the beginning. Interactions with carousels, swipe controls, and listviews often start somewhere in the middle, and each require Javascript to set this position on page load. By enabling CSS to specify this scroll start position, both users, page authors and browsers benefit.

In addition to setting an initial scroll position, developers need insights and events into Scroll Snap. Events like which element is snapped on which axis, when the snap event is changing, when snap completes and conveniences for snapping to children programatically.

1.1. First Layout

This event should follow the Animation code path. When animation objects are created and fire events, this is when a box has it’s first layout.

2. Motivating Examples

A carousel that starts in the middle:
.carousel {
    overflow-inline: auto;
    scroll-start: center;
}
<div class="carousel">
    <img src="img1.jpg">
    <img src="img2.jpg">
    <img src="img3.jpg">
    <img src="img4.jpg">
    <img src="img5.jpg">
</div>
A search bar is available when the user scrolls back to the top:
.scrollport {
    overflow-block: auto;
}

main {
    scroll-start-target: auto;
}
<div class="scrollport">
    <nav>
        ...
    </nav>
    <main>
        ...
    </main>
</div>

3. Setting Where Scroll Starts

3.1. The scroll-start property

Name: scroll-start
Value: [ auto | start | end | center | left | right | top | bottom | <length-percentage> ]{1,2}
Initial: see individual properties
Applies to: see individual properties
Inherited: see individual properties
Percentages: see individual properties
Computed value: see individual properties
Animation type: see individual properties
Canonical order: per grammar

This property is a shorthand property that sets all of the scroll-start-* longhands in one declaration. The first value defines the scroll starting point in the block axis, the second sets it in the inline axis. If the second value is omitted, it defaults to start. If scroll-start-target is set on any child, scroll-start is not used, in favor of using the element as the offset.

Values are defined as follows:

auto
...
<length-percentage>
...

Negative values are invalid. Values corresponding to a length greater than the width/height of the scrollport are valid, but clamped to the width/height of the scrollport.

start
center
end
Equivalent to 0%, 50%, and 100%, respectively.

3.1.1. Interaction with display: none and initial creation

Same behavior that animations follow with first layout.

3.1.2. Slow page loading or document streaming behavior

TODO

3.1.3. Interaction with fragment navigation

TODO If the scrollport has a in-page :target via a URL fragment or a previous scroll position, then scroll-start is unused. Existing target logic should go unchanged. This makes scroll-start a soft request in the scroll position resolution routines.

3.1.4. Interaction with place-content

TODO Side note: While place-content can make a scroller appear to start in the center or end, no browser supports it and it appears complicated to resolve.

3.1.5. Interaction with "find in page"

TODO

3.1.6. Interaction scroll-snap containers

This effectively will layout and start scroll at the snapped child, thus negating / cancelling scroll-start. scroll-start will only work if nothing else has effected the scroll position.

3.1.7. Nested scrollers with scroll-start

Should follow patterns that scroll snap has set.

3.1.8. Interaction when display is toggled

Same behavior that animations follow with first layout.

3.1.9. Interaction with RTL and LTR

Logical properties are offered for length offsets that should be flow relative. Furthermore, the end and start keywords are always logical.

3.2. The scroll-start-target property

Name: scroll-start-target
Value: [ none | auto ]
Initial: see individual properties
Applies to: see individual properties
Inherited: see individual properties
Percentages: see individual properties
Computed value: see individual properties
Animation type: see individual properties
Canonical order: per grammar

This property is a shorthand property that sets all of the scroll-start-target-* longhands in one declaration. The first value defines the scroll starting point in the block axis, the second sets it in the inline axis. If the second value is omitted, it defaults to none.

Values are defined as follows:

none
Element is not a scroll-start-target.
auto
Element is used to calculate the scroll-start position, taking into account scroll-padding or scroll-margin , same as a scroll-snap target.

4. Styling Snapped Items

4.1. The Snapped-element Pseudo-class: :snapped

The :snapped pseudo-class matches any scroll snap targets, regardless of axis. The longform physical and logical pseudo-class selectors allow for more finite snapped children styling as they can target an individual axis.

More specific options are defined as follows:

:snapped-x
Matches the child snapped on the horizontal axis.
:snapped-y
Matches the child snapped on the vertical axis.
:snapped-inline
Matches the child snapped on the inline axis.
:snapped-block
Matches the child snapped on the block axis.

Note: Issue #6985
Need to figure out resolution of the initial frame.

5. Snap Events

5.1. snapChanged and snapChanging

CSS scroll snap points are often used as a mechanism to create scroll interactive "selection" components, where selection is determined with javascript intersection observers and a scroll end guestimate. By creating a built-in event, the invisible state will become actionable, at the right time, and always correct.

Event handler Event handler event type
snapChanged scroll
snapChanging scroll

Appendix A: Longhands

The physical and logical longhands (and their shorthands) interact as defined in [CSS-LOGICAL-1].

Physical Longhands for scroll-start

Name: scroll-start-x, scroll-start-y
Value: auto | start | end | center | <length-percentage>
Initial: auto
Applies to: scroll containers
Inherited: no
Percentages: relative to the corresponding axis of the scroll container’s scrollport
Computed value: the keyword auto or a computed <length-percentage> value
Canonical order: per grammar
Animation type: by computed value type

...

Flow-relative Longhands for scroll-start

Name: scroll-start-inline, scroll-start-block
Value: auto | start | end | center | <length-percentage>
Initial: auto
Applies to: scroll containers
Inherited: no
Percentages: relative to the corresponding axis of the scroll container’s scrollport
Computed value: the keyword auto or a computed <length-percentage> value
Canonical order: per grammar
Animation type: by computed value type

...

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.

Advisements are normative sections styled to evoke special attention and are set apart from other normative text with <strong class="advisement">, like this: UAs MUST provide an accessible alternative.

Tests

Tests relating to the content of this specification may be documented in “Tests” blocks like this one. Any such block is non-normative.


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.

Implementations of Unstable and Proprietary Features

To avoid clashes with future stable CSS features, the CSSWG recommends following best practices for the implementation of unstable features and proprietary extensions to CSS.

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.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[CSS-DISPLAY-3]
Tab Atkins Jr.; Elika Etemad. CSS Display Module Level 3. 3 September 2021. CR. URL: https://www.w3.org/TR/css-display-3/
[CSS-LOGICAL-1]
Rossen Atanassov; Elika Etemad. CSS Logical Properties and Values Level 1. 27 August 2018. WD. URL: https://www.w3.org/TR/css-logical-1/
[CSS-OVERFLOW-3]
David Baron; Elika Etemad; Florian Rivoal. CSS Overflow Module Level 3. 23 December 2021. WD. URL: https://www.w3.org/TR/css-overflow-3/
[CSS-SHAPES-2]
CSS Shapes Module Level 2 URL: https://drafts.csswg.org/css-shapes-2/
[CSS-VALUES-4]
Tab Atkins Jr.; Elika Etemad. CSS Values and Units Module Level 4. 16 December 2021. WD. URL: https://www.w3.org/TR/css-values-4/
[CSSOM-VIEW-1]
Simon Pieters. CSSOM View Module. 17 March 2016. WD. URL: https://www.w3.org/TR/cssom-view-1/
[RFC2119]
S. Bradner. Key words for use in RFCs to Indicate Requirement Levels. March 1997. Best Current Practice. URL: https://datatracker.ietf.org/doc/html/rfc2119
[SELECTORS-4]
Elika Etemad; Tab Atkins Jr.. Selectors Level 4. 21 November 2018. WD. URL: https://www.w3.org/TR/selectors-4/

Property Index

Name Value Initial Applies to Inh. %ages Anim­ation type Canonical order Com­puted value
scroll-start [ auto | start | end | center | left | right | top | bottom | <length-percentage> ]{1,2} see individual properties see individual properties see individual properties see individual properties see individual properties per grammar see individual properties
scroll-start-block auto | start | end | center | <length-percentage> auto scroll containers no relative to the corresponding axis of the scroll container’s scrollport by computed value type per grammar the keyword auto or a computed <length-percentage> value
scroll-start-inline auto | start | end | center | <length-percentage> auto scroll containers no relative to the corresponding axis of the scroll container’s scrollport by computed value type per grammar the keyword auto or a computed <length-percentage> value
scroll-start-target [ none | auto ] see individual properties see individual properties see individual properties see individual properties see individual properties per grammar see individual properties
scroll-start-x auto | start | end | center | <length-percentage> auto scroll containers no relative to the corresponding axis of the scroll container’s scrollport by computed value type per grammar the keyword auto or a computed <length-percentage> value
scroll-start-y auto | start | end | center | <length-percentage> auto scroll containers no relative to the corresponding axis of the scroll container’s scrollport by computed value type per grammar the keyword auto or a computed <length-percentage> value