But only the first paragraph turns green, the last paragraph stays black. Why?
medium
A. Because the colors green and blue are invalid.
B. Because the CSS syntax is incorrect.
C. Because section cannot contain paragraphs.
D. Because p elements are not the first or last child of section.
Solution
Step 1: Understand :first-child and :last-child context
These selectors check if the element is the very first or last child of its parent, regardless of type.
Step 2: Check if p is first or last child
If other elements (like headings or divs) come before or after the p, then p is not first or last child, so styles won't apply.
Final Answer:
Because p elements are not the first or last child of section. -> Option D
Quick Check:
:first-child and :last-child depend on element position, not type [OK]
Hint: Check if element is truly first/last child, not just first/last of type [OK]
Common Mistakes:
Assuming :first-child means first of type
Ignoring other sibling elements
Thinking syntax is wrong when it's position issue
5. You want to style the first and last <li> elements inside every <ul> differently, but only if the <li> is also the first or last child of its parent. Which CSS selectors correctly achieve this?
hard
A. ul > li:first-child { font-weight: bold; } and ul > li:last-child { font-style: italic; }
B. ul:first-child li { font-weight: bold; } and ul:last-child li { font-style: italic; }
C. ul li:first-child { font-weight: bold; } and ul li:last-child { font-style: italic; }
D. ul li:first-of-type { font-weight: bold; } and ul li:last-of-type { font-style: italic; }
Solution
Step 1: Understand the requirement
We want to style only li elements that are the first or last child of their ul parent.
Step 2: Choose selectors that target direct children and correct position
ul > li:first-child and ul > li:last-child select li elements that are direct children and first or last child respectively. This matches the requirement exactly.
Step 3: Evaluate other options
ul li:first-child { font-weight: bold; } and ul li:last-child { font-style: italic; } misses the direct child combinator, which can cause incorrect matches if nested lists exist. ul:first-child li { font-weight: bold; } and ul:last-child li { font-style: italic; } incorrectly applies :first-child to ul. ul li:first-of-type { font-weight: bold; } and ul li:last-of-type { font-style: italic; } uses :first-of-type which selects first li regardless of position among siblings.
Final Answer:
ul > li:first-child { font-weight: bold; } and ul > li:last-child { font-style: italic; } -> Option A
Quick Check:
Direct child + :first-child and :last-child = ul > li:first-child { font-weight: bold; } and ul > li:last-child { font-style: italic; } [OK]
Hint: Use direct child combinator > with :first-child and :last-child [OK]