I have a file like
<div>
<div class="abc">
<div>
<!--some more divs inside-->
</div>
</div>
</div>
What I want to do is to apply styles only to the first div. I tried to use div:not(.abc, .abc *), div:not(.abc):not(.abc *), div:not(.abc), div:not(.abc) * but none of these worked. It would be hard to edit the html, because there would be many files to be edited. Also the code shown above appears in different places, so using > selector is not the solution… Does someone know how to do this?
You cannot reliably use the
:not()selector in CSS for excluding an element and/or its descendants. The reason for it is explained in this answer (and some others that it links to):And since your markup is unpredictable enough that you cannot edit it or use the
>selector, I’m afraid there’s not much of a way out for you other than to either find a way to apply a class to your topdivand use that class, as demonstrated by Fluidbyte, and/or use jQuery, as implied above.