Today, users of the web are often distracted by content moving around due to changes that occur outside the viewport. Examples include script inserting an iframe containing an ad, or non-sized images loading on a slow network.
Historically the browser’s default behavior has been to preserve the absolute scroll position when such changes occur. This means that to avoid shifting content, the webpage can attempt to reserve space on the page for anything that will load later. In practice, few websites do this consistently.
Scroll anchoring aims to minimize surprising content shifts. It does this by adjusting the scroll position to compensate for the changes outside the viewport.
The explainer document gives an informal overview of scroll anchoring.
1.1. Value Definitions
This specification follows the CSS property definition conventions from [CSS2] using the value definition syntax from [CSS-VALUES-3]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Combination with other CSS modules may expand the definitions of these value types.
In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly.
Scroll anchoring attempts to keep the user’s view of the document stable across layout changes. It works by selecting a DOM node (the anchor node) whose movement is used to determine adjustments to the scroll position.
However, if the scroll container is currently snapped to an element, (see [CSS-SCROLL-SNAP-1]) scroll anchoring is limited to adjustments that would be allowed by re-snapping.
2.1. Anchor Node Selection
Each scrolling box aims to select an anchor node that is deep in the DOM and either should be prioritized as an important DOM node or is close to the block start edge of its optimal viewing region.
Note: If the user agent does not support the scroll-padding property, the optimal viewing region of the scrolling box is equivalent to its content area.
An anchor node can be any box except one for a non-atomic inline. The anchor node is always a descendant of the scrolling box. In some cases, a scrolling box may not select any anchor node.
An element C is a viable candidate for becoming a scroll anchor for a scrolling box S if it meets all of the following criteria:
C is an element that is not a non-atomic inline.
C is a descendant of S
C is not in an excluded subtree
None of the ancestors of C up to S are in an excluded subtree
Some elements are considered to be priority candidates for anchor selection:
The DOM anchor of the focused area of the document, if such an anchor is text editable (editable, editing host, mutable
textarea, or mutable
inputwith a type that allows text entry).
An element containing the current active match of the find-in-page user-agent algorithm. If the match spans multiple elements, then consider only the first such element.
Note that if the priority candidate is a non-atomic inline element, then instead consider its nearest ancestor element that is not a non-atomic inline element as the priority candidate.
Otherwise, for each DOM child N of the element or document associated with S, perform the candidate examination algorithm for N in S, and terminate if it selects an anchor node.
If N is fully visible in S, select N as the anchor node.
If N is partially visible:
For each DOM child C of N, perform the candidate examination algorithm for C in S, and terminate if it selects an anchor node.
For each absolutely-positioned element A whose containing block is N, but whose DOM parent is not N, perform the candidate examination algorithm for A in S, and terminate if it selects an anchor node.
Select N as the anchor node. (If this step is reached, no suitable anchor node was found among N’s descendants.)
Note: Deeper nodes are preferred to minimize the possibility of content changing inside the anchor node but outside the viewport, which would cause visible content to shift without triggering any scroll anchoring adjustment.
Conceptually, a new anchor node is computed for every scrolling box whenever the scroll position of any scrolling box changes. (As a performance optimization, the implementation may wait until the anchor node is needed before computing it.)
2.2. Scroll Adjustment
If an anchor node was selected,
then when the anchor node moves,
the browser computes the previous offset
y0, and the current offset
of the block start edge of the anchor node’s scroll anchoring bounding rect,
relative to the block start edge of the scrolling content in the block flow direction of the scroller.
It then queues an adjustment to the scroll position of
y1 - y0,
in the block flow direction,
to be performed at the end of the suppression window.
The scroll adjustment is a type of [[cssom-view-1#scrolling-events#scrolling]] as defined by [CSSOM-VIEW], and generates scroll events in the manner described there.
2.2.1. Suppression Window
Every movement of an anchor node occurs within a window of time called the suppression window, defined as follows:
The suppression window begins at the start of the current iteration of the HTML Processing Model event loop, or at the end of the most recently completed suppression window, whichever is more recent.
The suppression window ends at the end of the current iteration of the HTML Processing Model event loop, or immediately before the next operation whose result or side effects would differ as a result of a change in the scroll position (for example, an invocation of
getBoundingClientRect()), whichever comes sooner.
Note: The suppression window boundaries should be incorporated into the HTML standard once the scroll anchoring API is stabilized.
More than one anchor node movement may occur within the same suppression window.
At the end of a suppression window, the user agent performs all scroll adjustments that were queued during the window and not suppressed by any suppression trigger during the window.
2.2.2. Suppression Triggers
A suppression trigger is an operation that suppresses the scroll anchoring adjustment for an anchor node movement, if it occurs within the suppression window for that movement. These triggers are:
Any change to the computed value of any of the following properties, on any element in the path from the anchor node to the scrollable element (or document), inclusive of both:
Any change to the computed value of the position property on any element within the scrollable element (or document), such that the element becomes or stops being absolutely positioned. Note that this trigger applies regardless of whether the modified element is on the path from the anchor node to the scrollable element.
The scroll offset of the scrollable element being zero.
Note: Suppression triggers exist for compatibility with existing web content that has negative interactions with scroll anchoring due to shifting content in scroll event handlers.
3. Exclusion API
Scroll anchoring aims to be the default mode of behavior when launched, so that users benefit from it even on legacy content. overflow-anchor can disable scroll anchoring in part or all of a webpage (opt out), or exclude portions of the DOM from the anchor node selection algorithm.
|Value:||auto | none|
|Applies to:||all elements|
|Computed value:||specified keyword|
|Canonical order:||per grammar|
Values are defined as follows:
Declares that the element is potentially eligible to participate in the anchor node selection algorithm for any scrolling box created by the element or an ancestor.
Declares that the element and its descendants (that aren’t nested inside of another scrolling element) are not eligible to participate in the anchor node selection algorithm for any scrolling box created by the element or an ancestor.
Note: It is not possible to turn scroll anchoring "back on" for descendants of a overflow-anchor: none element. However, descendant scroll containers automatically "turn it back on" (for their own scrolling box) unless they explicitly have overflow-anchor: none set on them as well.
overflow-anchor property was also proposed (with different values)
for CSS Sticky Scrollbars,
which has now been superseded.
4. Privacy and Security Considerations
This specification, as it only adjusts how we compute scroll positions, introduces no new privacy or security considerations.