-
Notifications
You must be signed in to change notification settings - Fork 677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[css-anchor-position-1] Better reusability of anchor names #9045
Comments
One thing we can currently do to scope the anchor names is to use Interestingly, this does not work if we add the However, as I did mention in #8588 (this issue could be considered a duplicate of it, maybe?), I don't like this implicit behavior of Playing with scroll-driven animations, I did appreciate how it had the ability to define explicit scopes via So, my proposal: what if we had an I imagine it work like this:
(This is a very quick draft; I imagine there can be other use cases to consider, like “do we want to have an inversion of the |
While it's a tree-scoped reference, it doesn't have to be unique for the document. It's scoped to the containing block of the anchor-positioned element. That being said, this scope is still the entire tree scope for popovers, so I can see there's still some difficulty here.
This is expected. We can't use the anchor name on the containing block itself. Btw, if you want it anchored to the containing block, isn't it just the good old positioned layout (without |
Yeah, as @xiaochengh said, this is incorrect. Tree-scoped-ness is just about how the names and references work with shadow trees, it says nothing about uniqueness. But also, yeah, the behavior for multiple valid anchors with the same name won't actually help you here, since all of the anchors are in scope for all of the tooltips here.
In theory, tho you'll be limited to what plain positioning can do. At least in this case, putting the abspos "below the element" is easy with a
I forget why this is the case - is it because the positioned element can trigger scrollbars on the containing block, and thus we don't have the "laid out strictly after" condition, @xiaochengh ?
I suspect some simple way to say "look for closest ancestor with the given anchor-name" (rather than "last element in the page with the given anchor-name") would address the common case here. Probably can bake it into the |
Hm, re: "closest" tho, we probably want to allow both for "positioned element is inside of its anchor" and "positioned element is right after its anchor". I think both markup patterns are reasonable, depending on what you're doing. It's probably okay to just conflate the two, and have the search first look for preceding siblings, then look for ancestors? |
Almost, but we couldn't use the
Ah, I see how the scrollbars on the containing block could prevent this from working, yes. If the scrollbars are the only case, I wonder if there is a way to solve this by assuming that they're absent only for this particular case? Not ideal, and I wish there could be a better solution, but I find having this kind of an exception better than requiring adding an extra wrapper or moving the element outside its container (this won't be always possible, like when the container is a
Thinking more about this — I like the idea of a |
I don't rememeber exactly. But we already the same issue with the plain positioned layout, e.g. <style>
body {
height: 0;
overflow: auto;
}
#target {
position: absolute;
width: 100px;
height: 100px;
right: 0;
background: lime;
top: calc(100vh - 10px); /* triggers vertical scrollbar to the right*/
}
</style>
<body>
<div id=target></div>
</body> In the end, It seems that we can do the same when anchoring to CB. Just some more layout passes, what can possibly go wrong? (famous last words) |
Defining
P. S. All the containing block stuff was pretty confusing to me. Sorry 😅 |
The Anchor Positioning Exploration doc also includes an
|
Note that the mentioned
It is currently being proposed to add |
The Anchor Positioning is based on Absolute Positioning. When laying out the anchored element, the context is its containing block, which is its parent in the box tree. When querying an anchor name, we are not supposed to see anything outside the containing block. In other words, we are not supposed to see any ancestor other than the parent node. Also, the Btw, the current spec doesn't even allow using the containing block as the anchor element. So the only anchors we can use are siblings and their descendants. |
The CSS Working Group just discussed
The full IRC log of that discussion<emeyer> TabAtkins: It was brought up that right now, per spec, anchor names are whole-document visible<fantasai> proposal -> https://github.com//issues/9045#issuecomment-1698431258 <emeyer> …They don’t have to be unique, and there are rules to deal with non-unique, but they’re all document-global <emeyer> …So it’s hard to make a component that has self-contained visibility <emeyer> …We’d like to fix this; there are a number of ways to do this <emeyer> …We think we’d like to have an anchor-scope property <emeyer> …You’d designate a container that scopes internal names or all names <emeyer> …You can make a component, use a well-chosen name, and abspos will find the correct nearby anchor without grabbing something else on the page <emeyer> fantasai: This was one of the things outlined when looking through the proposal back in July, so I think it makes sense to follow timeline scope <emeyer> astearns: Do we want to create timeline-scope, anchor-scope, whatever-else-scope, or can we create an ident-scope? <bramus> +1 to what fantasai said <emeyer> TabAtkins: No. It’s hard and namespaces clash <bramus> there’s a separate issue for `timeline-scope`: https://github.com//issues/9158 <emeyer> …You could allow naming the thing you’re scoping, but then you end up with additive scope clashes <emeyer> astearns: Works for me <fantasai> +1 <emeyer> bramus: +1 to fantasai about keeping timeline-scope separate <emeyer> astearns: Proposal is to add `anchor-scope` <emeyer> TabAtkins: Yes, which works like `tineline-scope` <emeyer> astearns: We may want a central place to document all of this <emeyer> astearns: Any objections? <emeyer> (silence) <emeyer> RESOLVED: Add an `anchor-scope` property <vmpstr> vmpstr <emeyer> vmpstr: Will style containment still scope acnhors independent of this property? <emeyer> fantasai: Yes <emeyer> astearns: It would win, I believe <emeyer> fantasai: Yes |
Spec: https://drafts.csswg.org/css-anchor-position-1/
The anchor name is currently defined to be a tree scoped reference (i. e. unique for the entire document). This makes it difficult to reuse.
For example tooltips on elements are often implemented using a class or an
aria-label
attribute together with a pseudo-element holding text from that attribute.In this fiddle you can see that all 3 of the tooltips are positioned on the first element defining
anchor-name
, not on their closest ancestor as one may have expected.Anchor names being unique throughout the document forces us to explicitly name an anchor for each and every one of those elements with tooltips. And there may be hundreds of them on the page.
If
anchor-name
was not defined as unique but was scoped instead, similar to container queries (container-name
property), every element withanchor-name
would create its own anchor. But then of course what to do with anchors that are not ancestors of an anchored element?Perhaps it would be possible to introduce a modifier in the anchor selecting syntax, which will select not the first instance of an element with the given anchor name but the closest ancestor?
So, instead of:
we could write:
And instead of:
we could write:
The text was updated successfully, but these errors were encountered: