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

How would you use :has() to select a <div> that contains an image?

Selecting a <div> Containing an Image Using :has() in CSS

The :has() pseudo-class in CSS allows you to select elements that contain specific descendants. You can use it to target a <div> that contains an <img> element and apply custom styles.

Key Points
  1. 1

    :has() selects elements based on their descendants or children.

  2. 2

    You can combine :has() with other selectors for more precise targeting.

  3. 3

    This pseudo-class is particularly useful for styling parent elements depending on their child elements.

Example: Style <div> Containing an Image

In this example, only the <div> that contains an <img> element receives the border and background styles, while other <div> elements remain unchanged.

Best Practices
  1. 1

    Use :has() to style containers based on their child elements dynamically.

  2. 2

    Check browser compatibility, as older browsers may not fully support :has().

  3. 3

    Combine with transitions for smooth visual effects when elements change state.

  4. 4

    Avoid overusing :has() for performance-critical layouts, as it may increase rendering complexity.

Difficulty: 6/10
Topics: CSS :has() selector, dynamic content targeting, selector performance

Scenario Questions

0-2 years experience
  1. 1

    How would you write a CSS rule to style a div only if it contains an img element inside it?

  2. 2

    What happens if you try to use :has() to select a div that contains an image with class 'avatar'?

2-5 years experience
  1. 1

    Our card component sometimes doesn't get the highlighted border even when it has an image — we're using :has() but it's not working in older browsers. How would you debug and fix this?

  2. 2

    A designer wants all product cards with images to have a shadow, but we're seeing inconsistent behavior on mobile. How would you investigate whether :has() is the right tool here?

5-8 years experience
  1. 1

    We're using :has() to conditionally style containers with media elements, but performance is degrading on pages with 200+ cards. How would you optimize this without resorting to JS?

  2. 2

    The design system uses :has() to apply hover effects to containers with buttons, but it breaks when buttons are conditionally rendered via React. How do you reconcile dynamic content with static CSS selectors?

8+ years experience
  1. 1

    We're migrating from a legacy JS-based component system to pure CSS-driven styling using :has(), but we still support IE11 in enterprise contracts. How would you architect a migration strategy that balances modern CSS with legacy constraints?

  2. 2

    Our CSS-in-JS library doesn't support :has() in SSR. How would you design a cross-team solution to ensure consistent styling between server-rendered and client-rendered components without introducing hydration mismatches?

Follow-up Questions

  • What happens if the image is loaded dynamically after the page renders?
  • How would you debug this if :has() isn't working in a specific browser?
  • Could this selector cause layout thrashing or performance issues on a large page?