WEB-DEVELOPMENT-FOUNDATION practice question 4 of 15

What describes the matches resulting from the img: first-child selector?

Full set: 75 questions
Answer options
A: Any img element that the cursor is currently over
B: Any img element that is the initial element within a container Correct answer
C: Any img element that is the final img element within a container
D: Any img element that has no other siblings within a parent container
Correct Answer:
B. Any img element that is the initial element within a container
Explanation

The CSS selector img:first-child is a combination of a type selector (img) and a structural pseudo-class (:first-child). The :first-child pseudo-class targets any element that is the first among a group of sibling elements within a parent container. Therefore, the selector img:first-child specifically matches any element that is the very first child of its parent element. If any other element, like a

or

, comes before the inside the parent, the will not be selected.

Why Incorrect

A. This describes the :hover pseudo-class, which applies when a user's cursor is positioned over an element.
C. This describes the :last-child pseudo-class, which targets the very last element within a parent container.
D. This describes the :only-child pseudo-class, which targets an element that is the sole child of its parent. ---

References

1. Mozilla Developer Network (MDN) Web Docs: In the documentation for the :first-child pseudo-class, it states, "The :first-child CSS pseudo-class represents the first element among a group of sibling elements." This confirms that the selector targets the initial element.

Source: MDN Web Docs, :first-child, retrieved from https://developer.mozilla.org/en-US/docs/Web/CSS/:first-child

2. W3C (World Wide Web Consortium) Specification: The official CSS Selectors Level 3 specification defines the behavior of this pseudo-class.

Source: W3C, Selectors Level 3, Section 6.6.5.1. :first-child pseudo-class, W3C Recommendation 29 September 2011. It specifies, "The :first-child pseudo-class represents an element that is the first child of some other element."

3. MIT OpenCourseWare: Course materials for web design and development frequently cover fundamental CSS selectors. Lecture notes on CSS structure and selectors explain this concept.

Source: MIT OpenCourseWare, 6.813/6.831 User Interface Design and Implementation, Fall 2011, Lecture 4: CSS, which covers the box model and selectors, including structural pseudo-classes like :first-child. Available at https://ocw.mit.edu/courses/6-813-user-interface-design-and-implementation-fall-2011/

Scroll to Top