-
Notifications
You must be signed in to change notification settings - Fork 707
/
Copy pathProposal.src.html
1164 lines (982 loc) · 45.3 KB
/
Proposal.src.html
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
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, Apple, http://fantasai.inkedblade.net/contact
Abstract: A brief description of an alternate model for scroll-snapping.
Ignored Terms: snap position, snap positions, 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: start;
}
</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: start;
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 ]{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' property 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>
after scrolling operations.
<a>Snap positions</a> are specified
as a particular alignment ('scroll-snap-align')
of a box’s <a>scroll snap area</a>
(its border bounding box, as modified by 'scroll-snap-margin')
within the <a>scroll container</a>’s <a>snapport</a>
(its scrollport, 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>.
A scroll position that satisfies the specified alignment
is a valid <a>snap position</a>.
The act of adjusting the scroll position
of a scroll container’s scrollport
such that it is aligned to a snap position
is called <dfn lt="snap | snapping | snapped">snapping</dfn>,
and a <a>scroll container</a> is said to be
<a>snapped</a> to a <a>snap position</a>
if its scrollport’s scroll position
is that <a>snap position</a>
and there is no active scrolling operation.
The CSS Scroll Snap Module
intentionally does not specify nor mandate
any precise animations or physics used to enforce <a>snap positions</a>;
this is left up to the user agent.
<a>Snap positions</a> only affect the nearest ancestor
<a>scroll container</a>
on the element's <a>containing block chain</a>.
Capturing Scroll Snap Areas: Properties on the scroll 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
</pre>
The 'scroll-snap-type' property specifies
whether a <a>scroll container</a> is a <a>scroll snap container</a>,
how strictly it <a>snaps</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 <dfn noexport lt="strictness value">strictness values</dfn>
(''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 position).
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 <a>scroll container</a>
must not <a>snap</a>:
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 <a>scroll container</a>
may <a>snap</a> to a valid <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>.
<dt><dfn>mandatory</dfn>
<dd>
If specified on a <a>scroll container</a>,
the <a>scroll container</a>
is required to be <a>snapped</a> to a valid <a>snap position</a>
when there are no active scrolling operations.
That is, it must <a>snap</a> to a valid <a>snap position</a>
at the termination of a scroll, if any such 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>.
</dl>
A box <dfn export>captures snap positions</dfn>
if it is a <a>scroll container</a>
<em>or</em> has a value other than ''scroll-snap-type/none'' for 'scroll-snap-type'.
If a box's nearest <a lt="captures snap positions">snap-position capturing</a> ancestor
on its <a>containing block chain</a>
is a <a>scroll container</a> with a non-''scroll-snap-type/none'' value for 'scroll-snap-type',
that is the box's <dfn export local-lt="snap container">scroll snap container</dfn>.
Otherwise, the box has no <a>scroll snap container</a>,
and its <a>snap positions</a> do not trigger <a>snapping</a>.
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 <dfn noexport lt="axis value">axis values</dfn>
specify 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.
If the content or layout of the <a>scroll container</a> changes
(e.g. content is added, moved, deleted, resized),
the UA must re-evaluate, and potentially re-<a>snap</a> if necessary,
the resulting scroll position
once the positions of the content have restabilized.
This can result in no snapping in some cases
(e.g. if there are no nearby <a>snap positions</a>
for a ''proximity''-snapping <a>scroll container</a>).
However, if there was a previously-<a>snapped</a> <a>snap position</a>
(associated with the same element)
that still exists after such changes,
the UA must remain <a>snapped</a> to it--
even if the changes would have placed a different <a>snap position</a>
closer to the current scroll position.
Otherwise, the <a>scroll container</a> must be re-<a>snapped</a>
as if the user had scrolled to its current scroll position
(as an <a lt=explicit>explicit scroll</a>, if no other scroll operation is active).
<!--
████████ ███ ████████ ████████ ████ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██
████████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████
██ █████████ ██ ██ ██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██
██ ██ ██ ████████ ████████ ████ ██ ██ ██████
-->
Scroll Snapport: 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 dimension of the <a>scroll container</a>’s scrollport
Computed value: as specified, with lengths made absolute
Animatable: as length, percentage, or calc
</pre>
The 'scroll-snap-padding' property defines the <dfn local-lt="snapport">scroll snapport</dfn>--
the area of the scrollport that is used as the <a>alignment container</a>
for the <a>scroll snap areas</a> when calculating <a>snap positions</a>.
Values are interpreted as for 'padding',
and specify inward offsets from each edge of the scrollport.
<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.
Aligning Scroll Snap Areas: Properties on the scrolling content {#element}
==========================================================================
<!--
███ ████████ ████████ ███
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ████████ ██████ ██ ██
█████████ ██ ██ ██ █████████
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ████████ ██ ██
-->
Scroll Snapping Margin: 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: as length
</pre>
The 'scroll-snap-margin' property defines
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
(interpreted as for 'margin' or 'border-image-outset').
The <a>scroll snap area</a> is the rectangular bounding box of the transformed border box,
plus the specified outsets,
axis-aligned 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 | start | end | center ]{1,2}
Initial: none
Applies to: all elements
Inherited: no
Percentages: n/a
Computed value: two keywords
Animatable: no
</pre>
The 'scroll-snap-align' property specifies
the box's <a>snap position</a> as an alignment of
its <a>snap area</a> (as the <a>alignment subject</a>)
within its <a>snap container's</a> <a>snapport</a> (as the <a>alignment container</a>).
The two values specify snapping alignment
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>snapport</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>snapport</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>snapport</a>,
snapping independently in each axis:
<pre>
.photos { scroll-snap-type: mandatory both; }
img { scroll-snap-align: center; }
</pre>
</div>
Values 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>snapport</a>
is a valid <a>snap position</a>
in the specified axis.
<dt><dfn>end</dfn>
<dd>
End alignment of this box's <a>scroll snap area</a>
within the <a>scroll container</a>'s <a>snapport</a>
is a valid <a>snap position</a>
in the specified axis.
<dt><dfn>center</dfn>
<dd>
Center alignment of this box's <a>scroll snap area</a>
within the <a>scroll container</a>'s <a>snapport</a>
is a valid <a>snap position</a>
in the specified axis.
</dl>
If the element's <a>scroll container</a> is <a>point-snapping</a>,
and this property does not specify a valid <a>snap position</a> in both axises
(that is, it contains ''scroll-snap-align/none''),
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="note">
<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>snapport</a>.
For example, a <a>snap area</a> is top-aligned to the <a>snapport</a>
if its top edge is coincident with the <a>snapport</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>snapport</a>.
<details class="note">
<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>snapport</a> in a particular axis,
and there are no other <a>snap areas</a> within the <a>snapport</a>
that would provide a <a>snap position</a> aligning the overflowing <a>snap area</a> within the <a>snapport</a>,
then any scroll position in which the <a>snap area</a> covers the <a>snapport</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>snapport</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>
<h4 id="unreachable">
Unreachable Snap Areas</h4>
If a <a>snap position</a> is unreachable as specified,
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 <em>used</em> <a>snap position</a> for this <a>snap area</a>
is the position resulting from scrolling <em>as much as possible</em> in each relevant axis
toward the desired <a>snap position</a>.
<!--
██████ ████████ ███████ ████████
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██
██████ ██ ██ ██ ████████
██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██
██████ ██ ███████ ██
-->
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
</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-concepts}
===================================
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}
------------------------------------------
When a page is scrolled,
the action is performed with
an intended end position
and/or an intended direction.
Each combination of these two things
defines a distinct category of scrolling,
which can be treated slightly differently:
: <dfn export local-lt="explicit" lt="explicit scroll">explicit scrolling</dfn>
:: A scroll is <a>explicit</a> if it has an intended end position,
but no intended direction.
This includes methods such as:
* a panning gesture,
released without momentum
* manipulating the scrollbar "thumb" explicitly
* programmatically scrolling via APIs such as {{Window/scrollTo()}}
* tabbing through the document's focusable elements
* navigating to an anchor within the page
: <dfn export local-lt="inertial" lt="inertial scroll">inertial scrolling</dfn>
:: A scroll is <a>inertial</a> if it has both an intended end position
and an intended direction.
This includes methods such as:
* a "fling" gesture,
released with momentum
(the "intended" end position might be implicitly determined by the UA's scrolling physics,
but the strength of the user's fling still expresses a weak intention
about where the scroll should end up)
* a mousewheel scroll
* programmatically scrolling via APIs such as {{Window/scrollBy()}}
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="directional" lt="directional scroll">directional scrolling</dfn>
:: A scroll is <a>directional</a> if it has an intended direction,
but no intended end point.
This includes methods such as:
* pressing an arrow key on the keyboard
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.
<!--
██ ████████ ███████ ████████
████ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██
██ ██ ██ ███████ ██ ██
██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██
██████ ████████ █████████ ████████
-->
Axis vs Point-Snapping {#snap-dimensions}
-----------------------------------------
Issue: This feature is planned to be removed in the next publication
in order to reduce the feature-set of Level 1.
It is included here for future reference in defining Level 2.
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>snapport</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>directional scroll</a>,
or the <a>snapport</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>directional scroll</a> must always ignore the starting <a>snap positions</a>.)
* 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 <a>snap</a> 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>