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

How does the :focus-within pseudo-class work?

Understanding the :focus-within Pseudo-Class in CSS

The :focus-within pseudo-class in CSS applies to an element when it or any of its descendants has focus. It is useful for styling a container when an input or interactive element inside it is focused.

Key Points
  1. 1

    :focus-within applies to the parent element, not just the focused child.

  2. 2

    It helps highlight groups of elements (like form sections) when one of their inputs is active.

  3. 3

    It works with all focusable elements, including <input>, <textarea>, <select>, <button>, and elements with tabindex.

Example: Using :focus-within

In this example, when the user focuses on either the 'First Name' or 'Last Name' input, the entire .form-section container gets a highlighted background and border, providing visual context to the focused group.

Best Practices
  1. 1

    Use :focus-within to improve form usability and highlight related fields.

  2. 2

    Combine with transitions for smooth visual feedback.

  3. 3

    Avoid over-styling; subtle highlighting is often sufficient.

  4. 4

    Test accessibility and keyboard navigation to ensure focus indicators remain clear.

Difficulty: 6/10
Topics: focus-within pseudo-class, form accessibility, CSS state management

Scenario Questions

0-2 years experience
  1. 1

    You're building a login form and want the entire form box to get a border glow when the user tabs into any input. How would you use :focus-within to do that without JavaScript?

  2. 2

    If you apply :focus-within to a div containing a button and an input, and the button gets focus, will the div still style? Why or why not?

2-5 years experience
  1. 1

    A teammate says the search bar’s container isn’t highlighting on focus anymore, even though the input is focusable. What are three things you’d check in the CSS and HTML structure?

  2. 2

    We’re using :focus-within to show a dropdown menu on focus, but it breaks on mobile Safari. What’s likely causing this, and how would you debug it without changing the markup?

  3. 3

    Our form validation messages are inside the container with :focus-within, but they’re getting the same highlight as the inputs. How would you fix that without adding extra classes?

5-8 years experience
  1. 1

    We’re designing a reusable form component library and want :focus-within to be the default focus indicator for accessibility. What edge cases or browser inconsistencies should you account for, and how would you structure the CSS to avoid style leakage?

  2. 2

    A senior engineer argues that :focus-within is too unpredictable for complex nested forms. How would you defend its use in a scalable UI system, and what performance or maintenance tradeoffs would you highlight?

  3. 3

    You’re migrating a legacy form system that uses JS to manage focus states. How would you approach replacing those with :focus-within while ensuring backward compatibility and accessibility compliance?

8+ years experience
  1. 1

    We’re standardizing accessibility patterns across 20+ product teams. Should :focus-within be mandated as the primary focus indicator for form containers? What cross-team adoption risks, legacy browser constraints, or testing infrastructure gaps would you surface?

  2. 2

    A major redesign is replacing all JS-based focus managers with CSS-only solutions using :focus-within. How would you architect the rollout plan, measure success, and handle regressions in enterprise systems with complex shadow DOMs and third-party widgets?

  3. 3

    Your team is building a design system for a global financial platform. How would you balance the elegance of :focus-within against the need for consistent, testable, and WCAG-compliant focus states across 50+ form variants and 12 supported languages?

Follow-up Questions

  • How would you test this in a real browser with only a keyboard?
  • What happens if you nest multiple focus-within containers?
  • Could this interfere with a custom focus indicator system?