CSS Portal

:user-invalid CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The :user-invalid pseudo-class represents a form control that the user has interacted with and whose current value does not satisfy its validation constraints. Unlike automatic validation states, this pseudo-class only becomes active after user interaction* making it ideal for showing validation feedback without immediately flagging untouched fields.

This pseudo-class is part of the user interaction–aware validation states introduced to improve usability and accessibility in modern forms.

How :user-invalid Works

The :user-invalid pseudo-class applies when all of the following are true:

  1. The element is a form control (such as an input, textarea, or select).

  2. The user has interacted with the element (for example, typed into it or blurred it).

  3. The value fails validation, such as:

    • Violating required
    • Failing a pattern
    • Being outside min / max
    • Not matching a required type like email

This makes it different from :invalid, which applies immediately, even before user interaction.

Why Use :user-invalid?

Using :user-invalid improves user experience by:

  • Preventing error styles from appearing on page load
  • Showing feedback only after the user interacts
  • Making validation feel less aggressive and more intuitive

It is especially useful for modern forms that guide users gently rather than confronting them immediately.

Basic Example

<form>
  <label>
    Email:
    <input type="email" required>
  </label>
</form>
input:user-invalid {
  border: 2px solid red;
  background-color: #ffecec;
}

In this example, the red styling only appears after the user enters an invalid email and leaves the field.

  • :user-valid Applies when the user has interacted with the field and the value is valid.

  • :invalid Applies immediately when the value is invalid - even before user interaction.

  • :focus Applies when an element is currently focused and is often combined with validation styling.

Combining with Other Selectors

You can combine :user-invalid with attribute selectors or input types for more control:

input[type="email"]:user-invalid {
  border-color: red;
}

Or combine with focus styling:

input:user-invalid:focus {
  outline: 2px solid red;
}

Accessibility Considerations

While visual feedback is helpful, it’s best paired with accessible messaging:

  • Use clear error text near the field
  • Consider aria-invalid="true" for assistive technologies
  • Avoid relying on color alone to convey errors

Syntax

:user-invalid {
  /* ... */
}

Example

<div class="field">
<label for="email">Email Address:</label>
<input type="email" id="email" required placeholder="example@domain.com">
<span class="error-msg">Please enter a valid email address.</span>
</div>

<div class="field">
<label for="username">Username (min 5 chars):</label>
<input type="text" id="username" minlength="5" required>
<span class="error-msg">Username is too short.</span>
</div>

<button type="submit">Submit</button>
/* Hide error messages by default */
.error-msg {
display: none;
color: #d93025;
font-size: 0.8rem;
margin-top: 5px;
}

/* Styles applied ONLY after user interaction */
input:user-invalid {
border: 2px solid #d93025;
background-color: #fff2f2;
}

/* Show the error message only when the input is :user-invalid */
input:user-invalid + .error-msg {
display: block;
}

/* Optional: Style for valid input after interaction */
input:user-valid {
border: 2px solid #1e8e3e;
}

/* Basic Layout Styling */
.field {
margin-bottom: 20px;
display: flex;
flex-direction: column;
width: 300px;
}

input {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
}

Browser Support

The following information will show you the current browser support for the CSS :user-invalid pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.

This psuedo class is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!