How would you select every second level in the stack of infinite levels using CSS only?
Sample code:
<div>
1
<div>
2
<div>
3
<div>
4
<div>
5
<div>
[...]
</div>
</div>
</div>
</div>
</div>
</div>
Visual example of the preferred result:
Note: no JavaScript assistance needed; there is no problem when the number of levels is known.
Selecting Only the Next Sibling with Adjacent Sibling Combinator ( + ) The + character used in CSS is a combinator — it combines 2 CSS selectors. It selects the second element, only if it is just after the first element, and both elements are children of the same parent.
Yes, you can use what's known as :nth-child selectors. In this case you would use: li:nth-child(3n) { // Styling for every third element here. }
Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state)
While there is no problem when the number of levels is known, when it is not known then it becomes impossible with CSS only, not even if you are able to anchor the topmost div
to some other element with a selector.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With