-
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
[selectors] New selector based on the amount of child elements #5694
Comments
Not sure if |
I will accept any other name as long is the functionality accepted. |
Related: #4559 (comment) proposes |
I think this can be closed as duplicate of #4559 |
It's not a duplicate! #4559 propose new functional notations (CSS values spec). This proposal is for new pseudo-class (CSS selectors spec). Those are two different things. We can discuss more to decide which approach is better but those are two different approaches. Each has its own advantages and disadvantages. Edit: we can also accept both proposals. |
Gotcha |
In that case it's the same complexity as
Edit: not quite true actually, :has descends the tree whereas this wouldn't have to. So no, not as complex. |
The new Just like |
Yeah, I agree this is not so complicated as Two things that come to mind:
Is there any chance you could elaborate of what particular styling changes you'd apply based on the number of child elements? The thing I can think of is stuff like changing the width of the parent based on number or such, but that seems brittle / repetitive and better suited by stuff like flex / grid / tables / inline-block etc... |
@emilio I have several use cases but I will elaborate on a particular example I am currently working on. I'm trying to create CSS framework turning HTML data It is very hard to create a radar chart with pure CSS when you don't know how many /* Triangle */
tbody:nth-children(3) {
clip-path: polygon(50% 0%, 0% 100%, 100% 100%);
}
/* Rhumbus */
tbody:nth-children(4) {
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}
/* Pentagon */
tbody:nth-children(5) {
clip-path: polygon(50% 0%, 100% 38%, 82% 100%, 18% 100%, 0% 38%);
}
/* Hexagon */
tbody:nth-children(6) {
clip-path: polygon(25% 0%, 75% 0%, 100% 50%, 75% 100%, 25% 100%, 0% 50%);
}
/* Heptagon */
tbody:nth-children(7) {
clip-path: polygon(50% 0%, 90% 20%, 100% 60%, 75% 100%, 25% 100%, 0% 60%, 10% 20%);
}
/* Octagon */
tbody:nth-children(8) {
clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
}
/* Nonagon */
tbody:nth-children(9) {
clip-path: polygon(50% 0%, 83% 12%, 100% 43%, 94% 78%, 68% 100%, 32% 100%, 6% 78%, 0% 43%, 17% 12%);
}
/* Decagon */
tbody:nth-children(10) {
clip-path: polygon(50% 0%, 80% 10%, 100% 35%, 100% 70%, 80% 90%, 50% 100%, 20% 90%, 0% 70%, 0% 35%, 20% 10%);
} This is not the final solution that I will use in the framework but it demonstrates the use-case of conditional design based on the total number of child elements. To do the same with alternative methods will require much more code with different thinking. |
@emilio Another example is to add an axes system to a Polar Chart which is like a Pie Chart but with equal slices. To add radial axes you need to know how many child items you have, and the new |
Selecting based on how many siblings an element has is an identical amount of work to Selecting based on how many children an element has is indeed |
Awesome idea! |
So what would be less complex and not available are:
Not sure any of these has sufficient use cases. |
Regarding complexity, we have (from hardest to easiest):
So this one seems the most feasible, and |
@tabatkins you label this as "selectors-5". But @Loirooriol wrote that |
If/when .container:has(:last-child:nth-child(3)) {
background: green;
} |
Extending on @johannesodland’s reply above, I’ve created several selectors leveraging /* At most 3 (3 or less, excluding 0) children */
ul:has(> :nth-child(-n+3):last-child) {
outline: 1px solid red;
}
/* At most 3 (3 or less, including 0) children */
ul:not(:has(> :nth-child(3))) {
outline: 1px solid red;
}
/* Exactly 5 children */
ul:has(> :nth-child(5):last-child) {
outline: 1px solid blue;
}
/* At least 10 (10 or more) children */
ul:has(> :nth-child(10)) {
outline: 1px solid green;
}
/* Between 7 and 9 children (boundaries inclusive) */
ul:has(> :nth-child(7)):has(> :nth-child(-n+9):last-child) {
outline: 1px solid yellow;
} Post with the details: https://brm.us/css-has-child-count |
Overview
Currently there are no selectors that let us apply a style based on the total amount of direct children the element has.
I can't apply red color if the element that has 2 direct children or green color if it has 3 direct children.
My proposal is to add a new pseudo-class called
:nth-children(n)
which is based on the existing naming conventions.Code Example
Lists:
Tables:
The Problem with Existing Pseudo-Class
The vast majority of the child element selectors are trying to drill up - the selector applied on the
<li>
checking against the parent<ul>
element.While my proposal is to drill down (like flex and grid) - the selector applied on the
<ul>
checking against the amount of child<li>
elements.E:first-child
An E element, first child of its parent.
E:last-child
An E element, last child of its parent.
E:only-child
An E element, only child of its parent.
E:nth-child(n [of S]?)
An E element, the n child of its parent matching S.
E:nth-last-child(n [of S]?)
An E element, the n child of its parent matching S, counting from the last one.
E:empty
An element that has no children (neither elements nor text) except perhaps white space. (This selector is the only selector that applied on the parent element, checking child elements.)
E > F
An F element child of an E element. (This is a general selector to target a specific child element. Doesn't count totals.)
The Solution
As mentioned above, the new selector will behave like flex and grid, it will be applied on the parent element and check the total number of children elements it has.
E:nth-children(n)
An E element, counting total n children.
Usage
Conditional design based on the amount of child elements:
Another example is to use keyword values:
Use Cases
This can help developers apply conditional design in many case:
<td>
elements) and amount of datasets (<tr>
elements).<ol>
,<ul>
,<dl>
based on the total amount of list items.The text was updated successfully, but these errors were encountered: