Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Highlight All Items Except the Selected One Using the CSS :not() Selector
📖 Scenario: You are creating a simple webpage with a list of fruits. You want to highlight all fruits except the one that is selected by the user.
🎯 Goal: Build a webpage with a list of fruits where all items except the selected one have a light gray background using the CSS :not() selector.
📋 What You'll Learn
Create an unordered list with exactly five fruit items: Apple, Banana, Cherry, Date, Elderberry
Add a CSS class called selected to the Banana list item
Use the CSS :not() selector to style all list items except the one with the selected class
Set the background color of the non-selected items to #f0f0f0
Ensure the selected item has no background color
💡 Why This Matters
🌍 Real World
Using the :not() selector helps you style all elements except a specific one, which is common in menus, lists, and interactive UI components.
💼 Career
Front-end developers often use :not() to write cleaner CSS that targets groups of elements while excluding exceptions, improving maintainability and user experience.
Progress0 / 4 steps
1
Create the HTML list of fruits
Create an unordered list <ul> with five list items <li> containing the exact fruit names: Apple, Banana, Cherry, Date, and Elderberry.
CSS
Hint
Use the <ul> tag for the list and add five <li> tags inside it with the fruit names.
2
Add the selected class to Banana
Add the CSS class selected to the Banana list item by modifying its <li> tag to include class="selected".
CSS
Hint
Find the Banana list item and add class="selected" inside its <li> tag.
3
Write CSS to style all non-selected list items
Write a CSS rule using the :not() selector to select all li elements that do NOT have the class selected. Set their background color to #f0f0f0.
CSS
Hint
Use li:not(.selected) as the selector and set background-color: #f0f0f0; inside the curly braces.
4
Ensure the selected item has no background color
Add a CSS rule to the li.selected selector that sets the background color to transparent to make sure the selected item has no background color.
CSS
Hint
Use the selector li.selected and set background-color: transparent; inside its curly braces.
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]