CKTIS / CSS
:optional — optional form controls
The CSS :optional pseudo-class selects form controls that do not require a value. It does not mean “any element without required” or “an empty field”: matching depends on the control type and HTML rules.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
:optional:optional is a pseudo-class without arguments for styling optional form controls. In HTML, it matches select and textarea elements without the required attribute, and input elements to which required applies that are not required.
It is not a replacement for :not([required]): that selector can also select ordinary elements that are not form controls at all. input types to which required does not apply do not match :optional. :required describes the opposite state for applicable controls, but not every element must match one of these two pseudo-classes.
Optionality does not depend on whether the user has already entered a value. Nor does it guarantee that a supplied value is valid. CSS only selects and styles elements; it does not change the rules for requiring a value. Adding or removing required can change matching.
In the example, input:optional selects only nickname, gives it a green outline, and allows its identifier to be displayed. name has required, so it is not selected. Visible labels communicate optionality without relying on color.
Examples
<!doctype html>
<meta charset="utf-8">
<title>:optional</title>
<style>
input:optional { outline: 2px solid green; }
</style>
<label>Nickname (optional): <input id="nickname" type="text"></label>
<label>Name (required): <input id="name" type="text" required></label>
<pre id="result"></pre>
<script>
const ids = [...document.querySelectorAll('input:optional')]
.map(element => element.id);
document.getElementById('result').textContent = ids.join(', ');
</script>Result
nickname
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.