:optional
Example
input:optional {
opacity: 0.5;
}label:has(+ input:optional)::after {
content: ' (optional)';
color: #555;
}A text hint marks the field as optional without making it look disabled or hard to read.
Common mistakes
- Expecting
:optionalto match buttons or custom controls built fromdivelements. - Styling optional fields faintly so they look disabled.
Key points
:optionaltargetsinput,selectandtextareaelements without therequiredattribute.- An optional field is labeled as optional in text, not only by a style difference.
Interview questions
Is :optional the opposite of :required?
Yes, for form fields. A field that can be required matches exactly one of :required and :optional.
Should I mark required or optional fields?
Mark whichever is rarer in the form, and say it in text so every user notices.