You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[css-selectors] Proposal for allowing selectors that depend on layout (:stuck, :snap, :on-screen, etc) #5979
Background:
Proposals like :snap and :stuck have been rejected because the layout could be changed on the matched element, thus undoing what was done, which then redoes the effect.. forever. This can be seen in :hover effects that change layout and cause the mouse to go out and in of the element as fast as the browser can draw it all. Not good. Reasonable reason to reject the ideas.
Proposal:
Layout dependent selectors can only be selector participants and never the subject.
/* wouldn't work, not allowed */:stuck{position: static;box-shadow:01ex1chhsl(00%0%/25%);}/* would work */:stuck>nav{position: static;box-shadow:01ex1chhsl(00%0%/25%);}
The cycle is heavily mitigated or entirely eliminated by not allowing the layout relevant selector to target the element with the desired layout pseudo class. We provide the hook, but prevent the hook from changing itself. Thoughts?
Conclusion:
Instead of attempting some list of approved styles for layout dependent pseudo classes, 1 rule could help prevent tons of footguns while simultaneously unlocking many new effects. The catch is that developers will need an extra node in the component so the layout effect and the styles are separated (aka: the element being stuck isn't what has the box-shadow).
I don't think this restriction is enough. Being able to select the contents means that you can affect the size of the sticky element through auto sizes, and thus whether it's stuck or not. See demo, which uses JS to crudely polyfill :stuck as .stuck, and only uses it in #sticky.stuck > *. It flickers since .stuck keeps toggling.
This circularity may be more obvious for similar proposals like :overflowed-content (#2011). Changing the contents can clearly affect whether the element has overflowing contents.
agree that :overflowed-content isn't a candidate here 👍🏻
demo makes sense, bottom: 0 was a good position to expose. this proposal definitely isn't bullet proof, but feels like you need to get creative to find a way to break it. thanks for sharing your creativity.
What about adding the additional constraint that :stuck can only match elements with size containment? I guess this would break the dependence on the contents.
Reacted by Adam Argyle, Ilya Streltsyn, jonjohnjohnson and Lea Verou
Also, I guess that there should be a child or descendant combinator (or pseudo-element?) after :stuck. + or ~ are not enough because if :stuck can impact later siblings, they can affect the size of the containing block, which can affect whether the element is stuck. Alternatively, the containing block should also have size containment.
And mixing with :has() should be invalid, or :has(> :is(:stuck > *)) would bypass the restriction.
@Loirooriol interested in your thoughts regarding ways to make a child not snap given this proposal?
if :snap is only featured on children with scroll-snap-align: start, this proposal certainly prevents the snap-align value from being changed cyclically. Are there clever ways to change snapped items children contents insomuch they unsnap from the parent?
No, in fact the spec is pretty explicit about staying snapped to an element as the page mutates; only user action or unrecoverable mutations (like the snapped element being removed) should change the snap.
By itself, size containment does not offer much optimization opportunity. Its primary benefit on its own is that tools which want to lay out the containment box's contents based on the containment box's size (such as a JS library implementing the "container query" concept) can do so without fear of "infinite loops", where having a child’s size respond to the size of the containment box causes the containment box's size to change as well, possibly triggering further changes in how the child sizes itself and possibly thus more changes to the containment box's size, ad infinitum.
Sounds like this proposal (adding pseudo classes that can only be a selector member) in combination with contain: layout which "nothing outside can affect its internal layout, and vice versa" as suggested by Oriol, would:
restrict all mutations to children
restrict all children mutations to not affect the container box
Those conditions seem like they do remove the infinite potentials, complimenting each other. Creates an effect similar to how position: relative results, that it's original box is still there but it's off doin whatever. In this new case, given new current browser state, a box is styled different but without modifying the original box space / page layout. A stuck header could freely change height due to children changing and not cause an infinite loop.
I wonder if instead of a pseudo-class with some syntax restrictions, this could work with container queries? See #5989 "What container features can be queried?". Maybe whether the container is a sticky element in a stuck state?
If :overflow would mean that the element overflows its containing block, then yes, I think that limiting the effect to the descendants and requiring size containment could do the trick.
Reacted by Adam Argyle, Miriam Suzanne, Sebastian Zartner and jonjohnjohnson
Later in that thread, @upsuperpointed out that events are working synchronously and would therefore slow down the layout. The suggestion back then was to use something asynchronous like an Observer.
Anyway, the discussion here is about a pure CSS way to handle those use cases. And the proposal @argyleink and @Loirooriol came up with sounds very promising to me so far. Also, it is orthogonal to any solution that involves JavaScript, i.e. they don't exclude each other.
Very clever idea, indeed this addresses most use cases!
However, do note that this is based on the fact that compound selectors before the subject can never target the same element as the subject. This is true today, but may not be true in the future. If we start adding pseudo-classes that avoid circularity by depending on this fact, it could severely restrict the types of new combinators we can add.
For example, there have been discussions about a preceding sibling combinator possibly being implementable (i.e. the opposite of +, let's call it <+ for the purposes of this discussion). This would re-introduce circularity, as you could do .foo:stuck + .bar <+ .foo.
Also, remember the reference combinator? Among other things, it allowed authors to target labels of elements, i.e. label /for/ input. Depending on the placement of the label, you could do input:stuck + label /for/ input to apply the pseudo-class to the input itself. It was dropped due to lack of implementor interest, but this would prevent it and anything like it from ever happening.
We may well decide that the value these pseudo-classes add outweighs the value of combinators that could go backwards. However, it should be a conscious decision to accept that tradeoff, and we should first explore variations of this proposal that don't force us into adding this restriction.
To handle that case it could be defined that the layout depending selector excludes the element from being the subject. In other words, .foo:stuck + .bar <+ .foo or input:stuck + label /for/ input would not match the stuck elements.
Quick note that I've started working on these features as part of container queries (see #5989). If this selector approach is viable, of course, it's certainly a less restrictive path for authors. I don't think there is any reason we need both, right? If we do create the selector, then the query would be redundant? I guess this is a "subscribe" comment, to keep an eye on this conversation before going too deep on the other.
I'm def excited about the container queries approach to some of these selectors! I have though started ideating a spec for :snap in the form of :snapped-target (and friends) over here. Would love feedback. It in particular doesn't feel as connected to the container as the others. Open for discussion!
I wanted to add the following link here from the Chrome Developers Blog as it did help me to current workaround this issue about the missing :stuck selector: https://developer.chrome.com/blog/sticky-headers/. In combination with CustomStateSet API which is sadly not yet supported by firefox it even could a :--stuck be achieved.
@lilles What is the status of your state queries explainer/proposal and prototype? Is that something we could try to get agreement on, and begin to specify?
@lilles What is the status of your state queries explainer/proposal and prototype? Is that something we could try to get agreement on, and begin to specify?
Yes, we resolved to create css-contain-4 and add scroll-state(), so we can start. I've just been very busy with anchor pos the last months. My next step is to finish the 'snapped' prototype.
(I am querying the status of a state query)
After the resolution in #6402 (comment) I moved the explainer to and renamed the function to scroll-state().
Reducing container-type to a single one (scroll-state) was discussed, but not resolved. But since I changed it in the prototype I should reflect that in the explainer.
It would be great to get these behind a UI flag, for author testing.
Sticky queries are enabled if you enable chrome://flags/#enable-experimental-web-platform-features
Reacted by Miriam Suzanne, Yisi and Michaël Vanderheyden
Hello hello! While I deeply appreciate all the moves towards a more powerful CSS, I'm a little bit worried that the pseudo-class approach has been rejected (#1656) because a drawback of the container query approach is that it’s very cumbersome to target of the stuck element.
While the purpose of @container scroll-state(stuck: top) is to say “One of my children with position: sticky; is stuck!”, the purpose of a :stuck (or :scroll-state(stuck: top)) pseudo-class is to directly target that stuck element. Without a pseudo, you have no idea which of the children is stuck.
If you have a list with dozens of items (e.g. ul > li.card * 80) where the stuck item should receive a shadow, you end up creating dozens of containers while there’s only 1 scrollable area (ul) so that you can @container scroll-state(stuck: top) and effectively target the stuck element (and not all cards at once 😨).
Furthermore, if you want that .card item to be the HTML element receiving the shadow, you also need 1 extra HTML wrapper for each card, since you can only access the children of the container inside @container.
With a :stuck (or :scroll-state(stuck: top)) pseudo-class , we could directly target the stuck element with no extra container (only the one on ul would be needed), and no extra markup neither because the pseudo would target the stuck item, and not an unknown child.
If the technical challenges for a pseudo-class are still out of reach (seems like it was tricky in 2021… but maybe the experience since then is telling something else), maybe a technically possible improvement towards a simpler usage for code authors would be to couple :stuck with a container so it would only work for elements inside a container?
Closing this one as the orignally requested :stuck and :snapped can be done with scroll-state queries. The discussion for :on-screen (~ :in-viewport) can continue in #10582 which is concerned with that.
@meduzen You're right to note the difference between targeting the stuck element directly vs the children of the stuck element. But the cause and effect is backwards. The reason we decided to use container queries here is because it would cause issues to target the stuck element itself. If it was possible to target the element directly, without causing layout loops, then a pseudo-class would have been a good option. Container queries give us one level of separation, and the ability to add essential 'containment' to the stuck element – so that changes within the query are not able to change the result of the query.
I think we all understand that it would be nicer if it wasn't required, but the re-targeting to children is what makes this feature possible.
Activity
Loirooriol commentedon Feb 11, 2021
I don't think this restriction is enough. Being able to select the contents means that you can affect the size of the sticky element through auto sizes, and thus whether it's stuck or not. See demo, which uses JS to crudely polyfill
:stuckas.stuck, and only uses it in#sticky.stuck > *. It flickers since.stuckkeeps toggling.This circularity may be more obvious for similar proposals like
:overflowed-content(#2011). Changing the contents can clearly affect whether the element has overflowing contents.argyleink commentedon Feb 11, 2021
agree that
:overflowed-contentisn't a candidate here 👍🏻demo makes sense,
bottom: 0was a good position to expose. this proposal definitely isn't bullet proof, but feels like you need to get creative to find a way to break it. thanks for sharing your creativity.Loirooriol commentedon Feb 11, 2021
What about adding the additional constraint that
:stuckcan only match elements with size containment? I guess this would break the dependence on the contents.Loirooriol commentedon Feb 13, 2021
Also, I guess that there should be a child or descendant combinator (or pseudo-element?) after
:stuck.+or~are not enough because if:stuckcan impact later siblings, they can affect the size of the containing block, which can affect whether the element is stuck. Alternatively, the containing block should also have size containment.And mixing with
:has()should be invalid, or:has(> :is(:stuck > *))would bypass the restriction.argyleink commentedon Feb 18, 2021
@Loirooriol interested in your thoughts regarding ways to make a child not snap given this proposal?
if
:snapis only featured on children withscroll-snap-align: start, this proposal certainly prevents the snap-align value from being changed cyclically. Are there clever ways to change snapped items children contents insomuch they unsnap from the parent?Loirooriol commentedon Feb 18, 2021
No idea, I know very little about snapping
tabatkins commentedon Feb 18, 2021
No, in fact the spec is pretty explicit about staying snapped to an element as the page mutates; only user action or unrecoverable mutations (like the snapped element being removed) should change the snap.
argyleink commentedon Feb 22, 2021
The containment requirement seems promising: containment spec quote
Sounds like this proposal (adding pseudo classes that can only be a selector member) in combination with
contain: layoutwhich "nothing outside can affect its internal layout, and vice versa" as suggested by Oriol, would:Those conditions seem like they do remove the infinite potentials, complimenting each other. Creates an effect similar to how
position: relativeresults, that it's original box is still there but it's off doin whatever. In this new case, given new current browser state, a box is styled different but without modifying the original box space / page layout. A stuck header could freely change height due to children changing and not cause an infinite loop.This would also unlock
:overflowas well?Loirooriol commentedon Feb 22, 2021
I wonder if instead of a pseudo-class with some syntax restrictions, this could work with container queries? See #5989 "What container features can be queried?". Maybe whether the container is a sticky element in a stuck state?
If
:overflowwould mean that the element overflows its containing block, then yes, I think that limiting the effect to the descendants and requiring size containment could do the trick.wle8300 commentedon Mar 19, 2021
How about using JavaScript Events?
This was discussed in #1660 (comment) back in 2017, and it's still open
SebastianZ commentedon Mar 20, 2021
Later in that thread, @upsuper pointed out that events are working synchronously and would therefore slow down the layout. The suggestion back then was to use something asynchronous like an Observer.
Anyway, the discussion here is about a pure CSS way to handle those use cases. And the proposal @argyleink and @Loirooriol came up with sounds very promising to me so far. Also, it is orthogonal to any solution that involves JavaScript, i.e. they don't exclude each other.
Sebastian
wle8300 commentedon Mar 20, 2021
@SebastianZ I see. Thx for the simple explanation!
LeaVerou commentedon Mar 21, 2021
Very clever idea, indeed this addresses most use cases!
However, do note that this is based on the fact that compound selectors before the subject can never target the same element as the subject. This is true today, but may not be true in the future. If we start adding pseudo-classes that avoid circularity by depending on this fact, it could severely restrict the types of new combinators we can add.
For example, there have been discussions about a preceding sibling combinator possibly being implementable (i.e. the opposite of
+, let's call it<+for the purposes of this discussion). This would re-introduce circularity, as you could do.foo:stuck + .bar <+ .foo.Also, remember the reference combinator? Among other things, it allowed authors to target labels of elements, i.e.
label /for/ input. Depending on the placement of the label, you could doinput:stuck + label /for/ inputto apply the pseudo-class to the input itself. It was dropped due to lack of implementor interest, but this would prevent it and anything like it from ever happening.We may well decide that the value these pseudo-classes add outweighs the value of combinators that could go backwards. However, it should be a conscious decision to accept that tradeoff, and we should first explore variations of this proposal that don't force us into adding this restriction.
SebastianZ commentedon Mar 21, 2021
To handle that case it could be defined that the layout depending selector excludes the element from being the subject. In other words,
.foo:stuck + .bar <+ .fooorinput:stuck + label /for/ inputwould not match the stuck elements.Sebastian
7 remaining items
mirisuzanne commentedon Aug 19, 2021
Quick note that I've started working on these features as part of container queries (see #5989). If this selector approach is viable, of course, it's certainly a less restrictive path for authors. I don't think there is any reason we need both, right? If we do create the selector, then the query would be redundant? I guess this is a "subscribe" comment, to keep an eye on this conversation before going too deep on the other.
argyleink commentedon Aug 26, 2021
I'm def excited about the container queries approach to some of these selectors! I have though started ideating a spec for
:snapin the form of:snapped-target(and friends) over here. Would love feedback. It in particular doesn't feel as connected to the container as the others. Open for discussion!Handle cases where the selection is more than can fit on a screen.
alexander-schranz commentedon Jun 30, 2022
I wanted to add the following link here from the Chrome Developers Blog as it did help me to current workaround this issue about the missing :stuck selector: https://developer.chrome.com/blog/sticky-headers/. In combination with CustomStateSet API which is sadly not yet supported by firefox it even could a
:--stuckbe achieved.Link2Twenty commentedon Jul 19, 2022
Am I correct in my assumption that this is the style of syntax suggested if we go the container query route?
If so it feels like a good compromise and opens up the potential for lots of states to exist on a container (for better or worse).
yisibl commentedon Aug 24, 2023
https://lilles.github.io/explainers/state_container_queries.html
trusktr commentedon Apr 17, 2024
I wanted
:stuck { /* ... box shadow ... */ }forposition:stickycontent, then Google led me here. It would be great to be able to do this.mirisuzanne commentedon Apr 17, 2024
@lilles What is the status of your state queries explainer/proposal and prototype? Is that something we could try to get agreement on, and begin to specify?
(I am querying the status of a state query)
argyleink commentedon Apr 17, 2024
in Canary (with cli flag
CSSStickyContainerQueries) you can prototype adding a shadow with position sticky, it looks like this:Codepen proto
mirisuzanne commentedon Apr 17, 2024
It would be great to get these behind a UI flag, for author testing.
lilles commentedon Apr 19, 2024
Yes, we resolved to create css-contain-4 and add scroll-state(), so we can start. I've just been very busy with anchor pos the last months. My next step is to finish the 'snapped' prototype.
After the resolution in #6402 (comment) I moved the explainer to and renamed the function to scroll-state().
Reducing container-type to a single one (scroll-state) was discussed, but not resolved. But since I changed it in the prototype I should reflect that in the explainer.
Sticky queries are enabled if you enable chrome://flags/#enable-experimental-web-platform-features
meduzen commentedon Jan 17, 2025
Hello hello! While I deeply appreciate all the moves towards a more powerful CSS, I'm a little bit worried that the pseudo-class approach has been rejected (#1656) because a drawback of the container query approach is that it’s very cumbersome to target of the stuck element.
While the purpose of
@container scroll-state(stuck: top)is to say “One of my children withposition: sticky;is stuck!”, the purpose of a:stuck(or:scroll-state(stuck: top)) pseudo-class is to directly target that stuck element. Without a pseudo, you have no idea which of the children is stuck.If you have a list with dozens of items (e.g.
ul > li.card * 80) where the stuck item should receive a shadow, you end up creating dozens of containers while there’s only 1 scrollable area (ul) so that you can@container scroll-state(stuck: top)and effectively target the stuck element (and not all cards at once 😨).Furthermore, if you want that
.carditem to be the HTML element receiving the shadow, you also need 1 extra HTML wrapper for each card, since you can only access the children of the container inside@container.With a
:stuck(or:scroll-state(stuck: top)) pseudo-class , we could directly target the stuck element with no extra container (only the one onulwould be needed), and no extra markup neither because the pseudo would target the stuck item, and not an unknown child.If the technical challenges for a pseudo-class are still out of reach (seems like it was tricky in 2021… but maybe the experience since then is telling something else), maybe a technically possible improvement towards a simpler usage for code authors would be to couple
:stuckwith a container so it would only work for elements inside a container?Could the same possibility of relaxation to avoid circularity than the one recently done for layout containment be a thing for scrollable areas, too?
(Sorry if I’m saying nonsense. You're the spec experts. :p)
bramus commentedon Aug 14, 2025
Closing this one as the orignally requested
:stuckand:snappedcan be done with scroll-state queries. The discussion for:on-screen(~:in-viewport) can continue in #10582 which is concerned with that.mirisuzanne commentedon Aug 14, 2025
@meduzen You're right to note the difference between targeting the stuck element directly vs the children of the stuck element. But the cause and effect is backwards. The reason we decided to use container queries here is because it would cause issues to target the stuck element itself. If it was possible to target the element directly, without causing layout loops, then a pseudo-class would have been a good option. Container queries give us one level of separation, and the ability to add essential 'containment' to the stuck element – so that changes within the query are not able to change the result of the query.
I think we all understand that it would be nicer if it wasn't required, but the re-targeting to children is what makes this feature possible.