Stop the Red Borders on Page Load: :user-valid / :user-invalid

October 8, 2026  |  8 min read  |  CSS

By Syed · Frontend Engineer & Creator of UITweak

You add input:invalid { border-color: red } to a signup form, reload, and every required field glows red before the user has typed a single character. So you do what everyone does: track a touched flag per field in JavaScript, or chain the :not(:placeholder-shown) hack that breaks the moment a field has no placeholder. Both are workarounds for a problem CSS solved natively: :user-invalid and :user-valid match only after the user has interacted with the field. Baseline since late 2023, zero gating risk, and your touched-state code can go away.

30-second version: replace :invalid with :user-invalid (and :valid with :user-valid) in your validation styles. Empty required fields stay calm on page load; the moment the user blurs or commits a value, the pseudo-classes kick in and then track every keystroke live. Supported in Chrome 119+, Firefox 88+, Safari 16.5+.

The Problem: :invalid Yells on Page Load

:invalid is a pure state match: an empty required input is invalid, so it matches immediately. That is technically correct and terrible UX. The old workarounds each carry a tax:

  • JS touched flags: an onBlur handler per field, state per form, reimplemented in every project.
  • The placeholder chain: input:not(:placeholder-shown):invalid only works when the input actually has a placeholder, and it misfires on selects and checkboxes.
  • Validate-on-submit-only: no live feedback at all, the worst of both worlds.

:user-invalid is the browser keeping the touched flag for you, per element, for free.

Recipe 1: The Drop-In Replacement

/* calm on load, honest after interaction */
.field:user-invalid {
  border-color: #dc2626;
  background: #fef2f2;
}

.field:user-valid {
  border-color: #16a34a;
}

.field:focus-visible {
  outline: 2px solid #4f46e5;
  outline-offset: 1px;
}

That is the whole migration for most forms: swap the pseudo-class names. A pristine required field shows your neutral border. The user tabs through it empty, or types something wrong and blurs, and from that moment the styles apply and keep updating as they type. It tracks live after first interaction, which is exactly the behavior hand-rolled touched flags were approximating.

Recipe 2: Error Messages Without JavaScript

Pair the field state with a sibling error message and the entire validation UI becomes declarative:

<label>
  Email
  <input class="field" type="email" required />
  <span class="error">Enter a valid email address.</span>
</label>
.error {
  display: none;
  color: #dc2626;
  font-size: 0.875rem;
}

/* show the message only once the user has interacted */
.field:user-invalid + .error {
  display: block;
}

No conditional rendering, no error-state variables. The message appears exactly when the red border does, because both key off the same interaction-aware state.

Recipe 3: Style the Whole Wrapper with :has()

Real forms wrap the label, input, hint, and error in a field container. :has() lets the container react to the input inside it:

.field-group:has(.field:user-invalid) .field-label {
  color: #dc2626;
}

.field-group:has(.field:user-invalid) {
  --field-ring: rgb(220 38 38 / 0.25);
}

The label, the hint icon, the container ring: all of them can respond to the interaction-aware state without a single line of component state. This is the recipe that finally kills the touched object most form libraries keep around.

It Tracks Live After First Interaction

One subtlety worth knowing: :user-invalid is not "match once on blur and freeze". After the first interaction (blur, Enter, or form submission), the pseudo-class re-evaluates on every keystroke. Type a valid email into a flagged field and the red border clears while you type. Delete it again and the error returns. That live tracking is better than most hand-rolled touched logic, which tends to either never clear or clear too eagerly.

The Fallback (and Killing the Placeholder Chain)

Support is wide enough that many teams ship unguarded: Chrome 119+, Edge 119+, Firefox 88+, Safari 16.5+, all Baseline widely available since November 2023. If you still cover older browsers, detect the selector form (not the property) with @supports:

/* legacy path: the old placeholder chain, scoped as fallback */
@supports not (selector(:user-invalid)) {
  .field:not(:placeholder-shown):invalid {
    border-color: #dc2626;
  }
}

@supports (selector(:user-invalid)) {
  .field:user-invalid {
    border-color: #dc2626;
  }
}

Notice what this does to the old hack: the :not(:placeholder-shown) chain survives only as the legacy branch, quarantined behind the negated query. New browsers never see it. When you drop the old browser floor, you delete one block instead of untangling hacks from your main styles.

Tailwind users: there is a user-invalid: variant you probably have not used: user-invalid:border-red-500 compiles to the real pseudo-class. It pairs with the usual user-valid: variant. If you are building the form in Tailwind, you get this entire guide in utility form with zero custom CSS.

Quick Recap

  1. :invalid matches on page load. :user-invalid waits for interaction, then tracks live.
  2. Swap the pseudo-class names. That is most of the migration.
  3. Show error messages with a sibling selector keyed off :user-invalid. No JS error state.
  4. Style wrappers with :has(.field:user-invalid) for labels, hints, and rings.
  5. Fallback: @supports (selector(:user-invalid)), with the old placeholder chain quarantined in the negated branch.
  6. Tailwind has user-invalid: and user-valid: variants built in.

# Expansion draft: css-user-valid-user-invalid-fix-guide

Screen Readers Do Not Hear Pseudo-Classes

Here is the gap the main guide does not mention: :user-invalid is pure presentation. A screen reader never announces "this field is invalid" because a CSS selector matched. The visual red border and the assistive-technology signal are two separate systems, and shipping only the CSS half leaves screen-reader users with a form that looks validated to everyone but them.

The honest fix is a small amount of JavaScript that mirrors the visual state into ARIA:

form.addEventListener("focusout", (e) => {
  const field = e.target;
  if (!field.matches("input, select, textarea")) return;
  const invalid = !field.checkValidity();
  field.setAttribute("aria-invalid", String(invalid));
});

Pair it with aria-describedby pointing at the same .error element the CSS reveals, so the message text gets announced with the field. The CSS from the main guide stays exactly as is. This is the division of labor I use on every form: CSS owns what it looks like, ARIA owns what gets announced, and a few lines of JS keep them in sync.

setCustomValidity Plays Along for Free

Custom validation logic feeds the same validity state these pseudo-classes read. If you call input.setCustomValidity("Username is taken"), the field becomes invalid, and :user-invalid matches after interaction exactly as it does for built-in constraints like required or type="email". Clear it with setCustomValidity("") and the field goes back to normal.

This matters because it means you do not need a parallel styling system for server-driven errors. The flow I use: submit, get the 422 response, call setCustomValidity on the offending fields, and the existing :user-invalid styles light up with the server's message in the sibling .error element. One visual language for client and server errors, zero extra CSS.

The Optional Empty Field Trap

An empty optional field is valid. That is correct per the spec, and it creates a styling surprise: the user tabs through an optional "Company" field without typing, and on blur it matches :user-valid and earns your green success border. A green border on a field the user never touched reads as a bug.

I handle it by scoping the success style to fields that actually have a value:

/* success only when the field is non-empty */
.field:user-valid:not(:placeholder-shown) {
  border-color: #16a34a;
}

This works for text inputs with placeholders. For selects and checkboxes, which have no placeholder, I scope :user-valid styling to required fields only, or skip the green state entirely and style just the invalid case. An absent success style is never a bug; a success style on an untouched field always looks like one.

novalidate Silences Everything

Adding novalidate to the <form> disables constraint validation for the whole form, and that takes :user-valid and :user-invalid down with it. Neither pseudo-class will ever match, because the browser no longer computes validity at all. The styles do not partially apply; they vanish.

This bites teams that add novalidate to take over validation with JavaScript, then wonder why their CSS validation styles stopped working. If you go the novalidate route, you own the visuals too: toggle classes or aria-invalid from your JS and style those instead. If you want the pseudo-classes, leave native validation on and layer your JS on top with setCustomValidity. Mixing the two (native CSS states plus a novalidate form) is the configuration that silently does nothing.

Frequently Asked Questions

What counts as "interaction"?

Blurring the field, pressing Enter, changing a checkbox or select, or attempting to submit the form. Mere focus does not count, so tabbing through fields never triggers error styles on its own.

Does it work on selects and checkboxes?

Yes, and this is where it beats the placeholder chain hardest. Selects and checkboxes have no placeholder, so the old hack never applied to them. :user-invalid works on every form-associated element uniformly. Two companion upgrades for the same form: style the select dropdown itself with our base-select guide, and make textareas grow with their content using field-sizing: content.

Which browsers support it?

Baseline widely available since November 2023: Chrome and Edge 119+, Firefox 88+, Safari 16.5+. Firefox actually shipped it earliest (version 88, April 2021).

Can I still validate on submit only?

Attempting submission counts as interaction, so a submit with untouched invalid fields lights them up exactly once, then they track live. You get submit-time validation and live validation from the same two declarations.

Share this guide