Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Pseudo-classes

The :user-invalid pseudo-class

Reference for the CSS :user-invalid pseudo-class: show validation errors only after the user has interacted with a field, instead of on page load.

The :user-invalid pseudo-class matches a form control that fails validation and that the user has interacted with. It solves the biggest annoyance of :invalid: required fields turning red before anyone has typed a character.

Browsers usually start matching after the user changes the value and leaves the field, or after a submit attempt. Fields the user never touched stay unstyled.

CSS
input:user-invalid {
  border-color: #dc2626;
}

Try it

Errors that wait for you

Syntax

CSS
:user-invalid { /* declarations */ }
  • Applies to: form controls that take part in constraint validation.
  • Specificity: (0,1,0).
  • Inherited / animatable: not applicable.

Values

Syntax and arguments Description
:user-invalid No arguments. Matches a validated control that currently fails a constraint and has been interacted with by the user.

Usage notes

  • No JavaScript needed. Before this pseudo-class, authors added classes on blur to get the same effect.
  • Programmatic changes don't count. Setting a value from script is not user interaction. Neither is reportValidity() by itself on a pristine field in every browser; test it.
  • Keep the message visible. Colour alone is not an error message. Pair it with text next to the field, and consider aria-describedby.
  • Fallback. For older browsers, rely on a baseline such as :invalid:not(:placeholder-shown), which works when inputs have a placeholder. Put the fallback and the new rule in separate rules.
  • Resetting. A form reset clears the interaction state again.

Browser support

Supported in current versions of Chrome, Edge, Safari and Firefox (Firefox formerly had :-moz-ui-invalid). It is a recent feature, so check your support targets.