14

li > ul > li selects all li elements which are deeper than the first level of a ul.

li selects all li elements

li:not(li > ul > li) should select all li elements which are no deeper than the first level of a ul--that is, only first level elements--but it doesn't. Why?

Thanks!

8

1 Answer 1

11

The reason li:not(li > ul > li) does not work is because the li > ul > li is not a simple selector (as Felix Kling noted in the comments to your question).

The easiest way to get the top level is to give a class or id to the outer most ul and then do:

.ulClassNameOrID > li {}

However, the following gets what you desire also (see fiddle) as it does not select any ul that is a direct child of a previous li (so is not a sublist of the outer list):

:not(li) > ul > li {}
6
  • He want's to select the first level elements, not the top-most level.
    – Michael
    Mar 29, 2013 at 14:27
  • @Michael: I understand "first-level" to be the list elements that are under the initial ul element and no deeper, which would be the "top-most level". If that is incorrect, then I need the OP to clarify the question.
    – ScottS
    Mar 29, 2013 at 14:38
  • 2
    @Michael: And besides that, the title to the question mentions the "top level" while the question itself states "first level," so I think I have interpreted the OP correctly.
    – ScottS
    Mar 29, 2013 at 14:41
  • @ScottS My comment was well before your edit ;). You indeed have.
    – Michael
    Mar 29, 2013 at 15:13
  • @Michael: My edit didn't change anything about the essence of my answer, it just offered a second (more generic, without class or id) solution. But I'm glad we have come to a consensus on what we believe the OP is asking (though he has not made a statement himself).
    – ScottS
    Mar 29, 2013 at 15:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.