html - Cascading with CSS :not pseudo-class -


i'm confused why example doesn't work:

css:

p {   color: red; }  div:not(.exclude) p {   color: green; } 

html:

<div>   <div class="exclude">     <p>i should red</p>   </div>   <div>     <p>i should green</p>   </div> </div> 

the end result both <p> green, have expected first 1 red. here's jsfiddle.

interestingly, found 3 different ways make work:

  1. remove top-level <div> html
  2. change top-level <div> different element (e.g. <section>)
  3. add div beginning of second css selector (div div:not(.exclude) p)

and weird way break it:

  1. using solution 2 basis, wrap <div> around <section>

according mdn:

this selector applies 1 element; cannot use exclude ancestors. instance, body :not(table) a still apply links inside of table, since <tr> match :not() part of selector.

that makes sense, don't think happening here. since there nothing between <div class="exclude"> , direct child <p>, should trigger rule regardless of nested inside. missing? i'd appreciate if me understand this.

since there nothing between <div class="exclude"> , direct child <p>, should trigger rule regardless of nested inside. missing?

the <p> descendant of both top-level <div> , <div class="exclude">. while latter doesn't match selector, former does, , therefore have match. doesn't matter either ancestor fails match selector closer <p> 1 does.

solutions 1 , 2 work eliminating match altogether.

solution 3 works when no other <div>s exist in <p>'s ancestry, because restrict selector exact criteria, in exact order. means if swapped class attribute inner <div> outer one, no longer match selector, , conversely if swapped class selector inner div outer one, selector not match original html structure (again, assuming no other <div>s exist in hierarchy).

wrapping <div> around <section> causes selector matched again <div>. <section> ignored, in same way <div class="exclude">.

see also:


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -