forked from oddbird/css-anchor-positioning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1262 lines (1195 loc) Β· 41.8 KB
/
index.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>CSS Anchor Positioning Polyfill Demo</title>
<link rel="stylesheet" href="https://unpkg.com/open-props" />
<link
rel="stylesheet"
href="https://unpkg.com/[email protected]/themes/prism.css"
/>
<script
src="https://unpkg.com/@oddbird/popover-polyfill@latest/dist/popover.min.js"
crossorigin="anonymous"
defer
></script>
<link rel="stylesheet" href="/demo.css" />
<!-- Included to test invalid stylesheets -->
<link rel="stylesheet" href="/fake.css" />
<link rel="stylesheet" href="/anchor.css" />
<link rel="stylesheet" href="/anchor-positioning.css" />
<link rel="stylesheet" href="/anchor-popover.css" />
<link rel="stylesheet" href="/position-try.css" />
<link rel="stylesheet" href="/position-try-tactics.css" />
<link rel="stylesheet" href="/position-try-tactics-combined.css" />
<link rel="stylesheet" href="/position-anchor.css" />
<link rel="stylesheet" href="/anchor-scroll.css" />
<link rel="stylesheet" href="/anchor-size.css" />
<link rel="stylesheet" href="/anchor-math.css" />
<link rel="stylesheet" href="/anchor-name-custom-prop.css" />
<link rel="stylesheet" href="/anchor-name-list.css" />
<link rel="stylesheet" href="/anchor-custom-props.css" />
<link rel="stylesheet" href="/anchor-duplicate-custom-props.css" />
<link rel="stylesheet" href="/anchor-implicit.css" />
<link rel="stylesheet" href="/anchor-update.css" />
<link rel="stylesheet" href="/anchor-absolute.css" />
<link rel="stylesheet" href="/anchor-pseudo-element.css" />
<link rel="stylesheet" href="/anchor-media-query.css" />
<style>
#my-anchor-style-tag {
anchor-name: --my-anchor-style-tag;
}
#my-target-style-tag {
position: absolute;
bottom: anchor(--my-anchor-style-tag start);
right: anchor(--my-anchor-style-tag left);
}
#anchor-manual .anchor {
inline-size: fit-content;
margin: 3rem auto;
}
</style>
<script type="module">
import polyfill from '/src/index-fn.ts';
const SUPPORTS_ANCHOR_POSITIONING =
'anchorName' in document.documentElement.style;
const btn = document.getElementById('apply-polyfill');
if (!SUPPORTS_ANCHOR_POSITIONING) {
btn.addEventListener('click', () =>
polyfill().then((rules) => {
btn.innerText = 'Polyfill Applied';
btn.setAttribute('disabled', '');
console.log(rules);
}),
);
} else {
btn.innerText = 'No Polyfill Needed';
btn.setAttribute('disabled', '');
console.log(
'anchor-positioning is supported in this browser; polyfill skipped.',
);
}
const updateBtn = document.getElementById('toggle-anchor-width');
const updateAnchor = document.getElementById('my-anchor-update');
updateBtn.addEventListener('click', () => {
if (updateAnchor.getAttribute('data-small')) {
updateAnchor.setAttribute('data-large', true);
updateAnchor.removeAttribute('data-small');
} else {
updateAnchor.setAttribute('data-small', true);
updateAnchor.removeAttribute('data-large');
}
});
function prepareManualPolyfill() {
// anchor style element
const anchorStyleEl = document.createElement('style');
anchorStyleEl.id = 'my-style-manual-anchor';
anchorStyleEl.textContent = [
'#my-anchor-manual {',
'anchor-name: --my-anchor-manual;',
'}',
].join('');
// style element
const styleEl = document.createElement('style');
styleEl.id = 'my-style-manual-style-el';
styleEl.textContent = [
'#my-target-manual-style-el {',
'position: absolute;',
'bottom: anchor(--my-anchor-manual top);',
'right: anchor(--my-anchor-manual left);',
'}',
].join('');
// link element
const linkEl = document.createElement('link');
linkEl.id = 'my-style-manual-link-el';
linkEl.rel = 'stylesheet';
linkEl.href = '/anchor-manual.css';
document.head.append(anchorStyleEl, styleEl, linkEl);
// inline style
document
.getElementById('my-target-manual-inline-style')
?.setAttribute(
'style',
[
'position: absolute',
'top: anchor(--my-anchor-manual bottom)',
'left: anchor(--my-anchor-manual right)',
].join(';'),
);
}
// These event listeners are for E2E testing only
document
.getElementById('prepare-manual-polyfill')
?.addEventListener('click', () => prepareManualPolyfill(), {
once: true,
});
const manualSet1Button = document.getElementById(
'apply-polyfill-manually-set1',
);
manualSet1Button?.addEventListener('click', (e) => {
polyfill({
elements: [
document.getElementById('my-style-manual-anchor'),
document.getElementById('my-style-manual-style-el'),
],
excludeInlineStyles: true,
}).then(() => {
manualSet1Button.setAttribute('disabled', '');
});
});
const manualSet2Button = document.getElementById(
'apply-polyfill-manually-set2',
);
manualSet2Button?.addEventListener('click', (e) => {
polyfill({
elements: [
document.getElementById('my-style-manual-anchor'),
document.getElementById('my-style-manual-link-el'),
document.getElementById('my-target-manual-inline-style'),
],
excludeInlineStyles: true,
}).then(() => {
manualSet2Button.setAttribute('disabled', '');
});
});
const manualSet3Button = document.getElementById(
'apply-polyfill-manually-set3',
);
manualSet3Button?.addEventListener('click', (e) => {
polyfill({
elements: [
document.getElementById('my-style-manual-anchor'),
document.getElementById('my-style-manual-style-el'),
],
}).then(() => {
manualSet3Button.setAttribute('disabled', '');
});
});
const manualBtn = document.getElementById('apply-polyfill-manually');
if (SUPPORTS_ANCHOR_POSITIONING) {
manualBtn.innerText = 'Load Anchor Positioning CSS';
}
manualBtn.addEventListener('click', () => {
prepareManualPolyfill();
if (!SUPPORTS_ANCHOR_POSITIONING) {
polyfill({
elements: [
document.getElementById('my-style-manual-anchor'),
document.getElementById('my-style-manual-link-el'),
document.getElementById('my-style-manual-style-el'),
document.getElementById('my-target-manual-inline-style'),
],
}).then((rules) => {
manualBtn.innerText = 'Polyfill Applied';
manualBtn.setAttribute('disabled', '');
console.log(rules);
});
} else {
manualBtn.innerText = 'Anchor Positioning CSS applied';
console.log(
'anchor-positioning is supported in this browser; polyfill skipped.',
);
}
manualBtn.setAttribute('disabled', '');
});
</script>
<script src="https://unpkg.com/[email protected]/components/prism-core.min.js"></script>
<script src="https://unpkg.com/[email protected]/plugins/autoloader/prism-autoloader.min.js"></script>
</head>
<body>
<header>
<h1>CSS Anchor Positioning Polyfill</h1>
<nav>
<span> See also: </span>
<a
href="https://github.com/web-platform-tests/wpt/tree/master/css/css-anchor-position"
target="_blank"
rel="noopener noreferrer"
>WPT examples</a
>
<!-- <a
href="https://anchor-position-wpt.netlify.app/"
target="_blank"
rel="noopener noreferrer"
>WPT results</a
> -->
<a
href="https://w3c.github.io/csswg-drafts/css-anchor-position/"
target="_blank"
rel="noopener noreferrer"
>Draft Spec</a
>
</nav>
<button id="apply-polyfill" data-button="apply-polyfill" type="button">
Apply Polyfill
</button>
</header>
<section>
<h2>Anchoring Elements Using CSS</h2>
<p>
The CSS anchor positioning
<a
href="https://w3c.github.io/csswg-drafts/css-anchor-position/"
target="_blank"
rel="noopener noreferrer"
>specification</a
>
defines anchor positioning, "where a positioned element can size and
position itself relative to one or more 'anchor elements' elsewhere on
the page." This CSS Anchor Positioning Polyfill supports and is based on
this specification.
</p>
<p>
The proposed <code>anchor()</code> and
<code>anchor-size()</code> functions add flexibility to how absolutely
positioned elements can be placed within a layout. Instead of being
sized and positioned based solely on the position of their containing
block, the proposed new functions allow absolutely positioned elements
to be placed relative to one or more author-defined anchor elements.
</p>
<ul>
<li>
View the polyfill on
<a
href="https://github.com/oddbird/css-anchor-positioning"
target="_blank"
rel="noopener noreferrer"
>GitHub</a
>
or browse
<a
href="https://github.com/oddbird/css-anchor-positioning/issues"
target="_blank"
rel="noopener noreferrer"
>open issues</a
>.
</li>
<li>
Find the polyfill on
<a
href="https://www.npmjs.com/package/@oddbird/css-anchor-positioning"
target="_blank"
rel="noopener noreferrer"
>npm</a
>.
</li>
<li>
Browse or open issues related to the specification
<a
href="https://github.com/w3c/csswg-drafts/labels/css-anchor-1"
target="_blank"
rel="noopener noreferrer"
>here</a
>
or
<a
href="https://github.com/tabatkins/specs/issues?q=is%3Aissue+is%3Aopen+%5Bcss-anchor-position%5D"
target="_blank"
rel="noopener noreferrer"
>here</a
>.
</li>
</ul>
<p>
<strong>Note: </strong>This polyfill was implemented against an early
version of the spec, and updates were paused to allow the syntax to
solidify. Now that browsers are working on implementation, we are in the
process of bringing it up to date, and welcome
<a
href="https://github.com/oddbird/css-anchor-positioning"
target="_blank"
rel="noopener noreferrer"
>code contributions</a
>
and <a href="#sponsor">financial support</a> to make that happen.
</p>
</section>
<section id="anchor-positioning" class="demo-item">
<h2>
<a href="#anchor-positioning" aria-hidden="true">π</a>
Positioning with <code>anchor()</code> [stylesheet]
</h2>
<div style="position: relative" class="demo-elements">
<div id="my-target-positioning" class="target">Target</div>
<div id="my-anchor-positioning" class="anchor">Anchor</div>
</div>
<p class="note">
With polyfill applied: Target and Anchor's right edges line up. Target's
top edge lines up with the bottom edge of the Anchor.
</p>
<pre><code class="language-css"
>#my-anchor-positioning {
anchor-name: --my-anchor-positioning;
}
#my-target-positioning {
position: absolute;
top: anchor(--my-anchor-positioning bottom);
right: anchor(--my-anchor-positioning right, 50px);
}</code></pre>
</section>
<section id="anchor-style-tag" class="demo-item">
<h2>
<a href="#anchor-style-tag" aria-hidden="true">π</a>
Positioning with <code>anchor()</code> [<style> tag]
</h2>
<div style="position: relative" class="demo-elements">
<div id="my-target-style-tag" class="target">Target</div>
<div id="my-anchor-style-tag" class="anchor">Anchor</div>
</div>
<p class="note">
With polyfill applied: Target is positioned at the top left corner of
the Anchor.
</p>
<pre><code class="language-css"
>/* a <style> tag */
#my-anchor-style-tag {
anchor-name: --my-anchor-style-tag;
}
#my-target-style-tag {
position: absolute;
bottom: anchor(--my-anchor-style-tag start);
right: anchor(--my-anchor-style-tag left);
}</code></pre>
</section>
<section id="anchor-inline" class="demo-item">
<h2>
<a href="#anchor-inline" aria-hidden="true">π</a>
Positioning with <code>anchor()</code> [inline styles]
</h2>
<div style="position: relative" class="demo-elements">
<div
id="my-target-inline"
class="target"
style="
position: absolute;
top: anchor(--my-anchor-in-line bottom);
right: anchor(--my-anchor-in-line right);
"
>
Target
</div>
<div
id="my-anchor-inline"
class="anchor"
style="anchor-name: --my-anchor-in-line"
>
Anchor
</div>
</div>
<p class="note">
With polyfill applied: Target and Anchor's right edges line up. Target's
top edge lines up with the bottom edge of the Anchor.
</p>
<pre><code class="language-css"
>/* inline <style> attributes */
/* anchor */
style="anchor-name: --my-anchor-in-line"
/* target */
style="
position: absolute;
top: anchor(--my-anchor-in-line bottom);
right: anchor(--my-anchor-in-line right);
"</code></pre>
</section>
<section id="implicit-name" class="demo-item">
<h2>
<a href="#implicit-name" aria-hidden="true">π</a>
Positioning with <code>anchor()</code> [implicit
<code>anchor</code> attribute]
</h2>
<div style="position: relative" class="demo-elements">
<div id="my-implicit-anchor" class="anchor">Anchor</div>
<div id="my-implicit-target" class="target" anchor="my-implicit-anchor">
Target
</div>
</div>
<p class="note">
With polyfill applied: Target is positioned at the top left corner of
the Anchor.
</p>
<pre><code class="language-html"
><div id="my-implicit-anchor">Anchor</div>
<div id="my-implicit-target" anchor="my-implicit-anchor">Target</div></code>
<code class="language-css"
>#my-implicit-target {
position: absolute;
right: anchor(implicit left);
bottom: anchor(top);
}</code></pre>
</section>
<section id="position-anchor" class="demo-item">
<h2>
<a href="#position-anchor" aria-hidden="true">π</a>
Positioning with <code>anchor()</code> [
<code>position-anchor</code> property]
</h2>
<div style="position: relative" class="demo-elements">
<div id="my-position-anchor-a" class="anchor">Anchor A</div>
<div id="my-position-target-a" class="target">Target A</div>
<div id="my-position-anchor-b" class="anchor">Anchor B</div>
<div id="my-position-target-b" class="target">Target B</div>
</div>
<p class="note">
With polyfill applied: Targets are positioned at the top right corner of
their respective Anchors.
</p>
<pre><code class="language-html"
><div id="my-position-anchor-a" class="anchor">Anchor A</div>
<div id="my-position-target-a" class="target">Target A</div>
<div id="my-position-anchor-b" class="anchor">Anchor B</div>
<div id="my-position-target-b" class="target">Target B</div></code>
<code class="language-css"
>#my-position-target-a {
position-anchor: --my-position-anchor-a;
}
#my-position-target-b {
position-anchor: --my-position-anchor-b;
}
.target {
position: absolute;
bottom: anchor(top);
left: anchor(right);
}
#my-position-anchor-a {
anchor-name: --my-position-anchor-a;
}
#my-position-anchor-b {
anchor-name: --my-position-anchor-b;
}</code></pre>
</section>
<section id="anchor-positioning-popover" class="demo-item">
<h2>
<a href="#anchor-positioning-popover" aria-hidden="true">π</a>
Anchoring a <code>popover</code>
</h2>
<div style="position: relative" class="demo-elements">
<button
id="my-anchor-popover"
class="anchor"
popovertarget="my-target-popover"
>
Anchor (click to open)
</button>
<div id="my-target-popover" class="target" popover>
Popover (Target)
</div>
</div>
<p class="note">
With polyfill applied: Target is positioned at the bottom right corner
of the Anchor.
</p>
<pre><code class="language-css"
>#my-anchor-popover {
position: absolute;
left: 200px;
anchor-name: --my-anchor-popover;
}
#my-target-popover {
position: absolute;
left: anchor(--my-anchor-popover right);
top: anchor(--my-anchor-popover bottom);
}</code></pre>
</section>
<section id="custom-prop-name" class="demo-item">
<h2>
<a href="#custom-prop-name" aria-hidden="true">π</a>
Positioning with <code>anchor()</code> [anchor name set as CSS custom
property]
</h2>
<div style="position: relative" class="demo-elements">
<div id="my-anchor-name-prop" class="anchor">Anchor</div>
<div id="my-target-name-prop" class="target">Target</div>
</div>
<p class="note">
With polyfill applied: Target is positioned at the top left corner of
the Anchor.
</p>
<pre><code class="language-css"
>#my-anchor-name-prop {
anchor-name: --my-anchor-name-prop;
}
#my-target-name-prop {
--anchor-var: --my-anchor-name-prop;
position: absolute;
right: anchor(var(--anchor-var) left);
bottom: anchor(var(--anchor-var) top);
}</code></pre>
</section>
<section id="position-try-tactics" class="demo-item">
<h2>
<a href="#position-try-tactics" aria-hidden="true">π</a>
Fallbacks with <code>position-try-fallbacks</code>
</h2>
<div style="position: relative" class="demo-elements">
<div class="scroll-container">
<div class="placefiller-above-anchor"></div>
<div class="placefiller-before-anchor"></div>
<span id="my-anchor-try-tactics" class="anchor"
>position-try-fallbacks Anchor</span
>
<div id="my-target-try-tactics" class="target">Target</div>
<span id="my-anchor-try-tactics-2" class="anchor"
>position-try-fallbacks Anchor 2</span
>
<div id="my-target-try-tactics-2" class="target">Target 2</div>
<div class="placefiller-after-anchor"></div>
</div>
</div>
<div class="note">
<p>
With polyfill applied, the following positions are attempted in order:
</p>
<ol>
<li>
Align the bottom, left edge of the target with the top, right edge
of its anchor.
</li>
<li>
Apply <code>flip-block</code>, flipping across the inline axis, and
align the top, left edge of the target with the bottom, right edge
of its anchor.
</li>
<li>
Apply <code>flip-inline</code>, flipping across the block axis, and
align the bottom, right edge of the target with the top, left edge
of its anchor.
</li>
<li>
Apply <code>flip-start</code>, flipping across a diagonal line from
upper left to lower right, and align the top, right edge of the
target with the bottom, left edge of its anchor.
</li>
<li>
When every position overflows, revert to the last successful
fallback position if there is one, otherwise revert to the initial
base styles.
</li>
</ol>
<p>
<strong>Note:</strong> Early Chromium implementations used
<code>position-try-options</code> instead of
<code>position-try-fallbacks</code>. We recommend adding both
properties, or using the <code>position-try</code> shorthand.
</p>
</div>
<pre><code class="language-css"
>#my-anchor-try-tactics {
anchor-name: --my-anchor-try-tactics;
}
#my-anchor-try-tactics-2 {
anchor-name: --my-anchor-try-tactics-2;
}
#my-target-try-tactics {
position-anchor: --my-anchor-try-tactics;
}
#my-target-try-tactics-2 {
position-anchor: --my-anchor-try-tactics-2;
}
#my-target-try-tactics,
#my-target-try-tactics-2 {
position: absolute;
bottom: anchor(top);
left: anchor(right);
position-try-fallbacks: flip-block, flip-inline, flip-start;
}</code></pre>
</section>
<section id="position-try" class="demo-item">
<h2>
<a href="#position-try" aria-hidden="true">π</a>
Fallbacks with <code>position-try-fallbacks</code> and
<code>@position-try</code>
</h2>
<div style="position: relative" class="demo-elements">
<div class="scroll-container">
<div class="placefiller-above-anchor"></div>
<div class="placefiller-before-anchor"></div>
<span id="my-anchor-fallback" class="anchor"
>@position-try Anchor</span
>
<div id="my-target-fallback" class="target">Target</div>
<span id="my-anchor-fallback-2" class="anchor"
>@position-try Anchor 2</span
>
<div id="my-target-fallback-2" class="target">Target 2</div>
<div class="placefiller-after-anchor"></div>
</div>
</div>
<div class="note">
<p>
With polyfill applied, the following positions are attempted in order:
</p>
<ol>
<li>
Align the bottom, left edge of the target with the top, left edge of
its anchor.
</li>
<li>
Align the top, left edge of the target with the bottom, left edge of
its anchor.
</li>
<li>
Align the bottom, right edge of the target with the top, right edge
of its anchor.
</li>
<li>
Align the top, right edge of the target with the bottom, right edge
of its anchor, and set the height and width of the target to 100px.
</li>
<li>
When every position overflows, revert to the last successful
fallback position if there is one, otherwise revert to the initial
base styles.
</li>
</ol>
</div>
<pre><code class="language-css"
>#my-anchor-fallback {
anchor-name: --my-anchor-fallback;
}
#my-anchor-fallback-2 {
anchor-name: --my-anchor-fallback-2;
}
#my-target-fallback {
position-anchor: --my-anchor-fallback;
}
#my-target-fallback-2 {
position-anchor: --my-anchor-fallback-2;
}
#my-target-fallback,
#my-target-fallback-2 {
position: absolute;
/* First try to align the bottom, left edge of the target
with the top, left edge of the anchor. */
bottom: anchor(top);
left: anchor(left);
width: anchor-size(width);
position-try-fallbacks: --bottom-left, --top-right, --bottom-right;
}
@position-try --bottom-left {
/* Next try to align the top, left edge of the target
with the bottom, left edge of the anchor. */
top: anchor(bottom);
left: anchor(left);
/* Values set by the initial styles will still be applied
unless they are explicitly reverted. */
bottom: revert;
}
@position-try --top-right {
/* Next try to align the bottom, right edge of the target
with the top, right edge of the anchor. */
bottom: anchor(top);
right: anchor(right);
left: revert;
width: revert;
}
@position-try --bottom-right {
/* Finally, try to align the top, right edge of the target
with the bottom, right edge of the anchor. */
top: anchor(bottom);
right: anchor(right);
width: 100px;
height: 100px;
bottom: revert;
left: revert;
}</code></pre>
</section>
<section id="position-try-tactics-combined" class="demo-item">
<h2>
<a href="#position-try-tactics-combined" aria-hidden="true">π</a>
Positioning with combined try tactics
</h2>
<div style="position: relative" class="demo-elements">
<div class="scroll-container">
<div class="placefiller-above-anchor"></div>
<div class="placefiller-before-anchor"></div>
<span id="my-anchor-try-tactics-combined" class="anchor"
>position-try Anchor</span
>
<div id="my-target-try-tactics-combined" class="target">Target</div>
<div class="placefiller-after-anchor"></div>
</div>
</div>
<div class="note">
<p>
With polyfill applied, the following positions are attempted in order:
</p>
<ol>
<li>
Align the bottom, left edge of the target with the top, right edge
of the anchor.
</li>
<li>
Apply <code>flip-block flip-inline</code>, flipping across both the
block and inline axis, and align the top, right edge of the target
with the bottom, left edge of the anchor.
</li>
<li>
Apply <code>flip-inline --bottom-left-combined</code>, flipping
across the block axis, and align the top, right edge of the target
with the bottom, right edge of the anchor.
</li>
<li>
When every position overflows, revert to the last successful
fallback position if there is one, otherwise revert to the initial
base styles.
</li>
</ol>
</div>
<pre><code class="language-css"
>#my-anchor-try-tactics-combined {
anchor-name: --my-anchor-try-tactics-combined;
}
#my-target-try-tactics-combined {
position: absolute;
position-anchor: --my-anchor-try-tactics-combined;
bottom: anchor(top);
left: anchor(right);
position-try: flip-block flip-inline, flip-inline --bottom-left-combined;
}
@position-try --bottom-left-combined {
top: anchor(bottom);
left: anchor(left);
bottom: revert;
}</code></pre>
</section>
<section id="anchor-scroll" class="demo-item">
<h2>
<a href="#anchor-scroll" aria-hidden="true">π</a>
Positioning with <code>anchor()</code> [scrolling elements]
</h2>
<div style="position: relative" class="demo-elements">
<div class="scroll-container">
<div class="placefiller-above-anchor"></div>
<div class="placefiller-before-anchor"></div>
<span id="scroll-anchor" class="anchor">Anchor</span>
<div id="inner-anchored" class="target">Inner-anchored Target</div>
<div class="placefiller-after-anchor"></div>
</div>
<div id="outer-anchored" class="target outer-anchored">
Outer-anchored Target
</div>
</div>
<p class="note">
With polyfill applied: The Inner-anchored Target is positioned at the
top right corner of the Anchor. The Outer-anchored Target is positioned
at the bottom left corner of the Anchor.
</p>
<pre><code class="language-css"
>#scroll-anchor {
anchor-name: --scroll-anchor;
}
#inner-anchored {
position: absolute;
bottom: anchor(--scroll-anchor top);
left: anchor(--scroll-anchor end);
}
#outer-anchored {
position: absolute;
top: anchor(--scroll-anchor bottom);
right: anchor(--scroll-anchor start);
}</code></pre>
</section>
<section id="math-function" class="demo-item">
<h2>
<a href="#math-function" aria-hidden="true">π</a>
Positioning with <code>anchor()</code> [used in
<code>calc()</code> function]
</h2>
<div style="position: relative" class="demo-elements">
<div id="my-anchor-math" class="anchor">Anchor</div>
<div id="my-target-math" class="target">Target</div>
</div>
<p class="note">
With polyfill applied: Target's left edge is 50px left of the Anchor's
right edge). The top edge of the Target is 50px above the bottom edge of
the Anchor.
</p>
<pre><code class="language-css"
>#my-anchor-math {
anchor-name: --my-anchor-math;
}
#my-target-math {
--full-math: anchor(--my-anchor-math 100%);
--full-minus-math: calc(anchor(--my-anchor-math 100%) - 50px);
position: absolute;
top: calc(var(--full-math) - 50px);
left: var(--full-minus-math);
}</code></pre>
</section>
<section id="custom-prop" class="demo-item">
<h2>
<a href="#custom-prop" aria-hidden="true">π</a>
Positioning with <code>anchor()</code> [passed through CSS custom
property]
</h2>
<div style="position: relative" class="demo-elements">
<div id="my-anchor" class="anchor">Anchor</div>
<div id="my-target" class="target">Target</div>
</div>
<p class="note">
With polyfill applied: Target's top edge is positioned at 50% of the
height of the Anchor. The right edge of the Target lines up with 100% of
the width of the Anchor (i.e. the Anchor's right edge).
</p>
<pre><code class="language-css"
>#my-anchor {
anchor-name: --my-anchor;
}
#my-target {
--center: anchor(--my-anchor 50%);
--full: anchor(--my-anchor 100%);
position: absolute;
top: var(--center);
right: var(--full);
}</code></pre>
</section>
<section id="custom-prop-passthrough" class="demo-item">
<h2>
<a href="#custom-prop-passthrough" aria-hidden="true">π</a>
Positioning with <code>anchor()</code> [passed through multiple CSS
custom properties]
</h2>
<div style="position: relative" class="demo-elements">
<div id="my-anchor-props" class="anchor">Anchor</div>
<div id="my-target-props" class="target">Target</div>
</div>
<p class="note">
With polyfill applied: Target's left edge is positioned in Anchor's
horizontal center. Target's bottom edge is 10% above the bottom edge of
Anchor.
</p>
<pre><code class="language-css"
>#my-anchor-props {
anchor-name: --my-anchor-props;
}
#my-target-props {
--half: anchor(--my-anchor-props 50%);
--quarter: calc(var(--half) / 2);
--tenth: calc(var(--quarter) / 2.5);
position: absolute;
left: var(--half);
bottom: var(--tenth);
}</code></pre>
</section>
<section id="duplicate-custom-props" class="demo-item">
<h2>
<a href="#duplicate-custom-props" aria-hidden="true">π</a>
Positioning with <code>anchor()</code> [with duplicate CSS custom
properties]
</h2>
<div style="position: relative" class="demo-elements">
<div id="anchor-duplicate-custom-props" class="anchor">Anchor</div>
<div id="target-duplicate-custom-props" class="target">Target</div>
</div>
<p class="note">
With polyfill applied: Target's top left corner is positioned in the
center (vertically and horizontally) of the Anchor.
</p>
<pre><code class="language-css"
>#anchor-duplicate-custom-props {
anchor-name: --anchor-duplicate-custom-props;
}
#target-duplicate-custom-props {
--center: anchor(--anchor-duplicate-custom-props 50%);
position: absolute;
top: var(--center);
left: var(--center);
}
#other {
--center: anchor(--anchor-duplicate-custom-props 100%);
}</code></pre>
</section>
<section id="anchor-size" class="demo-item">
<h2>
<a href="#anchor-size" aria-hidden="true">π</a>
Sizing with <code>anchor-size()</code>
</h2>
<div style="position: relative" class="demo-elements">
<div id="my-anchor-size" class="anchor">Anchor</div>
<div id="my-target-size" class="target">Target</div>
</div>
<p class="note">
With polyfill applied: Target has the same width as the Anchor.
</p>
<pre><code class="language-css"
>#my-anchor-size {
anchor-name: --my-anchor;
width: 5em;
}
#my-target-size {
width: anchor-size(--my-anchor width);
}</code></pre>
</section>
<section id="anchor-update" class="demo-item">
<h2>
<a href="#anchor-update" aria-hidden="true">π</a>
Dynamically update anchors
</h2>
<div style="position: relative" class="demo-elements">
<div id="my-anchor-update" class="anchor">Anchor</div>
<div id="my-target-update" class="target">Target</div>
</div>
<button id="toggle-anchor-width">Toggle anchor width</button>
<p class="note">
With polyfill applied: Target and Anchor's right edges line up. Target's
top edge lines up with the bottom edge of the Anchor.
<br />
<br />
When Anchor width is changed dynamically, Target position updates
accordingly.
</p>
<pre><code class="language-css"
>#my-anchor-update {