Form validity pseudo-classes
The form validity CSS pseudo-classes match <form> elements based on the constraints of a form field, such as validity (:valid, :invalid, :in-range, :out-of-range) and necessity (:optional or :required).
Overview
The form validity CSS pseudo-classes match <form> elements based on the constraints of a form field, such as validity (:valid, :invalid, :in-range, :out-of-range) and necessity (:optional or :required).
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 10 | 13 | 29 | 5.1 | 18 | 5 | |
| Other | ||||||
| `:invalid` | 10 | 12 | 4 | 5 | 18 | 5 |
| CSS selector | ||||||
form Applies to `<form>` elements | 40 | 79 | 13 | 9 | 40 | 9 |
| Other | ||||||
| `:optional` | 10 | 12 | 4 | 5 | 18 | 5 |
| `:out-of-range` | 10 | 13 | 29 | 5.1 | 18 | 5 |
| `:required` | 10 | 12 | 4 | 5 | 18 | 5 |
| `:valid` | 10 | 12 | 4 | 5 | 18 | 5 |
| CSS selector | ||||||
form Applies to `<form>` elements | 40 | 79 | 13 | 9 | 40 | 9 |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Notes 1 item(s)
Implementation note
- Before Chrome 52, `:in-range` matched disabled and read-only inputs (see bug 41248615). In Chrome 52, it was changed to only match enabled read-write inputs.
Notes 1 item(s)
Implementation note
- Before Firefox 50, `:in-range` matched disabled and read-only inputs (see bug 1264157). In Firefox 50, it was changed to only match enabled read-write inputs.
Notes 1 item(s)
Implementation note
- In Safari, `:in-range` matched disabled and read-only inputs (see bug 156530). It was later changed to only match enabled read-write inputs.
Notes 1 item(s)
Implementation note
- Before Chrome Android 52, `:in-range` matched disabled and read-only inputs (see bug 41248615). In Chrome Android 52, it was changed to only match enabled read-write inputs.
Notes 1 item(s)
Implementation note
- In Safari on iOS, `:in-range` matched disabled and read-only inputs (see bug 156530). It was later changed to only match enabled read-write inputs.
Syntax
CSS
input:valid { border-color: #22c55e; }
input:invalid { border-color: #ef4444; }
input:required { border-left: 3px solid #3b82f6; }
input:out-of-range { background: #fef2f2; } Live demo
Use cases
Using Form validity pseudo-classes
The form validity CSS pseudo-classes match <form> elements based on the constraints of a form field, such as validity (:valid, :invalid, :in-range, :out-of-range) and necessity (:optional or :required).
Cautions
- May not be supported in older browsers.
Accessibility
- Make sure visual changes are conveyed appropriately to assistive technology.
Related links
Powered by web-features