CKTIS / CSS
:user-invalid — invalid fields after interaction
The :user-invalid pseudo-class selects invalid form controls only after interaction warrants showing an error, such as an attempted form submission. It helps avoid marking untouched fields as erroneous immediately when a page opens.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
:user-invalid:user-invalid is a pseudo-class without arguments. In HTML, it matches input, textarea, and select elements that are candidates for constraint validation, fail their constraints, and have their internal user validity state set to true. That state name does not mean the value is valid.
Unlike :invalid, it accounts for interaction history: an empty field with required can be invalid on loading without yet matching :user-invalid. Attempting to submit the form is an important point for revealing errors; do not assume merely gaining and losing focus always suffices. Earlier activation details depend on HTML rules and the browser.
Every element matching :user-invalid also matches :invalid. In contrast, :user-valid concerns valid input after appropriate interaction; it is not the simple negation of :user-invalid, because an untouched field can match neither.
The example combines a border with error text rather than relying on color alone. CSS responds to validation state, but does not itself define constraints or replace server-side data validation.
Examples
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Showing errors with :user-invalid</title>
<style>
input { border: 2px solid #555; }
input:user-invalid { border-color: #b00020; }
.error { display: none; }
input:user-invalid + .error { display: block; }
</style>
</head>
<body>
<form id="demo">
<label for="email">Email (required)</label>
<input id="email" type="email" required aria-describedby="email-error">
<p id="email-error" class="error">Enter a valid email address.</p>
<button type="submit">Check</button>
<button type="reset">Reset</button>
</form>
<script>
const form = document.querySelector('#demo');
const field = document.querySelector('#email');
const error = document.querySelector('#email-error');
// Keep validation local, even if the field contains a valid address.
form.addEventListener('submit', event => event.preventDefault());
field.addEventListener('invalid', event => event.preventDefault());
function report(stage) {
const visible = getComputedStyle(error).display !== 'none';
console.log(`${stage}: :invalid=${field.matches(':invalid')}, :user-invalid=${field.matches(':user-invalid')}, error=${visible ? 'visible' : 'hidden'}`);
}
report('Before validation');
form.requestSubmit();
report('After empty submit attempt');
form.reset();
report('After reset');
</script>
</body>
</html>Result
Before validation: :invalid=true, :user-invalid=false, error=hidden After empty submit attempt: :invalid=true, :user-invalid=true, error=visible After reset: :invalid=true, :user-invalid=false, error=hidden
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.