Skip to content

Commit 9f7f7d8

Browse files
committed
[css-anchor-position] Rewrite intro, add an example.
1 parent 35e1af4 commit 9f7f7d8

File tree

1 file changed

+48
-13
lines changed

1 file changed

+48
-13
lines changed

css-anchor-position-1/Overview.bs

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,54 @@ spec:css-cascade-5; type:dfn; text:property
4242
Introduction {#intro}
4343
=====================
4444

45-
While CSS generally determines the position and size of elements
46-
according to their parents or other ancestors,
47-
[=absolutely positioned=] elements barely participate in their ancestors' layout.
48-
Instead, they're sized and positioned explicitly
49-
by the [=inset properties=] and [=box alignment properties=],
50-
only referencing the final size and position of their [=containing block=].
51-
This provides extreme flexibility,
52-
allowing elements to be positioned more or less arbitrarily,
53-
including over the top of other elements
54-
in ways that the layout methods don't otherwise allow,
55-
but in return it's not very expressive--
56-
the element cannot easily express its size and position
57-
in terms of other elements on the page.
45+
CSS [=absolute positioning=] allows authors
46+
to place elements anywhere on the page,
47+
without regard to the layout of other elements
48+
besides their containing block.
49+
This flexibility can be very useful,
50+
but also very limiting--
51+
often you want to position relative to <em>some</em> other element.
52+
<dfn export>Anchor positioning</dfn> allows authors to achieve this,
53+
"anchoring" an [=absolutely-positioned=] element
54+
to one or more other elements on the page,
55+
while also allowing them to try several possible positions
56+
to find the "best" one that avoids overlap/overflow.
57+
58+
For example, an author might want to position a tooltip
59+
centered and above the targeted element,
60+
unless that would place the tooltip offscreen,
61+
in which case it should be below the targeted element.
62+
This can be done with the following CSS:
63+
64+
<div class=example>
65+
<pre class=lang-css>
66+
.anchor {
67+
anchor-name: --tooltip;
68+
}
69+
.tooltip {
70+
/* Fixpos means we don't need to worry about
71+
containing block relationships;
72+
the tooltip can live anywhere in the DOM. */
73+
position: fixed;
74+
75+
/* Align the tooltip's bottom to the top of the anchor,
76+
but automatically swap if this overflows the window
77+
to the tooltip's top aligns to the anchor's bottom
78+
instead. */
79+
bottom: anchor(--tooltip auto);
80+
81+
/* Set up a 300px-wide area, centered on the anchor.
82+
If centering would put part of it off-screen,
83+
instead clamp it to remain on-screen. */
84+
left: clamp(0px, anchor(--tooltip center) - 150px, 100% - 300px);
85+
right: clamp(0px, anchor(--tooltip center) - 150px, 100% - 300px);
86+
max-width: 300px;
87+
88+
/* Center the tooltip in that area. */
89+
justify-self: center;
90+
}
91+
</pre>
92+
</div>
5893

5994
Anchoring {#anchoring}
6095
==================================

0 commit comments

Comments
 (0)