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

How can you use :empty to detect empty elements?

Understanding the :empty Pseudo-Class in CSS

The :empty pseudo-class in CSS targets elements that have no children at all, including text nodes. It is useful for styling or detecting elements that are truly empty.

Key Points About :empty
  1. 1

    :empty selects elements that contain no child elements or text content.

  2. 2

    Whitespace (spaces, tabs, or line breaks) inside an element counts as content, so :empty will not match.

  3. 3

    It can be used to style placeholders, highlight missing content, or manage layout adjustments for empty elements.

Example: Using :empty

In this example, the second paragraph is highlighted because it is truly empty. The third paragraph is not matched by :empty due to containing a whitespace character.

Best Practices
  1. 1

    Use :empty to detect elements without content dynamically.

  2. 2

    Trim whitespace inside elements if you want :empty to match them.

  3. 3

    Combine with pseudo-elements like ::before or ::after for custom placeholder content.

  4. 4

    Test across different browsers, as handling of whitespace and empty elements may vary slightly.

Difficulty: 3/10
Topics: CSS pseudo-class, empty element detection, UI rendering behavior

Scenario Questions

0-2 years experience
  1. 1

    You're building a todo list and want to show a 'No items yet' message only when the list is truly empty. You used :empty on the ul, but the message still shows even when there's a space in the list. Why?

  2. 2

    How would you use :empty to hide a sidebar section when it has no child elements, and what CSS rule would you write?

  3. 3

    If you apply :empty to a div that contains only a comment, will it match? Why or why not?

2-5 years experience
  1. 1

    A user reports that the empty state message in our dashboard keeps flickering — sometimes it shows, sometimes it doesn't. You checked the HTML and it looks empty. What could be causing this, and how would you fix it using :empty?

  2. 2

    We're using :empty to conditionally style a chat message container, but it breaks when users paste non-breaking spaces. How would you detect and handle this edge case without breaking accessibility?

  3. 3

    Our design system uses :empty to hide empty tooltips, but in some browsers, the tooltip still renders a tiny gap. What's likely happening, and how would you debug this cross-browser issue?

5-8 years experience
  1. 1

    You're designing a reusable component library where :empty is used to toggle placeholder content. How would you ensure consistent behavior across SSR, hydration, and dynamic content injection without introducing layout shifts?

  2. 2

    In a high-performance feed, we're using :empty to avoid rendering empty cards. But we're seeing performance regressions in Firefox. What might be the root cause, and how would you optimize this without abandoning :empty?

  3. 3

    A legacy component uses :empty for conditional rendering, but now we're migrating to a framework that injects comment nodes for hydration. How do you refactor this without breaking existing styles or introducing visual bugs?

8+ years experience
  1. 1

    We're standardizing our design system across 12 product teams, and some are using :empty while others use JS to detect emptiness. How would you drive alignment on this pattern, and what long-term maintenance risks do you foresee?

  2. 2

    Our CSS architecture relies on :empty for dynamic UI states, but we're scaling to 50+ micro-frontends. How would you ensure consistent behavior across teams without a centralized CSS build, and what monitoring would you put in place?

  3. 3

    A major accessibility audit flagged our :empty-based empty states as problematic for screen readers. How would you redesign this at the architecture level to maintain visual design while meeting WCAG standards?

Follow-up Questions

  • What happens if the element has a single space inside?
  • How would you debug why :empty isn't working on an element you think is empty?
  • Can you use :empty with other pseudo-classes like :hover?