Use :not() with the class selector inside parentheses to exclude elements with that class.
✗ Incorrect
The :not() selector excludes elements matching the selector inside it. Here, p:not(.exclude) selects all
elements that do NOT have the class 'exclude'. Option C colors those paragraphs red as required. Option C is invalid syntax because #exclude is an ID selector and missing parentheses. Option C colors the correct elements but uses blue instead of red. Option C changes background color, not text color.
❓ selector
intermediate
2:00remaining
Selecting inputs except checkboxes
Which CSS selector will select all elements except those of type checkbox?
Use attribute selectors inside :not() to exclude inputs with type checkbox.
✗ Incorrect
Option B correctly uses the attribute selector [type="checkbox"] inside :not() to exclude checkbox inputs. Option B is invalid syntax because it misses brackets around the attribute selector. Options A and D try to exclude by ID or class which do not apply here.
Check which selectors apply and which have higher specificity.
✗ Incorrect
The div has both classes 'box' and 'highlight'. The selector .box:not(.highlight) excludes elements with class 'highlight', so it does not apply here. The selector .box.highlight applies and sets background to blue. Therefore, the background color is blue.
🧠 Conceptual
advanced
2:00remaining
Understanding :not() with complex selectors
Which statement about the CSS selector div:not(.active) > p is true?
ASelects all <p> elements that are direct children of <div> elements without class 'active'.
BSelects all <p> elements inside any <div> that does not have class 'active', including nested descendants.
CSelects all <p> elements that do not have class 'active' and are inside any <div>.
DSelects all <div> elements that do not have class 'active' and contain <p> children.
Attempts:
2 left
💡 Hint
Look carefully at the combinator and :not() placement.
✗ Incorrect
The selector div:not(.active) > p means: select
elements that are direct children (>) of
elements that do NOT have the class 'active'. Option A states this correctly. Option A is incorrect because it says nested descendants, but > means direct children only. Option A incorrectly applies :not() to
elements. Option A selects
elements, but the selector targets
elements.
❓ accessibility
expert
3:00remaining
Using :not() for accessible focus styling
You want to style all interactive elements except disabled buttons when they receive keyboard focus. Which CSS selector correctly applies a visible outline only to those elements?
A:is(button, a, input):not(:disabled:focus-visible) { outline: 3px solid orange; }
B:is(button, a, input):focus-visible:not(:disabled) { outline: 3px solid orange; }
C:is(button, a, input):focus:not(:disabled) { outline: 3px solid orange; }
D:is(button, a, input):not(:disabled):focus-visible { outline: 3px solid orange; }
Attempts:
2 left
💡 Hint
Use :not() to exclude disabled elements before applying :focus-visible.
✗ Incorrect
Option D correctly selects buttons, links, and inputs that are not disabled and are focused via keyboard (:focus-visible). Option D places :focus-visible before :not(:disabled), but best practice is to exclude first. Option D uses :not(:disabled:focus-visible) which doesn't properly target the focus state. Option D uses :focus instead of :focus-visible, which is less accessible because it applies on mouse focus too.
Practice
(1/5)
1. What does the CSS :not() selector do?
easy
A. It selects only the elements matching the selector inside :not().
B. It selects all elements except those matching the selector inside :not().
C. It selects elements that have no children.
D. It selects elements that are hidden.
Solution
Step 1: Understand the purpose of :not()
The :not() selector excludes elements that match the selector inside it.
Step 2: Identify what it selects
It selects all elements except those that match the selector inside :not().
Final Answer:
It selects all elements except those matching the selector inside :not(). -> Option B
Quick Check:
:not() excludes matching elements [OK]
Hint: Think 'everything but' the selector inside :not() [OK]
Common Mistakes:
Thinking it selects only matching elements
Confusing it with :only-child or :empty
Assuming it selects hidden elements
2. Which of the following is the correct syntax to select all p elements except those with class intro?
easy
A. p:not(.intro) { color: blue; }
B. p:not#intro { color: blue; }
C. p:not[.intro] { color: blue; }
D. p:not(intro) { color: red; }
Solution
Step 1: Check the syntax for :not() with class selector
The correct syntax uses parentheses with a class selector inside: :not(.intro).
Step 2: Verify the selector targets p elements except those with class intro
p:not(.intro) means all p elements except those with class intro.
Final Answer:
p:not(.intro) { color: blue; } -> Option A
Quick Check:
Correct :not() syntax uses parentheses and proper selector [OK]
Hint: Use parentheses and proper selector inside :not() [OK]