Introduction
The :not() selector helps you style elements except the ones you want to exclude. It makes your CSS simpler by avoiding extra classes or complicated rules.
Jump into concepts and practice - no test required
The :not() selector helps you style elements except the ones you want to exclude. It makes your CSS simpler by avoiding extra classes or complicated rules.
selector:not(selector) { property: value; }
The :not() takes a simple selector inside the parentheses.
It excludes elements matching the selector inside :not() from the style.
submit.button:not(.submit) { background-color: lightblue; }
li:not(:first-child) { color: gray; }
special.p:not(.special) { font-style: italic; }
This example styles all buttons with a green background except the button with class submit, which has a red background.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Not Selector Example</title> <style> button:not(.submit) { background-color: lightgreen; border: 2px solid green; padding: 0.5rem 1rem; font-size: 1rem; cursor: pointer; } button.submit { background-color: lightcoral; border: 2px solid darkred; padding: 0.5rem 1rem; font-size: 1rem; cursor: pointer; } </style> </head> <body> <button>Cancel</button> <button class="submit">Submit</button> <button>Reset</button> </body> </html>
You can combine :not() with other selectors for more control.
Using :not() keeps your CSS clean and easier to read.
:not() excludes elements from a style.
Use it to style everything except specific elements.
It helps write simpler and clearer CSS.
: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