Sub topic 7
Styling valid and invalid fields
This chapter assumes you know basic CSS selectors, how the browser validates built-in constraints, and how to read validity on a control. Here you will use CSS pseudo-classes to show validation state without flashing errors on page load.
The validation pseudo-classes
The browser tracks each control's validity and exposes it to CSS. These are the pseudo-classes you will use most.
| Pseudo-class | Matches |
|---|---|
| `:valid` | A control that satisfies all its constraints, from the first render |
| `:invalid` | A control that fails at least one constraint, from the first render |
| `:user-valid` | A valid control, but only after the user has interacted with it |
| `:user-invalid` | An invalid control, but only after the user has interacted with it |
| `:required` | A control with the `required` attribute |
| `:optional` | A control without `required` |
| `:in-range` and `:out-of-range` | Controls with `min` or `max` whose value is inside or outside the range |
The difference between :invalid and :user-invalid is the most common source of mistakes.
The problem with :invalid
An empty required field is invalid the moment the page loads. If you style :invalid with a red border, the learner sees a form full of errors before typing anything.
/* Avoid: every empty required field turns red on page load */
input:invalid {
border: 2px solid #c00;
}:user-invalid fixes this. It starts matching only after the user has interacted with the control, typically after leaving it (blur) or after a submit attempt.
input:user-invalid {
border: 2px solid #c00;
}
input:user-valid {
border: 2px solid #080;
}
input:required {
border-left-width: 6px;
}The :required rule is independent of validity. It marks mandatory fields from the start, so users know what to fill in before any error appears.
Seeing the states change
The following diagram shows how a required field moves between states, and which selectors match in each.
Do not rely on color alone
A red or green border is invisible to some users, including people with color vision deficiency and people using a screen reader. Combine color with a second signal such as an icon, text, or a border style change. The CSS can only supply part of that. The error text itself belongs in the markup, which a later chapter covers.
You can also pair the pseudo-class with an aria-invalid attribute selector if your script sets it:
input:user-invalid,
input[aria-invalid="true"] {
border: 2px solid #c00;
background-image: linear-gradient(#fdd, #fdd);
}Selecting on aria-invalid also covers fields your JavaScript validated with noValidate, where the browser may not apply :user-invalid the same way.
Showing messages with sibling selectors
You can reveal a hint next to a field only when it is in the user-invalid state. Place the hint right after the input, then use the adjacent sibling combinator.
<label for="username">Username</label>
<input id="username" name="username" required minlength="3" pattern="[a-z0-9_]+">
<span class="hint">Use at least 3 lowercase letters, digits, or underscores.</span>.hint {
display: none;
color: #c00;
}
input:user-invalid + .hint {
display: block;
}Note that display: none hides content from assistive technology too. That is fine for a decorative hint. For a real error message, a later chapter shows a pattern that screen readers will announce.
Common errors
- Styling
:invalidand getting red fields on first load. Use:user-invalid. - Expecting
:user-invalidto work in older browsers. Support is recent, so check your target browsers. A fallback is to toggle a class from JavaScript afterblur. - Using color as the only indicator of an error.
- Forgetting that
:validalso matches optional empty fields, so a green border on an empty optional field is misleading.
Exercise
A signup form shows every required field with a red border as soon as the page loads. Which selector is probably responsible, and what should replace it?
Show answer
The rule uses :invalid, which matches empty required fields immediately. Replace it with :user-invalid, which matches only after the user has interacted with the field.
Exercise
Write a CSS rule that adds a thicker left border to all required inputs, regardless of whether they are valid, and explain why this helps.
Show answer
input:required { border-left-width: 6px; }. It marks mandatory fields before any validation runs, so users know what to complete without seeing error styling.