CSS :valid Selector

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 4 Examples
Form Validation

What You’ll Learn

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.

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.

📝 Syntax

The syntax for the :valid pseudo-class is:

syntax.css
:valid {
  /* CSS properties */
}

Scope to specific input types or form contexts:

scoped-valid.css
input:valid {
  border-color: #16a34a;
  background: #f0fdf4;
}

input[type="email"]:valid {
  box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.2);
}
:valid { } input:valid :invalid required

Validation Triggers

AttributeExample:valid when
requiredrequiredField has a non-empty value
type="email"you@example.comEmail format is correct
min / maxmin="18" max="60"Number is within range
patternpattern="[A-Z]{2}\d{4}"Value matches regex

Syntax Rules

  • Only applies to form controls: input, textarea, select.
  • The element needs at least one validation constraint to meaningfully toggle between :valid and :invalid.
  • Pair with :invalid for complete validation feedback.
  • Use input:focus:invalid or :not(:placeholder-shown):invalid to delay error styling.
  • Optional fields with no constraints often match :valid even when empty.

Related Topics

⚡ Quick Reference

QuestionAnswer
Selector typePseudo-class (validation)
What it targetsForm elements passing HTML5 validation
Common pattern:valid { border: 2px solid #16a34a; background: #f0fdf4; }
Natural pair:invalid for error styling
Works oninput, textarea, select
Browser supportAll modern browsers

When to Use :valid

:valid improves form UX in these scenarios:

  • Registration forms — Show green checkmarks when email and password meet rules.
  • Checkout flows — Confirm card number format and zip code as users type.
  • Survey inputs — Highlight completed numeric age or rating fields.
  • Search filters — Indicate valid date ranges in filter forms.
  • Multi-step wizards — Enable the next step only when current fields are :valid.

👀 Live Preview

Enter a valid email and an age between 18–65. Correct values turn green; invalid values turn red:

Examples Gallery

Practice :valid with success/error pairs, email fields, number ranges, and signup form layouts.

📜 Core Patterns

Style valid form fields with green success feedback.

Example 1 — Basic :valid and :invalid styling

Apply green to valid fields and red to invalid ones in a contact form.

valid-form.css
:valid {
  border: 2px solid #16a34a;
  background-color: #f0fdf4;
}

:invalid {
  border: 2px solid #dc2626;
  background-color: #fef2f2;
}

input {
  padding: 0.5rem;
  border-radius: 0.4rem;
  width: 100%;
}
Try It Yourself

How It Works

When the user enters a valid email and age within range, :valid applies green styling. Invalid or empty required fields match :invalid instead.

Example 2 — Email field success state

Add a green glow to email inputs that pass format validation.

valid-email.css
input[type="email"]:valid {
  border-color: #16a34a;
  background: #f0fdf4;
  box-shadow: 0 0 0 3px rgba(22, 163, 74, 0.2);
}

input[type="email"]:invalid {
  border-color: #dc2626;
  background: #fef2f2;
}
Try It Yourself

How It Works

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.

valid-number.css
input[type="number"]:valid {
  border-color: #16a34a;
  background: #f0fdf4;
}

input[type="number"]:invalid {
  border-color: #dc2626;
  background: #fef2f2;
}
Try It Yourself

How It Works

An input with min="18" max="60" becomes :valid when the entered number is within that range. Values like 10 or 99 match :invalid instead.

Example 4 — Signup form with validation feedback

Wrap a signup form and style all valid inputs with a consistent green theme.

valid-signup.css
.signup-form input:valid {
  border-color: #16a34a;
  background: #f0fdf4;
}

.signup-form input:invalid {
  border-color: #dc2626;
  background: #fef2f2;
}

.signup-form input {
  padding: 0.5rem 0.75rem;
  border: 2px solid #cbd5e1;
  border-radius: 0.4rem;
  width: 100%;
}
Try It Yourself

How It Works

Scoping to .signup-form keeps validation colors inside the form component. Each field independently toggles between :valid and :invalid as the user types.

💬 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.

⚠️ 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.

♿ 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.

🖥 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 Chrome 10+ · Desktop & Mobile
Full support
Mozilla Firefox 4+ · Desktop & Mobile
Full support
Apple Safari 5+ · macOS & iOS
Full support
Microsoft Edge 12+ / 79+ Chromium
Full support
Opera 11+
Full support
:valid pseudo-class 99% supported

Bottom line: Safe for modern form validation UX. Pair with :invalid for complete feedback.

🎉 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.

💡 Best Practices

✅ Do

  • Always pair :valid with :invalid
  • Use subtle green borders and light backgrounds
  • Scope to input:valid or form wrappers
  • Delay :invalid styling until focus or blur
  • Add visible labels and error message text

❌ Don’t

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about :valid

Use these points when styling form success states.

5
Core concepts
:inv 02

Pair invalid

Error twin.

Pattern
🎨 03

Green UI

Success cue.

Style
input 04

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.

Practice in the Live Editor

Open the HTML editor and experiment with :valid, :invalid, and real-time form validation styling.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful