Questions
32 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?
32 / 50

What is the specificity of pseudo-classes compared to normal selectors?

Understanding Specificity of Pseudo-Classes in CSS

Pseudo-classes in CSS have a specificity that is treated similarly to class selectors. They are more specific than type selectors but less specific than ID selectors. Understanding this helps predict which styles will apply when multiple rules target the same element.

Specificity Rules for Pseudo-Classes
  1. 1

    Pseudo-classes (e.g., :hover, :focus, :nth-child()) count as a single class in specificity calculations.

  2. 2

    Type selectors (e.g., div, p) and pseudo-elements (e.g., ::before, ::after) have lower specificity than pseudo-classes.

  3. 3

    ID selectors have higher specificity than pseudo-classes.

  4. 4

    Inline styles override pseudo-classes regardless of specificity.

Example: Pseudo-Class Specificity

Here, the first <p> changes to orange on hover because the :hover pseudo-class has higher specificity than the type selector. The second <p> with an ID stays red since ID selectors override pseudo-classes.

Best Practices
  1. 1

    Use pseudo-classes to enhance interactivity without unnecessarily increasing specificity conflicts.

  2. 2

    Combine pseudo-classes with classes or IDs for precise targeting.

  3. 3

    Avoid over-chaining selectors to keep specificity manageable.

  4. 4

    Test across browsers to ensure expected style application.

Difficulty: 6/10
Topics: CSS specificity, selector precedence, pseudo-class weighting

Scenario Questions

0-2 years experience
  1. 1

    You’re trying to style a button with :hover, but it’s not working — the base button style from a library is overriding it. What’s the most likely cause, and how would you fix it without using !important?

  2. 2

    You wrote a rule like li:hover { color: red; } and another like .active li { color: blue; }. When you hover over an active list item, which color wins? Why?

2-5 years experience
  1. 1

    A user reports that the dropdown menu’s :focus styles aren’t showing up on mobile — you’ve checked the CSS and it looks correct. What’s your debugging process, and how might specificity be involved?

  2. 2

    Your team’s design system uses :nth-child(odd) for alternating row colors, but now a new component is injecting dynamic content that breaks the pattern. How do you diagnose whether specificity or structural changes are the root cause?

5-8 years experience
  1. 1

    You’re designing a reusable card component that needs to support both :focus-within and :has() for accessibility, but legacy browsers don’t support :has(). How do you architect the CSS to maintain visual consistency while avoiding specificity wars across the component tree?

  2. 2

    Your CSS architecture uses utility classes heavily, but now you need to add state-driven styles like :disabled or :invalid for form fields. How do you balance reusability, maintainability, and specificity without creating brittle overrides?

8+ years experience
  1. 1

    You’re leading a migration from a legacy CSS framework that relied on !important everywhere to a modern CSS-in-JS system. How do you handle the technical debt of pseudo-class specificity conflicts that were buried under global overrides, and what long-term governance do you put in place?

  2. 2

    Your company has 12 product teams using a shared design system, but each team has their own CSS layer that injects pseudo-class styles. How do you design a specificity governance model that prevents visual regressions during independent deployments without stifling autonomy?

Follow-up Questions

  • What if you have two conflicting pseudo-classes targeting the same element?
  • How would you debug a hover state that isn’t applying even though the selector looks right?
  • Can you explain why :not() doesn’t increase specificity the way you might expect?