A form field that fails validation rarely needs only its own border changed. The label should turn red, the help text should appear, the icon should swap, and the whole group might want a tinted background. Until recently, that required JavaScript to find the parent and add an error class, because CSS could only select downward. The :has() pseudo-class changes that, and combined with :user-invalid it produces parent-aware error states from CSS alone.

Both pieces are now safe to rely on. MDN lists :user-invalid as Baseline since November 2023 and :has() as Baseline since December 2023, which puts :has() beyond the 30-month mark that Baseline uses for its Widely available label by mid-2026.

Why :user-invalid and Not :invalid

The older :invalid pseudo-class matches as soon as the element fails its constraints. A required empty field is invalid on page load, so a form styled with :invalid greets the visitor with a screen of red before they have typed anything.

:user-invalid waits. Per MDN, it matches an invalid field after the user has interacted with it or attempted to submit. The practical result is that errors appear when they are relevant. Its counterpart :user-valid does the reverse for confirmation styling.

The Core Pattern

Wrap each label and control in a container, then let the container react to the state of the control inside it.

.field:has(:user-invalid) {
  --accent: #b42318;
  background: #fef3f2;
}

.field:has(:user-invalid) label {
  color: var(--accent);
}

.field:has(:user-invalid) .hint {
  display: block;
}

The .hint element, which holds the instructions or error text, can stay hidden by default and appear only for the failing group. No class is toggled by a script, and no blur listener exists to get out of sync with the browser’s own validity state.

Styling the valid case

.field:has(:user-valid) gives you a success state. Use it sparingly. A green check on every completed field is noise for most forms, and for some users color alone does not convey the change.

Selecting Across Siblings

Because :has() accepts relative selectors, it also handles layouts where the control is not a descendant of the group. A label that precedes its input can be styled with a lookahead:

label:has(+ input:user-invalid) {
  color: #b42318;
}

That removes the need for wrapper elements in simple forms. For fieldsets, fieldset:has(:user-invalid) can mark an entire group of radio buttons, which is difficult to do any other way since a radio group is only invalid when the required choice is missing.

Form-Level Summaries

The same idea applies at the form level. A rule such as form:has(:user-invalid) .submit-note can reveal a “please fix the highlighted fields” message, and form:has(:invalid) button[type="submit"] can dim a submit button. Be careful with the second pattern. Disabling a button gives no explanation of what is wrong, and a disabled control is skipped by keyboard navigation. A more reliable approach leaves the button active and lets validation messages do the work.

What CSS Cannot Do

Styling is not announcing. A red border and a swapped icon are visual only, and WCAG 2.2 success criterion 3.3.1 requires that errors be identified in text. Browsers’ native validation bubbles provide that on submit. If you replace them with custom messages, connect the message to the field with aria-describedby and set aria-invalid appropriately, which generally requires either the novalidate pattern with scripted logic or a design that keeps native messages. Color must not be the only cue either, so include text and an icon with a text alternative.

CSS also cannot change the content of a validation message. The native text comes from the browser and is localized by it. If you need custom wording, that is a job for the Constraint Validation API, and :has(:user-invalid) can still handle the visual treatment around it.

Performance and Scoping

MDN’s guidance on :has() is to avoid broad anchors. A selector like body:has(:user-invalid) or *:has(input) forces the browser to re-evaluate large parts of the tree when the DOM changes. Anchor the rule on a narrow class and use child combinators where the structure allows it:

.field:has(> input:user-invalid) { /* narrower than .field:has(input:user-invalid) */ }

Form groups are small subtrees, so the cost is modest, but the habit of specific anchors is worth keeping. Also note that :has() cannot be nested inside another :has(), and pseudo-elements are not valid inside it.

Fallbacks and Testing

If :has() is unsupported, a rule containing it is dropped whole. In a browser that old, the field still gets its own :user-invalid border from a separate rule, so write the child-only style first and the parent-aware enhancement after it:

input:user-invalid {
  border-color: #b42318;
}

.field:has(:user-invalid) {
  background: #fef3f2;
}

Do not put both selectors in one comma-separated list, because an unsupported selector invalidates the entire list. Test by submitting an empty form, then filling in a field wrongly, correcting it, and tabbing away. Confirm that the error state clears at the moment you expect, and run through it once with a screen reader to verify the text messages are announced.

Keeping the Markup Honest

The CSS only works as well as the underlying constraints. Use real attributes such as required, type="email", pattern, min, and max, and the browser will supply the validity state that :user-invalid reads. A field validated entirely in script, with no constraint attributes, never matches the pseudo-class, and the parent rule will never fire. Declarative constraints also keep the validation logic visible in the markup, where reviewers can read it.

Practical Takeaway

Wrap each label and control in a small container, style the control itself with :user-invalid, and style the container with :has(:user-invalid) for labels, hints, and backgrounds. Keep the child rule separate from the parent rule, anchor :has() on a specific class, and treat the visual state as a layer on top of real text errors tied to the field through ARIA. That gives you the interaction polish of a scripted form without a listener to maintain.