← All Exercises
Reveal Field Errors with :has() and :user-invalid
intermediatecss
Background
:invalid matches before interaction, while :user-invalid waits for user input; :has lets a parent react to a child's state.
Problem
Style a form field so its error message appears and its border turns red only when the input is user-invalid, never on first paint before the user interacts.
Input
An email field with a hidden inline error message.
Output
CSS that reveals errors from field state without JavaScript.
Sample tests
Sample 1
Input: Form loads with an empty required email
Expected: the error stays hidden because :user-invalid does not match before interaction
Sample 2
Input: User types an invalid address and leaves the field
Expected: .field:has(input:user-invalid) shows the error paragraph and colours the border
Your solution
Learning objective
Gate validation styling behind user interaction with :user-invalid and style parent wrappers using :has.