-
Notifications
You must be signed in to change notification settings - Fork 709
[selectors-4] need "first-matching-sibling" combinator #3813
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
Comments
What is the difference between |
<div><p id=p1><p id=p2><p id=p3><p id=p4></div> .p2 ~2 p would choose .p4 ~N chooses the Nth matching sibling, not the sibling that happens to be Nth child of its parent, if that Nth child happens to also be a sibling of the left element. |
In many cases, this could be handled by the "nth of" syntax. However, that selector would fail if you had more than one To get the behavior that @v-python wants, the "nth of" counting would need to be applied to each sibling combinator, rather than counting across all children of a parent that also happen to match the any-sibling selector. Modifying the So I'd lean towards extending the But, extending the syntax to sibling relationships isn't straightforward. The scope of "child" is defined by the DOM tree, but the scope of "sibling" needs to be assessed relative to the previous part of the selector. We'd need something that's logically more like Maybe |
Thanks for the response, Amelia. My use case isn't one of the "many" you refer to... There could be dozens of items that match the left side, each of which would want to find the first sibling that matches the right side, of which there could be dozens or even hundreds following. So your first pararaph wouldn't apply immediately, and your second is a good example of why. Interesting comment regarding how much of the right hand side applies: my use case is all on the same level, but now that you mention it, I can see it certainly could be an issue of understanding or documenattion to define the binding. + doesn't look very far, binds to only one element, and either matches or doesn't based on the rest of the RHS. ~ starts with a set (that happens to also be a list), and can just start tossing items that don't match based on the rest of the RHS. But now I understand why all the current :nth-* items modify a particular selector, instead of using a "rich combinator" type of syntax... So another syntax possibliity could be E: S1 ~ S2:nth-match( n-expression ) This would clearly define the set of S2 that follows S1 as now, but would limit the scope of the counting to S2, not further to the RHS. The remainder of the RHS would simply choose whether to apply the rule or not, having identified the candidate sibling, among the set identified by the (unchanged for this syntax) ~ combinator. Does that simplify the presentation to the user, as well as limit the scoping on the RHS? |
Having a Say you wanted to style every first paragraph after a heading, even if there is other elements between: /* Using Amelias suggested syntax above */
:nth-sibling(1 of h2 / p) {
…
} <h2>Heading</h2>
<img />
<p>This paragraph should get a custom style.</p>
<p>This paragraph should be unaffected.</p> It would also be useful if it was possible to limit how far the selector applies with another selector and not just Say that you want to apply different styles to elements after even and odd headings: /* The sibling selector applies only until the next heading */
:nth-sibling(n of :nth-child(odd of h2) / p until h2) {
color: green;
}
:nth-sibling(n of :nth-child(even of h2) / p until h2) {
color: blue;
} <h2>Odd Heading</h2>
<img />
<p>Paragraphs after odd headings should be green.</p>
<p>…</p>
<h2>Even Heading</h2>
<img />
<p>Paragraphs after even headings should be blue.</p>
<p>…</p> |
I just wrote an article related to this: https://blog.kizu.dev/nth-sibling-christmas-tree/ The need for this kind of selector is something I stumble upon regularly, and it could be really nice if there would be some way of achieving it, be it via a new pseudo-class like |
Note that there is now a separate issue for a sibling scope at-rule named If @scope-siblings (.sibling) {
:nth-sibling(1 of .target) {…}
}
@scope-siblings (.sibling) to (:nth-sibling(5)) {
…
} |
When it comes to use cases, I think the arguments for having With If you need to apply custom styling to the first and/or last paragraph of an element? Use |
The current sibling combinators are:
~ select any following siblings that match
Neither of these can be used to select the first following sibilng that matches. Or the 2nd. I only need the first, in my current application, but could imagine that someone might need the Nth.
This type of selection becomes useful when dealing with a grid containing hundreds or thousands of child elements of different types or classes. Yep, I have such. So the relationships between siblings become far more important when siblings become far more numerous.
It doesn't seem that one can apply the currently defined :first-* to the subset of siblings selected by ~, as those all apply based on parentage, not a selected subgroup.
Some syntax ideas:
A: S1 ~1 S2
S1 ~2 S2
...
B: some other special character than ~ to just mean first, if Nth doesn't sound useful.
C: some new pseudo-class that selects among a group :first-that-matches( selector ), nth-that-matches( selector ), :last-that-matches( selector ), :nth-last-that-matches( selector ). In this syntax, one would write S1 ~ :first-that-matches( S2 )
D: S1 ~:first S2
S1 ~:nth( n-expression ) S2
S1 ~:last S2
S1 ~:nth-last( n-expression ) S2
The text was updated successfully, but these errors were encountered: