Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Pseudo-classes

The :valid pseudo-class

Reference for the CSS :valid pseudo-class: style form controls whose value passes constraint validation, with live examples, pitfalls and a modern alternative.

The :valid pseudo-class matches form controls (and forms and fieldsets) whose value passes HTML constraint validation: the required, type, pattern, min, max, minlength, maxlength and step rules are all satisfied. It updates live as the user types.

The catch is timing. A field with no constraints is valid from the start, and a field is evaluated even before the user touches it. For feedback after interaction, see :user-valid.

CSS
input:valid {
  border-color: #16a34a;
}

Try it

Valid border while typing

Syntax

CSS
:valid { /* declarations */ }
  • Applies to: <input>, <select>, <textarea>, <fieldset>, <form> and form-associated custom elements that take part in validation.
  • Specificity: (0,1,0).
  • Inherited / animatable: not applicable.

Values

Syntax and arguments Description
:valid No arguments. Matches an element whose constraint validation finds no problem. A <form> or <fieldset> matches when all its controls are valid.

Usage notes

  • Valid from the start. Optional empty fields are valid immediately, so a green border on every untouched optional field looks odd. Combine with :not(:placeholder-shown) or use :user-valid.
  • Same constraints as :invalid. Any control that qualifies for validation is either :valid or :invalid.
  • Not everything validates. Disabled controls, read-only inputs, hidden inputs and buttons are barred from validation and match neither.
  • Custom messages. setCustomValidity("text") makes a control invalid until you call it with an empty string.
  • Server validation. CSS reflects only client-side rules. Always validate on the server too.
  • Accessibility. A green border is not enough on its own; pair success states with text or an icon, and don't announce success noisily.

Browser support

Supported in every browser.