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

How can you style invalid form inputs without JavaScript?

Styling Invalid Form Inputs Using :invalid Pseudo-Class in CSS

You can style invalid form inputs directly using the :invalid pseudo-class in CSS. It allows you to provide visual feedback for inputs that fail HTML5 validation constraints without needing JavaScript.

Key Points About :invalid
  1. 1

    :invalid – Selects form elements whose current value does not satisfy HTML validation requirements (e.g., required fields, pattern mismatches, min/max values).

  2. 2

    :valid – Can be used alongside :invalid to style inputs that pass validation.

  3. 3

    Styles update dynamically as users interact with the form, giving immediate feedback.

Example: Styling Invalid Inputs

In this example, the email and age fields automatically receive a red border and light red background when their values are invalid, and a green border with light green background when valid, giving clear visual cues to the user.

Best Practices
  1. 1

    Combine :invalid with :valid to give real-time validation feedback.

  2. 2

    Use additional pseudo-classes like :required or :optional for better user guidance.

  3. 3

    Ensure that color cues are accessible; consider adding icons or text messages for users with color vision deficiencies.

  4. 4

    Test across browsers, as some older browsers may not fully support HTML5 validation pseudo-classes.

Difficulty: 3/10
Topics: CSS pseudo-classes, form validation styling, native HTML validation

Scenario Questions

0-2 years experience
  1. 1

    How would you make a required email input turn red when the user submits the form without entering anything?

  2. 2

    What CSS rule would you write to highlight a phone input as invalid if it doesn't match a 10-digit pattern?

2-5 years experience
  1. 1

    A form input is visually invalid even though the user entered valid data — what could be wrong with the CSS or HTML structure?

  2. 2

    You're told users aren't noticing invalid fields on mobile — how would you improve visibility without JavaScript, and what tradeoffs might you consider?

5-8 years experience
  1. 1

    How would you design a reusable form component library that styles invalid inputs consistently across 20+ forms, while accommodating different design systems and accessibility requirements?

  2. 2

    A legacy form uses inline styles and JavaScript for validation — how would you migrate to pure CSS-based validation without breaking existing behavior or accessibility?

8+ years experience
  1. 1

    You're leading a cross-team initiative to standardize form validation UX across 50+ products — how do you balance CSS-only styling with accessibility, internationalization, and legacy browser support?

  2. 2

    How would you architect a validation styling system that scales across micro-frontends, ensuring consistency without shared CSS tooling, and what metrics would you track to measure success?

Follow-up Questions

  • What happens if the form input is valid but the user hasn't interacted with it yet?
  • How would you style a field that's required but empty versus one that has invalid content?
  • Can you style invalid inputs differently across browsers? What's your fallback?