CSS Reference ยท Pseudo-classes
The :optional pseudo-class
Reference for the CSS :optional pseudo-class: match input, select and textarea elements without the required attribute, with examples and usage notes.
The :optional pseudo-class matches <input>, <select> and <textarea> elements that do not have the required attribute. It is the counterpart of :required.
Since most forms have more optional than required fields, it is usually clearer to mark the exception with :required and leave the rest alone, but :optional is useful for a gentle "(optional)" hint.
CSS
input:optional { background: #f8fafc; }
Try it
Syntax
CSS
:optional { /* declarations */ }
- Applies to:
<input>,<select>and<textarea>withoutrequired. - Specificity: (0,1,0).
- Inherited / animatable: not applicable.
Values
| Syntax and arguments | Description |
|---|---|
:optional |
No arguments. Matches a control that can take required but does not have it. |
Usage notes
- Always paired. A control where
requiredapplies is either:requiredor:optional, never both. - Buttons and hidden inputs. Input types that can never be required (such as
hidden,rangeorsubmit) match:optional, because the attribute simply does not apply to them. Narrow the selector, for exampleinput:optional:not([type="hidden"]), if that matters. - Not for other elements.
<div>and<label>never match. - Prefer marking the exception. Putting "(optional)" on most fields adds noise; it works best when optional fields are the minority.
- Text matters. Don't signal optional state through colour alone.
Browser support
Supported in every browser.
Related
:required:enabled:has()- Lesson: Styling Forms