CKTIS / CSS
:invalid — an element failing validation constraints
The CSS :invalid pseudo-class matches elements that fail data-validation constraints. It can highlight invalid values or empty required fields, including before the user has interacted with them.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
:invalid:invalid is a pseudo-class, not a report of selector syntax errors. In HTML it matches controls that are candidates for constraint validation but fail their constraints. The selector input:invalid restricts matching to input elements.
Matching can begin immediately: an empty field with required does not need a previous form submission. :user-invalid additionally considers user-interaction-related validity state; it is not another name for :invalid. Elements without validation semantics match neither :invalid nor :valid, so :not(:valid) is not equivalent to :invalid.
HTML also covers containers: form:invalid matches a form that owns at least one invalid control eligible for validation, while fieldset:invalid matches a group containing such a descendant. This does not make every ancestor of an invalid field match.
The example checks an empty required field and the value Ada, then restores the empty value. Expected text is provided separately; the example requires local verification. In an interface, do not rely on color alone: provide an understandable error message.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>:invalid example</title>
<style>
input:invalid { outline: 3px solid crimson; }
</style>
<label for="name">Name (required)</label>
<input id="name" required autocomplete="off">
<pre id="result"></pre>
<script>
const input = document.getElementById('name');
const results = [];
input.value = '';
results.push(`empty: ${input.matches(':invalid')}`);
input.value = 'Ada';
results.push(`filled: ${input.matches(':invalid')}`);
input.value = '';
document.getElementById('result').textContent = results.join('\n');
</script>
</html>Result
empty: true filled: false
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.