-
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][css-position] Add the “centering” behavior which is now defined as an example in the specs as something built-in #8979
Comments
This actually seems to be the most feasible idea to me. We just need to add a new anchor value, e.g.:
which automatically unfolds into
then we can achieve centering with two lines of CSS inset-inline: anchor(center-align);
justify-self: center; /* The proper way to do centering in positioned layout, but lacking browser support */
margin-inline: auto; /* Fallback if the browser doesn't support it */
To make it compatible, it has to be a new keyword value, something like
That's an interesting idea. It's also more general than anchor positioning that it should go to the CSS Positioned Layout module instead. |
Agree about the feasibility — and it could be a good start in absence of other ways to achieve this, even with the Started reading the working draft from scratch, and the first example really falls apart when it comes to centering. First all is good: we have a few declarations with very simple values, but then, suddenly, we get a block of declarations with calculations and a hard-coded values: left: clamp(0px, anchor(center) - 150px, 100% - 300px);
right: clamp(0px, anchor(center) - 150px, 100% - 300px);
max-width: 300px; With this being the first example (and the tooltips/popovers centered around their anchors being one of the most prominent use cases for anchor positioning overall), it would be really nice to somehow address this in the specs. cc @tabatkins |
The issue with centering is indeed that the inset properties don't actually align a point of the element, they create an area that you size/position into. When aligning one of the box corners you can generally conflate the two, but centering exposes the difference. The way to actually center thus is to size and align the positioning area such that it's centered on the anchor; then you can center-align the positioned element with justify-self:center. This is easy to do naively; the more complicated examples are baking in the additional constraints that it not overflow the CB, thus the min()/etc functions. (The one with 300/150 is additionally doing some work to ensure the box shifts off-center if that's needed to keep it 300px wide.) Also, you can generally do these things more simply if you're not relying on fallback and are okay with it overflowing the "inset-modified containing block". (The @xiaochengh's idea works, tho we might want to give it slightly more options. Happy to start from the simple version that just gives you the "largest possible centered area", tho. |
Yep, I think even that would drastically improve things! |
Brought up in the CSSWG meeting right now is the idea of an Then the code would just be |
Dropped a PR for this proposal in #9136. |
This is not the same as “circular center alignment” in #1849 but somewhat related, so I’m leaving a reference to it anyway. ;) |
So to clarify, is this correct? Current spec: .centered-anchor {
position: absolute;
anchor-default: --myAnchor;
left: anchor(center);
translate: -50% 0;
} New proposal with .centered-anchor {
position: absolute;
anchor-default: --myAnchor;
left: 0;
right: 0;
align-self: anchor-center;
} |
You can actually omit the left/right as well - anchor-center makes auto insets resolve to 0. So it's just
|
Ok nice, that makes sense to me and looks a lot cleaner 🙂 Big +1 |
The CSS Working Group just discussed
The full IRC log of that discussion<dael_> TabAtkins: The very first example in the spec shows a common use case. Center positioned element over anchor with a bit of safety<dael_> TabAtkins: If centering would overflow CB we shift you a little off center. <dael_> TabAtkins: Cool you can express this, but it's a bit complicated. We had an issue to make it easier <dael_> TabAtkins: Luckily in grid-baed proposal there was an alternative. A new self-alignment value 'anchor-center' <dael_> TabAtkins: If you're using anchor positioning and are abspos it will attempt to center you over the element in the axis you spec. If strict center causes overflow we'll shift a bit <dael_> TabAtkins: I have a PR with spec text. <dael_> TabAtkins: To decide on- live in anchor positioning or alignment? I put in anchor positioning <dael_> TabAtkins: few more detailed questions on exact mechanics if no general concerns <dael_> TabAtkins: Opening the floor for questions <florian> q? <dael_> astearns: Seeing +1 in issue. Other opinions <fantasai> +1 ot adding it, but I haven't had time to review the spec text <miriam> +1 <una> +1 <dael_> dholbert: Took a quick look and it makes sense to me. Nice to have easy syntax to do the thing people will want <chrishtr> +1 <florian> +1 to the idea, didn't review the PR yet. <changseok> +1 <dael_> astearns: I do think makes sense to leave in anchor positioning for now since defined based on anchor properties <dael_> astearns: Lots of +1s <dael_> astearns: Objections to adding the anchor-center value <dael_> RESOLVED: add the anchor-center value <dael_> astearns: You said there were details. Would it make sense to get PR in and raise separate issues? <dael_> TabAtkins: That would be good |
(Edit: This comment is not about whether to have Discussed with @tabatkins offline. Unfortunately, the way how More work is needed. |
.centered-anchor {
position: absolute;
anchor-default: --myAnchor;
left: anchor(center);
translate: -50% 0;
} One potential benefit of no longer needing |
Should be addressed by the current draft spec. |
The example in the specs that describes using math functions for the anchors involves a rather complicated calculation:
https://drafts.csswg.org/css-anchor-position-1/#example-a7ac0832
All of this to attempt to align the element and the anchor alongside their centers, and the example in the specs is not even enough: for a good result we need to add an extra element inside and use flex alignment:
https://codepen.io/kizu/pen/wvYYLGr?editors=1100
To quote @o-t-w from the OpenUI discord,
I previously attempted to do this on a single element with
margin: auto
, but this did not give a good result as it lead to negative margins in some cases. Similar to if we would usetranslateX(-50%)
, a single element solution won't work well with the fallbacks as well.This is one of the very very common things developers do, and usually end up having to add wrappers to use flex/grid alignment, use margins (which can become negative), or transform (which creates subpixel rendering issues).
I think it is time to add something that would enable these use cases to CSS, either as a part of anchor positioning, or a part of the
css-position
spec in general.Some ideas:
anchor-size(self width)
only when the width is not auto? this way, settingwidth: max-content;
would unlock the self-anchor-size, allowing doing calculations for centering much much easier. I think I would prefer this solution, unless there are other limitations not related to the auto-sizing.inset-center
or something, to align the element based on its center? In this case we'd need to update how the auto-sizing would work (probably disable it?), and how it would interplay with thestart
andend
if they're set. Probably use them as “limits”, so if we'd say something likeinset-inline-center: 100px
alongside withinset-inline-start: 0px
, then we would try to position the element so its center would be at the 100px coordinate, but if itsstart
would go beyond the0px
mark, we stop it at it. Overall, I like this option, but it seems to be much more complicated and would require more spec work than option 2.Overall, it is nice that we can kinda align things with math functions, but, given this is a very common case, it would be really nice to have something build-in that would help the developers.
The text was updated successfully, but these errors were encountered: