The :invalid pseudo-class styles form fields whose values fail validation rules. It provides instant visual feedback during data entry.
01
Failed rules
Bad input.
02
required
Empty fields.
03
type & pattern
Email, regex.
04
:valid
Success pair.
05
Real-time
Live feedback.
06
HTML5
No JS needed.
Fundamentals
Introduction
The :invalid selector in CSS is used to select form elements whose values fail to meet validation criteria. It is especially useful for styling inputs with incorrect or incomplete data based on type, pattern, required, or range constraints.
This selector helps enhance form usability and provides visual feedback to users as they type.
Definition and Usage
Use :invalid on its own or scoped to elements: input:invalid, textarea:invalid, or input[type="email"]:invalid. Styles apply while the field’s value does not satisfy its validation constraints.
💡
Beginner Tip
Pair :invalid with :valid for red/green feedback. To avoid showing errors on empty fields before the user types, use input:invalid:not(:placeholder-shown) or input:invalid:focus.
Foundation
📝 Syntax
The syntax for the :invalid pseudo-class is:
syntax.css
:invalid{/* CSS properties */}
The :invalid selector applies to <input>, <textarea>, and <select> elements with built-in validation rules.
While the placeholder is visible (field empty), :not(:placeholder-shown) prevents the invalid style. Once the user types, validation feedback kicks in.
Example 4 — Pattern validation for phone number
Use the pattern attribute with :invalid to validate a specific format.
Security — Never rely on CSS or client-side validation alone for data integrity.
A11y
♿ Accessibility
Do not rely on color alone — Pair red borders with text error messages linked via aria-describedby.
Associate labels — Every input needs a <label> so screen readers announce the field name.
aria-invalid — Set aria-invalid="true" on invalid fields for assistive technology (often via JS).
Clear error text — Explain what is wrong (“Enter a valid email”) not just that the field is red.
Contrast — Error borders and backgrounds must meet WCAG contrast requirements.
🧠 How :invalid Works
1
HTML defines rules
Attributes like required, type="email", or pattern set constraints.
HTML
2
Browser validates value
On input or submit, the browser checks the value against all constraints.
Check
3
:invalid matches
If any rule fails, the element matches :invalid and CSS applies.
Style
=
❌
Instant error feedback
Users see immediately that their input needs fixing.
Compatibility
🖥 Browser Compatibility
The :invalid pseudo-class is supported in all modern browsers.
✓ Baseline · Universal support
Form validation styling everywhere
:invalid works in Chrome, Firefox, Safari, Edge, and Opera.
99%Universal support
Google Chrome10+ · Desktop & Mobile
Full support
Mozilla Firefox4+ · Desktop & Mobile
Full support
Apple Safari5+ · macOS & iOS
Full support
Microsoft Edge12+
Full support
Opera11+
Full support
:invalid pseudo-class99% supported
Bottom line::invalid is safe for modern form validation UI. Always validate on the server too.
Wrap Up
🎉 Conclusion
The :invalid selector is a key component of modern form design, allowing developers to provide instant feedback when users enter invalid data.
Pair it with :valid, use the placeholder-shown trick to avoid premature errors, and always back up client-side styling with server-side validation and accessible error messages.
The :invalid pseudo-class matches form elements whose current value fails HTML5 validation rules — such as an empty required field, a malformed email, or a number outside min/max.
Common triggers include required, type (email, url, number, etc.), pattern, min, max, minlength, maxlength, and step. The input must have at least one constraint for :invalid to apply meaningfully.
:invalid is broader — it covers any validation failure including empty required fields and bad email format. :out-of-range only checks whether a numeric or date value is outside min/max.
Yes. Use :invalid for error styling (red border) and :valid for success styling (green border) so users get clear feedback when they fix their input.
:invalid is supported in all modern browsers. Note that empty required fields may match :invalid immediately on page load — consider :focus or :not(:placeholder-shown) to delay error styling.