It is a commonly misunderstood concept among authors about how the ":first-child" pseudo-class works. Initially introduced in CSS2, ":first-child" specifically targets the very first child of its parent. Contrary to popular belief, it does not consider any other conditions or attributes when selecting elements. To understand this better and the difference between ":first-child" and ":first-of-type," you can refer to this detailed explanation.
In comparison, Selectors Level 3 introduces ":first-of-type," which focuses on selecting the first element among siblings of the same type. Sadly, there isn't a similar pseudo-class like ":first-of-class" available for targeting the first child element with a specific class. However, the Selector Level 4 previously proposed ":nth-match()" as a solution, which later merged into the functionality of ":nth-child()" itself, simplifying the selection process.
Edit: The "selector list argument of :nth-child and :nth-last-child CSS pseudo-classes" is now supported across Chrome, Edge, Safari, and Firefox starting from Baseline 2023. (Can I Use: css-nth-child-of)
To achieve styling only the first element with a certain class, you can utilize a workaround technique developed by Lea Verou and others involving applying styles to all elements with that class and then undoing those styles for subsequent elements using the general sibling combinator "~". This allows precise control over which element receives specific styles.
...
Keep in mind that overriding rules in CSS is crucial for this technique to work effectively, as single selectors may not provide the necessary flexibility. Additionally, there's no straightforward solution for matching the nth instance of a complex selector across an entire document in CSS. Different approaches exist using jQuery or the Selectors API for more specialized selections.
The original solution provided by Philip Daubmeier regarding ".red:nth-of-type(1)" showcases a potential method but comes with limitations related to element positioning within the document structure. Understanding these nuances will help prevent unexpected behavior when working with such selectors.
...