Skip to content

Commit

Permalink
Merge pull request #792 from oddbird/anchor-valid-edit
Browse files Browse the repository at this point in the history
Update top layer examples
  • Loading branch information
jamesnw authored Jan 29, 2025
2 parents 6a69453 + 9d0493f commit 2ae7cdd
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions content/blog/2025/anchor-position-validity.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Here's why your anchor positioning isn't working
title: Here's Why Your Anchor Positioning Isn't Working
sub: How to find an anchor element
date: 2025-01-29
image:
Expand Down Expand Up @@ -208,15 +208,7 @@ the anchor element is in a [higher top
layer](https://drafts.csswg.org/css-position-4/#top-layer) than the positioned
element, the positioned element will not be able to locate the anchor.

This area needs additional investigation. My reading of the spec says that an
element _inside_ a higher top layer (for instance, a button inside a dialog) can
be anchored to an element in a lower layer. However, I can't seem to get that to
work, and I'm not sure it should. The containing block of the dialog or popover
might not overlap with the anchor, so it's not clear to me how the top layer
would handle overflow.

What _does_ work is anchoring the dialog or popover itself to an element in the
bottom layer.
You can position the root popover or dialog directly using `position: absolute`.

{{ embed.codepen(
id='OPLzjJq',
Expand All @@ -226,6 +218,19 @@ bottom layer.
tab='html,result'
) }}

However, if you want to position an element that is inside the popover or
dialog, you will need to use `position: fixed`. Note that the positioned
elements are not inside their parents in this example -- `position: fixed` moves
the element's containing block to the viewport and allows positioning to work.

{{ embed.codepen(
id='vEBbVXq',
title='Top Layer - inside',
user='jamessw',
height=250,
tab='html,result'
) }}

<h3 id="anchoring-across-shadow-trees">Anchoring across shadow trees</h3>

An element in one tree can anchor to an element in another tree, as long as the
Expand Down

0 comments on commit 2ae7cdd

Please sign in to comment.