A CSS scroll snap area is the target region aligned within a CSS snapport during CSS scroll snapping. The specification constructs it from the target's transformed CSS border box: find its rectangular bounding box, aligned to the scroll container's coordinate axes, then apply the outsets supplied by CSS scroll margin.[ref]
CSS Scroll Snap Module Level 1 by World Wide Web Consortium. §5.1, scroll-margin and snap-area construction
Consider an untransformed element whose border box occupiesin content coordinates measured in CSS pixels. Set
scroll-margin: 10px 20px 30px 40px;, in top/right/bottom/left order. The snap area becomesIt is pixels wide and high, even though the original border box remains by .For a candidate start alignment, suppose the CSS snapport begins at viewport coordinates and is large enough to contain this area. Use a horizontal left-to-right writing mode and assume adequate reachable scroll ranges. The required offsets satisfygiving . At that position, the element's actual border box appears at ; the extra room comes from aligning the larger snap area, not moving its borders relative to its content.[ref]
CSS Scroll Snap Module Level 1 by World Wide Web Consortium. §5.2, start alignment of snap area within snapport
A CSS transform can change the initial bounding rectangle. The snap area is nevertheless axis-aligned in the scroll container's coordinate space, even if the transformed border is rotated. Its rectangular shape is therefore an alignment construction, not necessarily the painted outline of the element.
CSS scroll margin also differs from an ordinary layout margin: the example's outsets enlarge the snapping region without assigning extra space between neighboring boxes in normal layout. Reachability and visibility still constrain valid snap positions. If an exact alignment would require scrolling beyond the available area, the specification uses the reachable position as far toward that alignment as possible.[ref]
CSS Scroll Snap Module Level 1 by World Wide Web Consortium. §5.1, outsets; §§5.2.1 and 5.2.3, valid visibility and unreachable positions
CSS scroll margin supplies outsets around an element's transformed border bounds to define its CSS scroll snap area. The
scroll-margin shorthand assigns top, right, bottom and left values using the usual one-to-four-value ordering. In the pinned specification these values are lengths, rather than percentages.[ref]CSS Scroll Snap Module Level 1 by World Wide Web Consortium. §5.1, scroll-margin syntax, ordering and area construction
For an own example, let an untransformed CSS border box occupy in content coordinates measured in CSS pixels. Set
scroll-margin: 20px 10px;. The first value applies at top and bottom, and the second at left and right, giving the snap areaThe box itself is still by pixels; the alignment region is by .Suppose a reachable horizontal start alignment should place this area's left edge at coordinate within the CSS snapport. With a left-to-right horizontal convention, the candidate offset satisfies , giving . Without the left outset, it would instead be . The extra target-side room changes the alignment computation without changing where the border is laid out in the content.
The construction uses the axis-aligned bounding box of the transformed border box before adding the outsets. A rotated CSS transform can therefore affect the base rectangle, even though the resulting snap area remains rectangular in the scroll container's coordinates.[ref]
CSS Scroll Snap Module Level 1 by World Wide Web Consortium. §5.1, transformed bounds and axis alignment
CSS scroll margin and ordinary layout margins serve different purposes. The former does not assign this extra space between neighboring elements in normal layout. It is also distinct from CSS scroll padding, which adjusts the viewing/alignment region on the container side.
Articles by others on the same topic
There are currently no matching articles.