Difficulty: Easy
Correct Answer: Child selectors use the parent > child syntax to select elements that are direct children of a specified parent element.
Explanation:
Introduction / Context:
CSS provides several combinators to express relationships between elements in the document tree. Understanding the difference between a descendant selector and a child selector is important when you want to control exactly which elements receive certain styles. This question focuses on child selectors, which are used to match elements that are immediate children of a given parent element.
Given Data / Assumptions:
Concept / Approach:
A child selector consists of two simple selectors separated by the > combinator. For example, ul > li targets only those li elements that are direct children of a ul element. It will not select li elements nested deeper inside other lists or containers. In contrast, a descendant selector uses a space, such as ul li, and matches li elements at any depth inside the ul. The correct answer must therefore mention direct children and the parent > child syntax.
Step-by-Step Solution:
Step 1: Recall that the > combinator is used for child relationships in CSS.Step 2: Understand that direct child means the element appears one level below the parent in the DOM tree.Step 3: Option A states that child selectors use the parent > child syntax to select direct children.Step 4: Option B describes descendant selectors, which use a space, not the > combinator.Step 5: Options C and D misunderstand child selectors entirely, so option A is the correct explanation.
Verification / Alternative check:
You can verify this by styling a list where some li elements are directly inside a ul and others are nested inside an inner ul. Using ul > li { color: red; } will colour only the top level list items. Using ul li { color: red; } will affect all li elements at every nesting level. This experiment demonstrates that the > combinator restricts the selection to immediate children, as described in the correct option.
Why Other Options Are Wrong:
Option B describes the descendant selector, not the child selector, and therefore is inaccurate for this specific question. Option C incorrectly suggests that child selectors ignore the parent context and always choose the first child of the whole document. Option D is incorrect because child selectors are part of the standard CSS specification and work with HTML documents in all modern browsers.
Common Pitfalls:
Learners sometimes confuse child selectors with descendant selectors and use the wrong combinator, leading to styles being applied too broadly or not at all. Another common mistake is to assume that child selectors automatically apply to deeply nested elements. Always remember that the > combinator is strict: it only matches direct parent child relationships, which gives you fine grained control over styling.
Final Answer:
Child selectors use the parent > child syntax to select elements that are direct children of a specified parent element.
Discussion & Comments