-
Notifications
You must be signed in to change notification settings - Fork 790
Expand file tree
/
Copy pathOverview.bs
More file actions
1119 lines (947 loc) · 44 KB
/
Overview.bs
File metadata and controls
1119 lines (947 loc) · 44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
8000
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class='metadata'>
Title: CSS Scroll Snapping Change Proposal
Shortname: css-scroll-snap
Level: 1
Status: UD
Work Status: exploring
Group: CSSWG
ED: https://drafts.csswg.org/css-scroll-snap/
Editor: Tab Atkins-Bittner, Google, http://xanthir.com/contact/
Editor: Elika J. Etemad / fantasai, Invited Expert, http://fantasai.inkedblade.net/contact
Abstract: A brief description of an alternate model for scroll-snapping.
Ignored Terms: snap position, snap positions, inertial scroll, inertial scrolls, semantic scroll, semantic scrolls, scrollable area, scroll-group-align, containing block chain
At Risk: ''point'' value of 'scroll-snap-type'
</pre>
<pre class="link-defaults">
spec: css-shapes-1; type: value; for: <shape-box>
text: border-box
text: margin-box
spec: css-writing-modes-3; type: dfn
text: start
text: end
</pre>
Introduction {#intro}
=====================
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.
Use Cases {#use-cases}
======================
<div class="example">
Use Case 1: Snapping to the start or middle of each box
e.g. address book (start) or photo album (middle)
1. Snapping to 0.25rem above the top of each heading
<pre class="lang-css">
:root { scroll-snap-type: proximity; }
h1, h2, h3, h4, h5, h6 {
scroll-snap-align: start;
scroll-snap-margin: 0.25em;
}
</pre>
2. Snapping to the center of each photo
<pre class="lang-css">
:root { scroll-snap-type: mandatory; }
img { scroll-snap-align: center; }
</pre>
</div>
<div class="example">
Use Case 2: Snapping to boxes (or points) in 2D
e.g. on a map, where you want to snap points of interest to the
center, or a flow-chart diagram, where you want to snap the edges
of each box into the visible area. In both cases, you don't want
objects wholly outside the visible area to influence snapping.
1. Snapping each flow chart entry to within the viewport when it falls near the edge:
<pre class="lang-css">
:root {
scroll-snap-type: proximity;
}
li {
scroll-snap-align: edges;
}
</pre>
2. Snapping each city on a map to the center of the viewport,
but only once it gets near the center in both dimensions:
<pre class="lang-css">
:root {
scroll-snap-type: proximity;
}
.city {
scroll-snap-align: center;
}
</pre>
</div>
<div class="example">
Use Case 3: Slideshow, where successive slides are arranged horizontally,
and sometimes "detail" slides are placed below the "main" slide for that point.
<pre class="lang-html">
<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: edges;
width: 100vw;
min-height: 100vh;
}
.slide.details {
display: flex;
flex-flow: column;
scroll-snap-type: mandatory;
overflow-y: scroll;
}
</style>
</pre>
</div>
Overview of Change {#proposal}
==============================
On the scroll container:
<table class=data>
<thead>
<tr>
<th>Spec
<th>Proposal
<th>Priority
<tbody>
<tr>
<td>''scroll-snap-type: none | mandatory | proximity''
<td>''scroll-snap-type: none | [ mandatory | proximity ] || [ x | y | block | inline | both | point]''
<td>High priority
<tr>
<td>''scroll-snap-destination: <<position>>''
<td>''scroll-snap-padding: [ <<length>> | <<percentage>> ]{1,4}''
<td>
</table>
On the children:
<table class=data>
<thead>
<tr>
<th>Spec
<th>Proposal
<th>Priority
<tbody>
<tr>
<td>''scroll-snap-coordinate: <<position>>#''
<td>''scroll-snap-align: [ none | start | end | center | edges ]{1,2}''
<td>High priority
<tr>
<td>n/a
<td>''scroll-snap-margin: <<length>>{1,4}''
<td>High priority
</table>
Scroll Snapping Model {#snap-model}
=====================
This module introduces control over <dfn lt="scroll snap position" local-lt="snap position">scroll snap positions</dfn>,
which are scroll positions that produce particular alignments
of content within a scrollable viewport.
Using the 'scroll-snap-type' propoperty on the relevant <a>scroll container</a>,
the author can request a particular bias
for the viewport to land on a valid <a>snap position</a>
during scrolling operations.
Valid <a>snap positions</a> can be specified
as a particular alignment ('scroll-snap-align')
of an element's <a>scroll snap area</a> ('scroll-snap-margin', defaulting to its border box)
within the <a>scroll container</a>’s <a>snap viewport</a>
(its viewport, as reduced by 'scroll-snap-padding').
This is conceptually equivalent to specifying the alignment of
an <a>alignment subject</a> within an <a>alignment container</a>.
The scroll position that satisfies the specified alignment
is a valid <a>snap position</a>.
<a>Snap positions</a> must only affect the nearest ancestor
(on the element's <a>containing block chain</a>)
<a>scroll container</a>.
Scroll Snapping Container {#snap-container}
=========================
<!--
████████ ██ ██ ████████ ████████
██ ██ ██ ██ ██ ██
██ ████ ██ ██ ██
██ ██ ████████ ██████
██ ██ ██ ██
██ ██ ██ ██
██ ██ ██ ████████
-->
Scroll Snapping Rules: the 'scroll-snap-type' property {#snap-type}
----------------------
<pre class="propdef">
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
Media: interactive
</pre>
The 'scroll-snap-type' property is used to specify
whether and how strictly <a>snap positions</a> are intepreted on the <a>scroll container</a>,
and which axes are considered.
Issue: <a href="https://lists.w3.org/Archives/Public/www-style/2015Nov/0328.html">We're considering splitting this into subproperties.</a>
Current proposed names are <css>scroll-snap</css> for the current grammar,
<css>scroll-snap-affinity</css> for the proximity/mandatory distinction,
and <css>scroll-snap-axis</css> for the x/y/etc distinction.
The strictness values (''scroll-snap-type/none'', ''proximity'', ''mandatory'') specify
how strictly <a>snap positions</a> are enforced on the <a>scroll container</a>
(by forcing an adjustment to the scroll offset).
It intentionally does not specify nor mandate
any precise animations or physics used to enforce those <a>snap positions</a>;
this is left up to the user agent.
Values are defined as follows:
<dl dfn-type=value dfn-for=scroll-snap-type>
<dt><dfn>none</dfn>
<dd>
If specified on a <a>scroll container</a>,
the visual viewport of this <a>scroll container</a>
must ignore <a>snap positions</a>, if any, when scrolled:
all scroll positions are equally valid.
If specified on a non-<a>scroll container</a>,
this value has no effect.
<dt><dfn>proximity</dfn>
<dd>
If specified on a <a>scroll container</a>,
the visual viewport of this <a>scroll container</a>
may come to rest at a <a>snap position</a> at the termination of a scroll
at the discretion of the UA given the parameters of the scroll.
If specified on a non-<a>scroll container</a>,
this value “traps” descendant boxes’ <a>snap positions</a>,
preventing them from affecting any ancestor <a>scroll containers</a>.
If the content of the <a>scroll container</a> changes
(e.g. content is added, moved, deleted, resized),
the UA must re-snap as if the user explicitly scrolled to the current scroll offset
once the positions of the content and any active scrolling actions have restabilized.
(This can result in no snapping,
if there are no nearby <a>snap positions</a>.)
In addition, if the previously-snapped <a>snap position</a>
(associated with the same element)
still exists after such changes,
the UA must re-snap to it
(even if the changes would place a different <a>snap position</a>
closer to the current scroll offset).
Issue: Should the element-follow behavior be MUST or SHOULD here?
<dt><dfn>mandatory</dfn>
<dd>
If specified on a <a>scroll container</a>,
the visual viewport of this <a>scroll container</a>
is guaranteed to rest at a <a>snap position</a>
when there are no active scrolling operations.
That is, it must come to rest at a <a>snap position</a>
at the termination of a scroll, if any exist.
(If none exist, then no snapping occurs.)
If specified on a non-<a>scroll container</a>,
this value “traps” descendant boxes’ <a>snap positions</a>,
preventing them from affecting any ancestor <a>scroll containers</a>.
If the content changes such that the visual viewport
would no longer rest at a <a>snap position</a>
(e.g. content is added, moved, deleted, resized),
the scroll offset must be modified to maintain this guarantee,
once the positions of the content and any active scrolling actions have restabilized.
In addition, if the previously-snapped <a>snap position</a>
(associated with the same element)
still exists after such changes,
the UA must re-snap to it.
</dl>
Advisement:
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 <a href="#snap-overflow">handled by the UA</a>,
if authors assign mandatory snapping to non-adjacent siblings,
content in between can become inaccessible
in cases where it is longer than the screen.
<div class="example">
For example, if an author wishes to force snapping to the top of each section heading,
s/he could accomplish this in two ways: snapping the headings
<pre>h1, h2, h3, h4, h5, h6 { scroll-snap-align: start; } /* snap headings - but not section content */</pre>
or snapping the section elements.
<pre>section { scroll-snap-align: start; } /* snap entire section - including content */</pre>
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.
</div>
The axis values specifies what axis(es) are affected by <a>snap positions</a>,
and whether <a>snap positions</a> are evaluated independently per axis,
or together as a 2D point.
Values are defined as follows:
<dl dfn-type=value dfn-for="scroll-snap-type">
<dt><dfn>x</dfn>
<dd>
The <a>scroll container</a> <a>axis-snaps</a> to <a>snap positions</a> in its horizontal axis only.
<dt><dfn>y</dfn>
<dd>
The <a>scroll container</a> <a>axis-snaps</a> to <a>snap positions</a> in its vertical axis only.
<dt><dfn>block</dfn>
<dd>
The <a>scroll container</a> <a>axis-snaps</a> to <a>snap positions</a> in its block axis only.
<dt><dfn>inline</dfn>
<dd>
The <a>scroll container</a> <a>axis-snaps</a> to <a>snap positions</a> in its inline axis only.
<dt><dfn>both</dfn>
<dd>
The <a>scroll container</a> <a>axis-snaps</a> to <a>snap positions</a> in both of its axises independently
(potentially snapping to different elements in each axis).
<dt><dfn>point</dfn>
<dd>
The <a>scroll container</a> <a>point-snaps</a> to <a>snap positions</a> in both axises simultaneously,
treating each element’s <a>snap position</a> as a single 2D position
(rather than potentially snapping to different elements in each axis).
</dl>
If no axis value is specified, then the axis is automatically computed:
* If the <a>scroll container</a> is only scrollable in one axis
(only one axis has its 'overflow' set to ''overflow/auto'' or ''overflow/scroll'')
it <a>axis-snaps</a> in the scrollable axis only.
* Otherwise, it <a>axis-snaps</a> in its <a>block axis</a> only.
<!--
████████ ███ ████████ ████████ ████ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██
████████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████
██ █████████ ██ ██ ██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██
██ ██ ██ ████████ ████████ ████ ██ ██ ██████
-->
Scroll Snapping Window: the 'scroll-snap-padding' property {#snap-padding}
-----------------------
<pre class="propdef">
Name: scroll-snap-padding
Value: [ <<length>> | <<percentage>> ]{1,4}
Initial: 0
Applies to: <a>scroll containers</a>
Inherited: no
Percentages: relative to the corresponding axis of the element's visual
D4C
viewport
Computed value: as specified
Animatable: see individual properties
Media: interactive
</pre>
The 'scroll-snap-padding' property defines the <dfn local-lt="snap viewport">scroll snap viewport</dfn>--
the area of the viewport that is used as the <a>alignment container</a>
when aligning a <a>scroll snap area</a> to a <a>snap position</a>.
Values are interpreted as for 'padding',
and specify inward offsets from each side of the viewport.
<div class="example">
In this example, points of interest in a map are centered
within the portion of the viewport that does not include the toolbar overlay.
<pre>
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;
}
</pre>
</div>
This property is a <a>shorthand property</a> that sets
all of the <a href="#longhands"><css>scroll-snap-padding-*</css> longhands</a>
in one declaration.
Element-based Snapping {#element}
======================
<!--
███ ████████ ████████ ███
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ████████ ██████ ██ ██
█████████ ██ ██ ██ █████████
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ████████ ██ ██
-->
Scroll Snapping Area: the 'scroll-snap-margin' property {#scroll-snap-areas}
---------------------
<pre class="propdef">
Name: scroll-snap-margin
Value: <<length>>{1,4}
Initial: 0
Applies to: all elements
Inherited: no
Computed value: as specified, with lengths made absolute
Animatable: see individual properties
Media: interactive
</pre>
Specifies the <dfn lt="scroll snap area" local-lt="snap area">scroll snap area</dfn>
that is used for snapping this box to the viewport.
The <<length>> values give outsets
(similar to 'margin' or 'border-image-outset').
The <a>scroll snap area</a> is the bounding box of the transformed border box,
plus the specified outsets,
in the <a>scroll container’s</a> coordinate space.
Note: This ensures that the <a>scroll snap area</a> is always rectangular and axis-aligned
to the <a>scroll container’s</a> coordinate space.
This property is a <a>shorthand property</a> that sets
all of the <a href="#longhands"><css>scroll-snap-margin-*</css> longhands</a>
in one declaration.
<!--
███ ██ ████ ██████ ██ ██
██ ██ ██ ██ ██ ██ ███ ██
██ ██ ██ ██ ██ ████ ██
██ ██ ██ ██ ██ ████ ██ ██ ██
█████████ ██ ██ ██ ██ ██ ████
██ ██ ██ ██ ██ ██ ██ ███
██ ██ ████████ ████ ██████ ██ ██
-->
Scroll Snapping Alignment: the 'scroll-snap-align' property {#scroll-snap-alignment}
--------------------------
<pre class="propdef">
Name: scroll-snap-align
Value: [ none | edges | start | end | center ]{1,2}
Initial: none
Applies to: all elements
Inherited: no
Percentages: n/a
Computed value: two keywords
Animatable: no
Media: interactive
</pre>
Specifies the element's <a>snap position</a> as an alignment of
its <a>snap area</a> (as the <a>alignment subject</a>)
within the viewport's <a>snap viewport</a> (as the <a>alignment container</a>).
The two values specify the snapping behavior in the <a>inline axis</a> and <a>block axis</a>,
respectively.
If only one value is specified,
the second value defaults to the same value.
<div class="example">
The following example aligns the start edge of the box's <a>snap area</a>
to the start edge of the scroll container's <a>snap viewport</a>:
<pre>section { scroll-snap-align: start; }</pre>
The following example aligns the center of each city
to the center of the scroll container's <a>snap viewport</a>,
snapping only when the city is centered in both axes:
<pre>
.map { scroll-snap-type: proximity point; }
.map .city { scroll-snap-align: center; }
</pre>
The following example aligns the center of each photo
to the center of the scroll container's <a>snap viewport</a>,
snapping independently in each axis:
<pre>
.photos { scroll-snap-type: mandatory both; }
img { scroll-snap-align: center; }
</pre>
</div>
The <dfn export>scroll alignment values</dfn> are defined as follows:
<dl dfn-type=value dfn-for="scroll-snap-align">
<dt><dfn>none</dfn>
<dd>
This box does not define a <a>snap position</a> in the specified axis.
<dt><dfn>start</dfn>
<dd>
Start alignment of this box's <a>scroll snap area</a>
within the <a>scroll container</a>'s <a>snap viewport</a>
is a valid <a>snap position</a>.
<dt><dfn>end</dfn>
<dd>
End alignment of this box's <a>scroll snap area</a>
within the <a>scroll container</a>'s <a>snap viewport</a>
is a valid <a>snap position</a>.
<dt><dfn>center</dfn>
<dd>
Center alignment of this box's <a>scroll snap area</a>
within the <a>scroll container</a>'s <a>snap viewport</a>
is a valid <a>snap position</a>.
<dt><dfn>edges</dfn>
<dd>
Both start alignment and end alignment of this box's <a>scroll snap area</a>
within the <a>scroll container</a>'s <a>snap viewport</a>
are valid <a>snap positions</a>.
Issue: It's not 100% clear if this value is necessary, given handling for <a href="#snap-overflow">over-large elements</a>.
</dl>
If the element's <a>scroll container</a> is <a>point-snapping</a>,
and this property does not specify a single valid <a>snap position</a> in both axises
(that is, it contains ''scroll-snap-align/none'' or ''edges''),
the element does not contribute any <a>snap positions</a> at all.
For all of these values,
the <a>block</a> or <a>inline</a> axis
is relative to the element's parent's <a>writing mode</a>.
Issue: Is this the correct writing mode to compute against?
Or should it be the scroll container's writing mode?
<details class="why">
<summary>Why no <<length>> or <<position>> values?</summary>
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--
but does so in a smarter way, that degrades better on small screens (see above)
because it provides more information (a box, rather than a point) to the UA.
We could have also added lengths here,
but it would provide multiple ways to do the same thing,
which is additional overhead for implementation, testing, and (most importantly) author learning.
It also introduces more room for cascading errors,
and guides authors in the wrong direction--
away from 'scroll-snap-margin'.
</details>
<h4 id="snap-scope">
Scoping Valid Snap Positions to Visible Boxes</h4>
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 <a>snap position</a>
only if at least part of the <a>snap area</a> is within the <a>snap viewport</a>.
For example, a <a>snap area</a> is top-aligned to the <a>snap viewport</a>
if its top edge is coincident with the <a>snap viewport</a>’s top edge;
however, this alignment is nonetheless not a valid <a>snap position</a>
if the entire <a>snap area</a> is outside the <a>snap viewport</a>.
<details class="why">
<summary>Why limit snapping to only when the element is visible?</summary>
As the <a href="https://www.webkit.org/blog/4017/scroll-snapping-with-css-snap-points/">WebKit implementers point out</a>,
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.)
</details>
<h4 id="snap-overflow">
Snapping Boxes that Overflow the Scrollport</h4>
If the <a>snap area</a> is larger than the <a>snap viewport</a> in a particular axis,
and there are no other <a>snap areas</a> within the <a>snap viewport</a>,
then any scroll position in which the <a>snap area</a> covers the <a>snap viewport</a>
is a valid <a>snap position</a> 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).
<div class="example">
For example, take the third code fragment in the previous example,
which had a photo as the area.
The author wants mandatory snapping from item to item,
but if the item happens to be larger than your viewport,
you want to be able to scroll around the whole thing once you're over it.
Since the <a>snap area</a> is larger than the <a>snap viewport</a>,
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 <a>snap position</a>.
</div>
<!--
██████ ████████ ███████ ████████
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██
██████ ██ ██ ██ ████████
██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██
██████ ██ ███████ ██
-->
Scroll Snap Limits: the 'scroll-snap-stop' property {#scroll-snap-stop}
--------------------------
<pre class="propdef">
Name: scroll-snap-stop
Value: normal | always
Initial: normal
Applies to: all elements
Inherited: no
Percentages: n/a
Computed value: as specified
Animatable: no
Media: interactive
</pre>
This property specifies whether the <a>snap position</a>
absorbs all remaining inertia during an <a>inertial scroll</a>,
or allows the <a>inertial scroll</a> to pass multiple <a>snap positions</a> before coming to rest.
Values are defined as follows:
<dl dfn-type=value dfn-for=scroll-snap-stop>
<dt><dfn>normal</dfn>
<dd>
A <a>snap position</a> defined by this element
does not interfere with the inertia
of an <a>inertial scroll</a> that is passing across it,
unless it is the landing <a>snap position</a>.
<dt><dfn>always</dfn>
<dd>
A <a>snap position</a> defined by this element,
when encountered by an <a>inertial scroll</a>,
absorbs all remaining inertia from an <a>inertial scroll</a>,
forcing a stop at this <a>snap position</a>,
exactly as if the scroll had enough inertia to reach the <a>snap position</a>,
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 <a>snap position</a>
per inertial scroll action.
</dl>
Snapping Mechanics {#snap}
==========================
The precise model algorithm to select a <a>snap position</a> 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.
<!--
████████ ██ ██ ████████ ████████ ██████
██ ██ ██ ██ ██ ██ ██ ██
██ ████ ██ ██ ██ ██
██ ██ ████████ ██████ ██████
██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██
██ ██ ██ ████████ ██████
-->
Types of Scrolling Methods {#scroll-types}
------------------------------------------
There are at least three distinct form of scroll methods that a user might perform on a page,
which can reasonably trigger different snapping behaviors:
: <dfn export local-lt="explicit" lt="explicit scroll">explicit scrolling</dfn>
:: A scroll is <a>explicit</a> if the user is explicitly scrolling to a well-defined and obvious end-point.
This includes methods such as:
* a panning gesture,
released without momentum
* manipulating the scrollbar "thumb" explicitly
* programmatically scrolling via APIs such as {{Window/scrollTo()}}.
: <dfn export local-lt="inertial" lt="inertial scroll">inertial scrolling</dfn>
:: A scroll is <a>inertial</a> if it is a method where the user "flings" the scroll position,
indicating a direction and a momentum for the scroll,
but no well-defined and intentional end-point.
User agents tend to implement <a>inertial</a> scrolls
by simulating a "friction" force that gradually reduces the scroll's momentum,
or by otherwise gradually reducing the speed in a way feels "natural"
and respects the user's intention.
The scroll position that an <a>inertial</a> scroll would naturally land on
without further intervention is the <dfn noexport>natural end-point</dfn>.
: <dfn export local-lt="semantic" lt="semantic scroll">semantic scrolling</dfn>
:: A scroll is <a>semantic</a> if it expresses a preferred direction to scroll in,
but not a geometric amount of scrolling,
or a specific "momentum" to a fling.
This is most commonly from pressing an arrow key;
for example, pressing the Down key indicates that you want to scroll down some amount.
Additionally, because page layouts usually align things vertically and/or horizontally,
UAs sometimes <dfn export>axis-lock</dfn> a scroll when its direction
is sufficiently vertical or horizontal.
An <a>axis-locked</a> scroll is bound to only scroll along that axis.
This prevents,
for example,
a <em>nearly</em> horizontal fling gesture from gradually drifting up or down as well,
because it is very difficult to fling in a precisely horizontal line.
<div class="issue">
Rewrite this section to be clearer,
perhaps by classifying scroll methods into those that have
<ul>
<li>a preferred direction
<li>a preferred position
<li>both a preferred direction and a preferred position
</ul>
?
</div>
<!--
██ ████████ ███████ ████████
████ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██
██ ██ ██ ███████ ██ ██
██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██
██████ ████████ █████████ ████████
-->
Axis vs Point-Snapping {#snap-dimensions}
-----------------------------------------
There are two distinct <dfn lt="snap behavior|snapping behavior">snapping behaviors</dfn> that a <a>scroll container</a> might engage in:
: <dfn export local-lt="axis" lt="axis-snapping|axis-snap">axis-snapping</dfn>
:: If a <a>scroll container</a> is <a>axis-snapping</a>,
its descendants indicate a desired scroll position
in each axis of the <a>scroll container</a> independently,
with no dependent preference for what the other axis's scroll position should be.
Note: This is the “default” type of <a>snap behavior</a>
that most <a>scroll containers</a> will want to use,
and so the ''scroll-snap-type'' property intentionally defaults to it.
Note: An element in an <a>axis-snapping</a> <a>scroll container</a>
can declare two <a>snap positions</a>,
one in each axis.
If one of the element's <a>snap positions</a> is chosen in one axis,
this has no bearing on the other dimension's <a>snap position</a>--
it might be chosen,
or a different element's <a>snap position</a> might be chosen for that axis,
or that axis might not snap at all.
: <dfn export local-lt="point" lt="point-snapping|point-snap">point-snapping</dfn>
:: If a <a>scroll container</a> is <a>point-snapping</a>,
its descendants indicate a desired scroll position
in both axises of the <a>scroll container</a> simultaneously--
in other words,
some point in the descendant must be aligned to a corresponding point in the <a>scroll container</a>.
This type of <a>snapping behavior</a> 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 <a>scroll container</a> was <a>axis-snapping</a>).
<!--
██████ ██ ██ ███████ ███████ ██████ ████ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██
██ █████████ ██ ██ ██ ██ ██████ ██ ██ ██ ██ ██ ████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██
██████ ██ ██ ███████ ███████ ██████ ████ ██ ██ ██████
-->
Choosing Snap Positions {#choosing}
-----------------------------------
A <a>scroll container</a> can have many <a>snap areas</a>
scattered throughout its <a>scrollable area</a>.
A naive algorithm for selecting a <a>snap position</a>
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:
* <a>Snap positions</a> should be chosen to minimize the distance between the end-point
(or the <a>natural end-point</a>)
and the final snapped scroll position,
subject to the additional constraints listed in this section.
* <a>Point-snapping</a> is all-or-nothing;
if the <a>snap position</a> of an element is chosen to align to,
the <a>scroll container</a> must set its scroll position
according to the element's <a>snap positions</a> in <em>both</em> axises;
the <a>scroll container</a> <em>must not</em> “partially align” to the element
by taking its <a>snap position</a> in one axis
and aligning the other axis according to something else.
* If a scroll is <a>axis-locked</a> and the <a>scroll container</a> is <a>axis-snapping</a>,
any <a>snap positions</a> in the other axis should be ignored
during the scroll.
(However, <a>snap positions</a> in the other axis can still effect the final scroll position.)
If a scroll is <a>axis-locked</a> and the <a>scroll container</a> is <a>point-snapping</a>,
<a>snap positions</a> should be penalized in the selection process
according to the amount of other-axis scrolling they would cause.
* <a>Snap positions</a> should be ignored if their elements are far outside of the “corridor”
that the <a>snap viewport</a> defines as it moves through the <a>scrollable area</a>
during an <a>inertial scroll</a>,
or a hypothetical “corridor” in the direction of a <a>semantic scroll</a>,
or the <a>snap viewport</a> after an <a>explicit scroll</a>.
(This is to prevent a far-offscreen element
from having difficult-to-understand effects
on the scroll position.)
* User agents <em>must</em> ensure that a user can “escape” a <a>snap position</a>,
regardless of the scroll method.
For example, if the snap type is ''mandatory''
and the next <a>snap position</a> 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 <a>snap position</a>
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 <a>semantic scroll</a> must always ignore the starting <a>snap positions</a>.)
* If the most appropriate <a>snap position</a> is unreachable,
such that aligning to it would require scrolling the <a>scroll container</a>’s viewport
past the edge of its <a>scrollable area</a>,
the <a>scroll container</a> must be scrolled <em>as much as possible</em> in each relevant axis
toward the desired <a>snap position</a>.
This is the <em>used</em> <a>snap position</a> corresponding to that <a>snap position</a>;
that is, the UA must “snap” to this position just as it would have if it were the actual specified alignment.
* 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 <a>snap positions</a>,
the user agent should snap to one of that element's <a>snap positions</a>.
The user agent may do this even when the <a>scroll container</a> has ''scroll-snap-type: none''.
<!--
Group-based Snapping {#group}
========================
Issue: This section will likely be dropped.
Collects the <a>scroll snap areas</a> of all group-snapped boxes,
segments them into groups that will fit within the viewport,
then creates synthesized <a>scroll snap areas</a> to represent each group.
The <a>snap positions</a> introduced by these boxes
is then the 'scroll-group-align' alignment of each such group
within the viewport.
(Note that such areas may overlap,
if group-snapped boxes are arranged in an overlapping pattern.)
This is a simple form of “scrolling by pages”.
<div class="example">
Use Case 1: Snapping to the top of each “page” of address book entries in a list of entries.
<pre class="lang-css">
:root {
scroll-snap-type: proximity;
scroll-group-align: start;
}
article {
scroll-snap-align: group;
}
</pre>
</div>
<div class="example">
Use Case 2: Scrolling an article to the first paragraph that hasn't been completely read.
<pre class="lang-css">
article {
scroll-snap-type: proximity;
scroll-group-align: start;
}
article > * {
scroll-snap-align: group;
}
</pre>
</div>
<div class="example">
Use Case 3: Scrolling image gallery, a la Pinterest, where images are packed tightly on the page.
<pre class="lang-css">
.gallery {
scroll-snap-type: proximity;
scroll-group-align: center;
}
.gallery > img {
scroll-snap-align: group;
}
</pre>
</div>
Turning On Group Snapping: the ''group'' value of 'scroll-snap-align' {#scroll-snap-align-group}
-------------------------
<pre class="propdef partial">
Name: scroll-snap-align
New values: group
</pre>
The <dfn value for=scroll-snap-align>group</dfn> value
specifies that this element's scroll snap area should be group-aligned to the viewport.
Aligning the Group: the 'scroll-snap-group' property {#scroll-snap-group}
-----------------
<pre class="propdef">
Name: scroll-snap-group-align
Value: <'scroll-snap-align'>
Initial: start
Applies to: all elements
Inherited: no
Computed value: as specified
Animatable: no
Media: interactive
</pre>
Specifies the alignment of a group-snapped group's area within the viewport.
-->
<!--
██ ███████ ██ ██ ██████ ██ ██ ███ ██ ██ ████████ ██████
██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ ██
██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ████ █████████ ██ ██ ██ ██ ██ ██ ██ ██████
██ ██ ██ ██ ████ ██ ██ ██ ██ █████████ ██ ████ ██ ██ ██
██ ██ ██ ██ ███ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██
████████ ███████ ██ ██ ██████ ██ ██ ██ ██ ██ ██ ████████ ██████
-->