I'm trying to select all elements of a given class "class1" which don't have any ancestor in "class2". I thought the following selector would work:
:not(.class2) .class1
I am putting a space between the selectors because I want all descendants of elements not in class2 to be selected. But this didn't work, so I also tried changing the space to >
. However this doesn't work quite right either.
I create a JSFiddle to demonstrate what I am expected versus what I actually see.
Update: It seems like the issue with using "space" is that this is literally selecting every element of class1 which has any element in its ancestry which is not in "class2", while the problem with ">" is that it only selects elements in which the parent is not "class2" without regard to whether further ancestors are in that class or not.
Update 2:
After looking at the example by @DaniP it occurs to me that another way of looking at the problem is to subtract one set from another. So I tried .class1:not(.class2 .class1)
but this apparently does not work because :not()
doesn't accept complex selectors.