-2

DOM snippet

ul.this>li>ul                                  ->match
ul.this>li>div>ul                              ->match
ul.this>li>div>div>ul                          ->match
ul.this>li>div>menu>div>nav>div>div>div>div>ul ->match
ul.this>li>ul>li>div>ul                        ->dont match
ul.this>li>div>ul>li>div>ul                    ->dont match

Is there a better way in CSS than stating:

ul.this>li>ul, ul.this>li>:not(ul)>ul, ul.this>li>:not(ul)>:not(ul)>ul,
ul.this>li>:not(ul)>:not(ul)>:not(ul)>ul,
ul.this>li>:not(ul)>:not(ul)>:not(ul)>:not(ul)>ul,
ul.this>li>:not(ul)>:not(ul)>:not(ul)>:not(ul)>:not(ul)>ul

…as that will still fail the n+1 case, but you have to end somewhere. Something like /(ul)+/ in regex?

Read:

Any ul inside and descending ul.this that has in its path from ul.this to the resulting ul no other ul in between.

17
  • 3
    What use-case are you trying to solve by asking the question? There's probably a way to do what you want, but without seeing an example of what you're trying to do it's hard (for me at least) to offer an answer. Can you post a snippet of your "minimal reproducible example" HTML and explain what should and shouldn't be selected? Commented Aug 26, 2024 at 9:45
  • 1
    Trying to get my brain round this: you want the first ul descendant of a given li? Is that correct? Commented Aug 26, 2024 at 11:08
  • 2
    "Any ul inside and descending this li that has in its path from li to the resulting ul no other ul in between.". What do you mean by "this li"? Your example li>ul,li>:not(ul.... matches ul descendants of any li. That matters because in valid HTML, all children of ul elements that can themselves contain a ul element must be an li element. So a ul element that's a descendant of a ul element that's a descendant of a given li - something that you don't want to match - will, by necessity be the descendant of a different li that doesn't have an intervening ul element. Commented Aug 26, 2024 at 12:16
  • 2
    On the other hand, if you mean a specific li, say one with an id of "myli", then your requirement is met simply by #myli ul:not(#myli ul ul) Commented Aug 26, 2024 at 12:35
  • 1
    It would help a lot if you presented some HTML/structure and told us what you wish and don’t wish to match within it. Commented Aug 26, 2024 at 16:06

1 Answer 1

1

ul.this ul:not(ul.this ul ul) {/*...*/}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.