Sub topic 12
Styling valid and invalid states
This chapter assumes you know the built-in validation attributes (required, pattern, min, max) and basic CSS selectors. Here you will learn how to style controls based on whether they pass validation.
The validity pseudo-classes
The browser tracks whether each control satisfies its constraints. CSS exposes that state through pseudo-classes:
| Pseudo-class | Matches |
|---|---|
| `:valid` | A control that meets all its constraints |
| `:invalid` | A control that fails at least one constraint |
| `:required` | A control with the `required` attribute |
| `:optional` | A control without `required` |
| `:in-range` / `:out-of-range` | A number or date control inside or outside `min` and `max` |
| `:user-invalid` / `:user-valid` | Validity, but only after the user has interacted with the control |
The problem with plain :invalid
:invalid applies from the moment the page loads. An empty required field is already invalid, so a rule like this paints every untouched field red before anyone types:
input:invalid {
border: 2px solid #c62828;
}Users see errors for fields they have not seen yet. The fix is to show invalid styling only after interaction.
Option 1: :user-invalid
:user-invalid matches only after the user has changed the control or tried to submit the form. It is the simplest solution, and current major browsers support it. Check support for your audience before relying on it.
input:user-invalid {
border: 2px solid #c62828;
}
input:user-valid {
border: 2px solid #2e7d32;
}Option 2: a placeholder trick
Before :user-invalid was available, a common workaround combined :placeholder-shown with :not(). It requires a placeholder, even a single space, on each input.
input:not(:placeholder-shown):invalid {
border: 2px solid #c62828;
}This styles a field as invalid only once it contains text. It misses one case: a required field the user focused and then left empty stays unstyled.
Combining style with accessible feedback
Color alone is not enough. Some users cannot tell red from green, so pair color with another signal, such as an icon, a border style, or visible text.
<label for="age">Age (18 to 99)</label>
<input id="age" name="age" type="number" min="18" max="99" required>
<span class="hint">Enter a number from 18 to 99.</span>input:user-invalid {
border: 2px solid #c62828;
background: #fdecea;
outline-offset: 2px;
}
input:user-invalid + .hint {
color: #c62828;
font-weight: 600;
}
input:user-invalid:focus-visible {
outline: 3px solid #c62828;
}The adjacent sibling selector (+) changes the hint beside an invalid control. Keep a visible focus outline. Do not remove it to make the border look cleaner.
Common errors
- Using
:invalidalone, which flags every empty required field on load. - Relying on color as the only indicator of an error.
- Removing
outlineon focus without a replacement. - Using
:placeholder-shownwithout aplaceholderattribute. The selector never matches, so the rule does nothing. - Styling
:validgreen on fields where "valid" only means "empty and optional", which suggests the user did something right.
Exercise
A signup form has an empty required email field. You write input:invalid { border-color: red; }. Why is the field red as soon as the page loads, and which selector fixes it?
Show answer
An empty required field already fails its constraint, so :invalid matches at load. Use input:user-invalid, which matches only after the user edits the field or tries to submit. As a fallback for older browsers, use input:not(:placeholder-shown):invalid with a placeholder attribute.