The :valid pseudo-class styles form elements whose values pass HTML5 validation. Pair it with :invalid to give users instant green success feedback when their input is correct.
01
Passes rules
Valid value.
02
Green UI
Success style.
03
:invalid
Error pair.
04
email
type check.
05
min/max
Range check.
06
Real-time
As you type.
Fundamentals
Introduction
The CSS :valid pseudo-class matches form controls whose current value satisfies all HTML5 validation constraints. When a user enters a correct email, a number within range, or fills a required field properly, that input becomes :valid and your success styles apply.
It works on input, textarea, and select elements that have validation attributes like required, type="email", min/max, or pattern.
Definition and Usage
Use :valid to reinforce correct input with green borders, light green backgrounds, or checkmark icons. It is the positive counterpart to :invalid and together they create a complete real-time validation UX without JavaScript for basic forms.
💡
Beginner Tip
Always style both :valid and :invalid together. Users need to see red when something is wrong and green when they fix it — that contrast makes forms much easier to complete.
Scoping to input[type="email"] limits validation styling to email fields only. The green box-shadow gives a subtle success glow when the format is correct.
📄 Range & Forms
Validate numbers and build complete form success states.
Example 3 — Number range validation
Style age inputs green when the value falls within min and max.
Scoping to .signup-form keeps validation colors inside the form component. Each field independently toggles between :valid and :invalid as the user types.
Tips
💬 Usage Tips
Pair with :invalid — Always style both states for complete feedback.
Use green sparingly — Light green backgrounds work better than loud neon.
Scope selectors — Use input:valid instead of bare :valid when possible.
Delay error styling — Show :invalid only on :focus or after first blur to avoid red fields on load.
Add HTML constraints — :valid only works when inputs have required, type, or pattern.
Combine with labels — Visual color cues supplement, but never replace, accessible error messages.
Watch Out
⚠️ Common Pitfalls
Empty optional fields — Inputs with no constraints may match :valid even when empty.
Red on page load — Empty required fields are :invalid immediately; delay error display.
Not for all elements — :valid only works on form controls, not div or p.
Over-reliance on CSS — Complex validation still needs JavaScript or server-side checks.
Color-only feedback — Do not rely solely on green/red; add text error messages.
Browser differences — Test validation behavior across Chrome, Firefox, and Safari.
A11y
♿ Accessibility
Use visible labels — Every field needs a <label>; color alone is not enough.
Announce errors — Pair CSS with aria-invalid and descriptive error text for screen readers.
Do not rely on color — Add icons or text like “Valid email” alongside green borders.
Maintain contrast — Green text on light green backgrounds must still meet WCAG guidelines.
Focus management — Move focus to the first invalid field on submit for keyboard users.
🧠 How :valid Works
1
HTML constraints set
The input has required, type="email", or min/max attributes.
HTML
2
User enters a value
The browser checks the value against all constraint validation rules.
Check
3
Value passes validation
If all rules pass, the element matches :valid.
Match
=
✓
Green success styles apply
Your :valid CSS gives the user positive confirmation their input is correct.
Compatibility
🖥 Browser Compatibility
The :valid pseudo-class is supported in all modern browsers with HTML5 constraint validation.
✓ Baseline · Modern browsers
Form validation styling everywhere
:valid works in Chrome, Firefox, Safari, Edge, and Opera alongside native HTML5 validation.
99%Global support
Google Chrome10+ · Desktop & Mobile
Full support
Mozilla Firefox4+ · Desktop & Mobile
Full support
Apple Safari5+ · macOS & iOS
Full support
Microsoft Edge12+ / 79+ Chromium
Full support
Opera11+
Full support
:valid pseudo-class99% supported
Bottom line: Safe for modern form validation UX. Pair with :invalid for complete feedback.
Wrap Up
🎉 Conclusion
The :valid pseudo-class is a valuable tool for form validation in CSS. It lets you provide real-time positive feedback when users enter correct data, creating a more intuitive and user-friendly form experience.
Paired with :invalid, it ensures users can easily identify and correct mistakes. Remember to add accessible labels and error messages alongside your color-based validation styling.
Rely on CSS alone for security-critical validation
Use color as the only success/error indicator
Apply :valid to non-form elements
Show red invalid fields before the user types
Forget server-side validation on submit
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :valid
Use these points when styling form success states.
5
Core concepts
✓01
Passes rules
Valid value.
Purpose
:inv02
Pair invalid
Error twin.
Pattern
🎨03
Green UI
Success cue.
Style
input04
Form only
Not div/p.
Scope
🌐05
99% support
All browsers.
Compat
❓ Frequently Asked Questions
The :valid pseudo-class matches form elements whose current value passes HTML5 validation rules — such as a properly formatted email, a number within min/max, or a filled required field.
Validation constraints like required, type (email, url, number), pattern, min, max, minlength, and maxlength determine whether an input matches :valid or :invalid.
:required only checks if a field is mandatory. :valid checks whether the current value actually satisfies all validation rules. An empty required field is :required but :invalid.
Yes. Use :valid for success styling (green border) and :invalid for error styling (red border) so users get clear feedback as they type.
Yes. :valid is supported in all modern browsers alongside HTML5 constraint validation. Empty optional fields with no constraints may match :valid by default.