The :invalid pseudo-class matches a form control whose contents fail HTML constraint validation.
Typical cases: a bad type="email" value, an empty required field, a pattern mismatch, or a number outside min/max. Pair it with :valid for clear success/error feedback.
Baseline · Widely available.
Not deprecated. Standard HTML / Selectors Level 4 pseudo-class. Widely available since July 2015.
Accessibility Do not rely on color alone
Red borders help, but add text or an icon so color-blind users still understand the error. Prefer delaying harsh styles until interaction when possible.
Syntax
CSS Syntax
syntax.css
:invalid {/* styles when validation fails */ }
Often combined with a type or constraint: input[type="email"]:invalid or input:required:invalid.
Add a pattern.[a-z]+ allows only lowercase letters.
Start with a mismatch.Test1 has uppercase and a digit.
:invalid fires. The outline shows the constraint failure.
Type a match. Something like ok becomes valid.
Edge Cases & Pitfalls
:invalid follows HTML validation rules — timing and messaging matter as much as color.
timing
Errors can appear too early
Empty required fields match :invalid on load. Prefer :user-invalid (or focus / placeholder tricks) so errors wait until the user interacts.
vs :valid
Style both states
Pair with :valid so users see when a fix worked, not only when something is wrong.
range
:out-of-range is narrower
:out-of-range covers min/max style failures. :invalid also covers type, pattern, required, and more.
a11y
Color is not enough
Add helper text or an icon next to the field. Do not convey invalid state with red alone.
Compatibility
Browser Support
:invalid is a browser CSS pseudo-class. Node.js, Deno, and Bun do not evaluate it.
✓ Baseline · Widely available since Jul 2015
:invalid
Chrome 10+, Edge 12+, Firefox 4+, Safari 5+, Opera 10+, Samsung Internet 4+, Internet Explorer 10+.
97%Global reach
Google Chrome10+
Full support
Mozilla Firefox4+
Full support
Apple Safari5+
Full support
Microsoft Edge12+
Full support
Opera10+
Full support
Samsung Internet4+
Full support
Internet Explorer10+
Full support
:invalidWidely supported
Bottom line: Safe for production form styling. Combine with accessible text, not color alone. It is not a server runtime API.
Remember
Key Takeaways
Role: style controls that fail HTML validation.
Triggers:required, type, pattern, min/max, and more.
Pair with :valid: show both error and success states.
Practice: delay harsh errors; never use color alone.
One line: let HTML constraints decide validity, then style failures with :invalid.
Frequently Asked Questions
It matches a form control whose current value fails HTML constraint validation — for example a bad email, an empty required field, or a number outside min/max.
Typically input, select, and textarea. A form element can also match :invalid when any of its controls are invalid.
:invalid matches failed constraints. :valid matches when the control satisfies its constraints. Pair them for error and success styling.
An empty required control fails validation immediately, so :invalid matches before the user types. Prefer :user-invalid or focus/placeholder techniques to delay error chrome.
:out-of-range only covers values outside min/max (and similar range constraints). :invalid is broader and includes type, pattern, required, and more.
No. It is a CSS pseudo-class evaluated by the browser from HTML constraint validation, not a Node.js or server runtime feature.