Which CSS selector will style only the second <p> element inside the <div>?
medium
A. div p ~ p
B. div p + span
C. div > p + p
D. div > p.intro
Solution
Step 1: Understand the HTML structure
Inside div, there are two p elements separated by a span.
Step 2: Analyze each selector
div > p + p selects a p immediately following another p as a direct child of div. Here, the second p is not immediately after the first p (there is a span in between), so this selector matches nothing. div p ~ p selects any p siblings after a p inside div. The second p is a sibling after the first p, so this matches the second p.
Step 3: Choose the correct selector
div p ~ p correctly selects the second p element.
Final Answer:
div p ~ p -> Option A
Quick Check:
General sibling selector = ~ [OK]
Hint: Use ~ to select siblings after a specific element [OK]
Common Mistakes:
Confusing + (adjacent sibling) with ~ (general sibling)
Using > which requires direct child relationship
Selecting by class when not needed
4. Consider this CSS selector: section > article + p What is wrong with this selector if the goal is to style all <p> elements that are direct children of section?
medium
A. The + combinator is invalid between article and p.
B. It selects all p elements inside section, not just direct children.
C. It only selects p elements immediately after an article, not all p children.
D. The selector should use a space instead of >.
Solution
Step 1: Understand the selector components
section > article + p means: select any p element that is immediately after an article which is a direct child of section.
Step 2: Compare with the goal
The goal is to select all p elements that are direct children of section. This selector only selects p elements that come immediately after an article child, missing other p children.
Final Answer:
It only selects p elements immediately after an article, not all p children. -> Option C
Quick Check:
Adjacent sibling + limits selection [OK]
Hint: Remember + selects only immediate next sibling [OK]
Common Mistakes:
Thinking + selects all siblings
Confusing > (child) with space (descendant)
Assuming invalid syntax with + combinator
5. You want to style all <li> elements that are inside a <ul> with class menu, but only if the <li> is not the first child. Which CSS selector achieves this?
hard
A. ul.menu li + li
B. ul.menu > li:not(:first-child)
C. ul.menu > li:first-child
D. ul.menu > li ~ li
Solution
Step 1: Understand the requirement
We want li elements inside ul.menu but exclude the first child li.
Step 2: Analyze each selector
ul.menu > li:not(:first-child) selects all direct li children of ul.menu except the first one. ul.menu li + li selects every li immediately following another li, which also works but only for adjacent siblings. ul.menu > li ~ li selects all li siblings after the first li as direct children, which also works. However, the most precise and clear selector that excludes only the first child is ul.menu > li:not(:first-child).
Final Answer:
ul.menu > li:not(:first-child) -> Option B
Quick Check:
:not(:first-child) excludes first child [OK]
Hint: Use :not(:first-child) to exclude first child elements [OK]