Performance: Not selector
The :not() selector affects CSS selector matching speed and style calculation during page rendering.
Jump into concepts and practice - no test required
div { color: blue; } .exclude { color: initial; }div:not(.exclude) { color: blue; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| :not() with simple class | Low | 1 if style changes | Low | [OK] Good |
| :not() with multiple complex selectors | Medium | Multiple if style changes | Medium | [!] OK |
| Separate styles without :not() | Low | 1 if style changes | Low | [OK] Good |
:not() selector do?:not():not() selector excludes elements that match the selector inside it.:not().:not(). -> Option B:not() excludes matching elements [OK]:not() [OK]p elements except those with class intro?:not() with class selector:not(.intro).p elements except those with class introp:not(.intro) means all p elements except those with class intro.:not() syntax uses parentheses and proper selector [OK]:not() [OK]<div> <p class="intro">Hello</p> <p>World</p> <p class="note">Note</p> </div>
p:not(.intro) { color: red; }<p> be?p elements match :not(.intro)p with class intro is excluded. The other two p elements match.p elements without class intro get color: red;. So "World" and "Note" are red, "Hello" stays default black.:not(.intro) excludes "Hello" [OK]:not() get styles [OK]p get red colordiv:not .highlight { background: yellow; }:not() usage:not() selector requires parentheses around the selector inside it.:not .highlight without parentheses, which is invalid syntax.:not(). -> Option C:not() always needs parentheses [OK]:not [OK]:not():not and parentheses:not works like a combinatorbutton elements except those that are disabled or have the class cancel. Which CSS selector achieves this?:not():not() selectors.:not(:disabled) and :not(.cancel). button:not(:disabled, .cancel) { background: green; } tries to combine selectors inside one :not(), which is invalid. button:not(:disabled .cancel) { background: green; } uses invalid syntax. button:not(:disabled) .cancel { background: green; } selects .cancel inside button:not(:disabled), which is incorrect.:not() for multiple exclusions [OK]:not() for multiple exclusions [OK]:not():not() incorrectly