Questions
11 of 50
1What is a pseudo-class in CSS?
2How are pseudo-classes different from pseudo-elements?
3What is the syntax of a pseudo-class in CSS?
4Give some commonly used pseudo-classes.
5What does the :hover pseudo-class do?
6What’s the purpose of the :active pseudo-class?
7How does the :visited pseudo-class work for links?
8What is the :focus pseudo-class used for?
9What does the :checked pseudo-class do?
10How does the :disabled pseudo-class behave?
11What is the difference between :first-child and :first-of-type?
12How do :nth-child() and :nth-of-type() differ?
13How do you select every odd or even element using pseudo-classes?
14How can you style an element when it’s being hovered over along with its child?
15What does the :not() pseudo-class do?
16How can you use multiple pseudo-classes together?
17What is the difference between :link and :visited?
18What does the :target pseudo-class represent?
19How can you use :empty to detect empty elements?
20How does the :root pseudo-class differ from the html selector?
21What does the :valid and :invalid pseudo-class do?
22How can you use :required and :optional pseudo-classes in forms?
23What is the use of :in-range and :out-of-range?
24What does :read-only and :read-write do?
25How can you style an input field when it’s autofilled?
26How does the :focus-within pseudo-class work?
27What’s the difference between :focus and :focus-visible?
28What is the purpose of the :placeholder-shown pseudo-class?
29How can you style a checkbox when it is checked or unchecked?
30How can you style invalid form inputs without JavaScript?
31Can pseudo-classes be chained together? Give an example.
32What is the specificity of pseudo-classes compared to normal selectors?
33How can you use :not() effectively to exclude elements from styling?
34How does :has() pseudo-class work, and why is it powerful?
35Is :has() supported in all browsers?
36How does :is() differ from :where() in terms of specificity?
37How can you combine :is() or :where() with other selectors for cleaner code?
38What’s the difference between :nth-child() and :nth-last-child()?
39How can you style only the last element of a list using pseudo-classes?
40What does the :lang() pseudo-class do?
41How do you change a button’s color when hovered but not when disabled?
42How can you highlight the current section in a menu using :target?
43How can you style a form field differently when focused and valid?
44How do you style every third list item differently?
45How can you style alternate table rows without adding extra classes?
46How do you hide empty <p> tags using pseudo-classes?
47How can you style the parent when any child inside it is focused?
48How can you highlight a link only when it’s both focused and hovered?
49How can you style the first letter of only the first paragraph using pseudo-classes?
50How would you use :has() to select a <div> that contains an image?
11 / 50

What is the difference between :first-child and :first-of-type?

Understanding :first-child vs :first-of-type in CSS

Both :first-child and :first-of-type are CSS pseudo-classes used to target elements based on their position, but they work differently.

Differences
  1. 1

    :first-child – Selects an element if it is the very first child of its parent, regardless of type.

  2. 2

    :first-of-type – Selects the first element of its specific type (tag) among its siblings.

Example: :first-child vs :first-of-type

In this example, :first-child applies only if the element is the very first child of the parent, whereas :first-of-type applies to the first element of that tag type, even if it's not the very first child.

Best Practices
  1. 1

    Use :first-child when you want to style the very first child element regardless of type.

  2. 2

    Use :first-of-type when you want to style the first element of a specific type among siblings.

  3. 3

    Combine these pseudo-classes with other selectors for precise targeting.

  4. 4

    Test in different HTML structures, especially when other elements may precede the target element.

Difficulty: 3/10
Topics: CSS selectors, pseudo-classes, element positioning

Scenario Questions

0-2 years experience
  1. 1

    You're styling a list of articles where each has a header and a paragraph. You want the first paragraph to have a different font size, but your CSS using :first-child isn't working. Why?

  2. 2

    You have a div with three span elements and one p element. You apply color: red to both :first-child and :first-of-type. What colors do you see and why?

  3. 3

    A teammate says :first-child should style the first paragraph in a section, but it's not working. What’s the most likely reason?

2-5 years experience
  1. 1

    Our dynamic blog component sometimes wraps the first paragraph in a span for analytics, and now our :first-child style breaks. How do you fix this without hardcoding element types?

  2. 2

    A designer reports that the first item in a user-generated list sometimes doesn't get the top margin reset. You notice it's because a hidden comment node is the first child. How do you solve this reliably?

  3. 3

    You're building a reusable card component where the first heading should have a larger font. The component can be used with h2, h3, or even divs as headers. Which selector do you use and why?

5-8 years experience
  1. 1

    You're designing a themable component library where authors can nest arbitrary content inside a container. How do you ensure consistent styling of the first content element without assuming structure, and what edge cases keep you up at night?

  2. 2

    A legacy CMS outputs unpredictable markup with comments, whitespace text nodes, and inline scripts. How do you reliably target the first actual semantic element (like p, h1, or div) without breaking in edge cases?

  3. 3

    Your team is migrating from a rigid grid system to a flexible one. Previously, :first-child was used to reset margins. Now with dynamic content insertion, you're seeing layout shifts. How do you refactor this safely?

8+ years experience
  1. 1

    You're leading a cross-team initiative to standardize UI components across 12 products. Some teams use :first-child, others :first-of-type. How do you decide the standard, and how do you handle legacy components that break under the new rule?

  2. 2

    Our design system has a base typography component that’s used in 50+ micro-frontends. The first element styling breaks unpredictably in edge cases due to SSR hydration or third-party widgets injecting nodes. What architectural changes do you propose to make this robust long-term?

  3. 3

    A legacy product uses :first-child for critical layout resets, but now we're adding A/B test variants that inject tracking elements. How do you de-risk this migration without breaking hundreds of pages, and what metrics do you track to validate success?

Follow-up Questions

  • What if you add a comment before the first paragraph? What does :first-child target then?
  • How would you debug a case where a button isn't getting the expected style even though it's clearly the first element?
  • Can you think of a scenario where :first-of-type is more reliable than :first-child?