CSS :optional Selector

Definition
Definition and Usage
The :optional pseudo-class matches any <input>, <select>, or <textarea> that does not have the required attribute.
Use it to style non-required fields. Its opposite is :required. Fields are optional by default until you add required.
Standard-track.Baseline widely available since July 2015. Not deprecated, experimental, or non-standard.
Accessibility a11y
Mark required fields with the required attribute and visible text or an asterisk—do not rely on color alone (MDN).
Syntax
CSS Syntax
Often scoped: input:optional, select:optional, textarea:optional.
Parts
Selector Parts
| Part | Meaning | Try it |
|---|
:optional | Control has no required attribute | Try it |
input:optional | Optional inputs only | Try it |
select / textarea | Same rule for other form controls | Try it |
:required | Opposite state when required is set | Try it |
Approach
How to Use :optional
Mark required fields in HTML, then style both states in CSS.
- Add
required only on fields that must be filled. - Leave other controls without
required (they match :optional). - Style
:optional and/or :required for a clear visual difference. - Also show required status in the label text for accessibility.
| Goal | Typical selector |
|---|
| Soft optional look | input:optional |
| Emphasize required | input:required |
| Optional select | select:optional |
| Optional notes field | textarea:optional |
Try it
Live Preview
Toggle which state to highlight. Styles use real :optional / :required rules.
Code
CSS Examples
Each snippet focuses on :optional. Use Try It Yourself for a live editor.
Example 1 — Style Optional Inputs
Give non-required inputs a dashed border.
<style>
input:optional {
border: 2px dashed #64748b;
}
</style>
<input type="text" required value="Required">
<input type="email" value="[email protected]">
How It Works
- Omit
required. The email field has no required attribute. - Match with
:optional. Only that field gets the dashed border. - Required stays out. The first input has
required, so it does not match. - Default is optional. Most fields match until you mark them required.
Example 2 — Optional vs Required Together
Style both states so users can tell them apart quickly.
<style>
:optional {
border: 2px solid #1d4ed8;
}
:required {
border: 2px solid #b91c1c;
}
</style>
<label>Name *<input type="text" required></label>
<label>Email <input type="email"></label>
How It Works
- Set HTML truth. Name has
required; email does not. - Style both pseudo-classes. Blue for optional, red for required.
- Keep a text cue. The asterisk on Name supports accessibility.
- Pair with validation. Later add
:valid / :invalid if needed.
Example 3 — Optional Select and Textarea
The same pseudo-class works on other required-capable controls.
<style>
select:optional,
textarea:optional {
border: 2px dashed #64748b;
}
</style>
<select>
<option>Optional topic</option>
</select>
<textarea>Optional notes</textarea>
How It Works
- Same rule set.
select and textarea support required too. - No attribute = optional. Both controls match
:optional. - Shared style. One rule styles both with a dashed border.
- Add
required later. Then they would match :required instead.
Edge Cases & Pitfalls
If optional styling looks wrong, check these first.
defaultMost fields are already optional
You may only need :required styles. Use :optional when optional fields need a distinct look.
typesNot every input supports required
hidden, range, color, and button types are not required-capable the same way. Stick to text-like inputs, checkboxes/radios, select, and textarea.
a11yDo not use color alone
Pair border styles with label text such as “optional” or a required asterisk.
ariaaria-required is not the same
:optional / :required follow the HTML required attribute, not ARIA alone.
validOptional is not the same as valid
An optional email can still be :invalid if the typed value is malformed.
Compatibility
Browser Support
:optional is a browser CSS pseudo-class for form controls. Node.js, Deno, and Bun do not evaluate it.
✓ Baseline · Widely available since Jul 2015
:optional
Supported in Chrome 15+, Firefox 4+, Safari 5+, Edge 12+, Opera 15+, IE 10+. Pair with the HTML required attribute.
Google Chrome 15+
Full support
Mozilla Firefox 4+
Full support
Apple Safari 5+
Full support
Microsoft Edge 12+
Full support
Opera 15+
Full support
Samsung Internet 4+
Full support
Internet Explorer 10+
Full support:optional Widely supported
Bottom line: Safe for production forms. Prefer text cues plus styles for required vs optional.
Remember
Key Takeaways
Role: matches input / select / textarea without required.
Common use: softer borders on optional fields next to required ones.
vs :required: opposites based on the HTML required attribute.
Pitfall: color-only cues fail accessibility—label required fields in text too.
One line: use :optional to style form controls that are not marked required.
Frequently Asked Questions
It matches input, select, and textarea elements that do not have the required attribute — fields the user may leave empty.
Form controls that support required: input, select, and textarea. It does not style divs, buttons, or other non-form elements.
:optional matches fields without required. :required matches fields that have required set. They are opposites for those controls.
Yes. Omit required and the control matches :optional. Add required and it matches :required instead.
No. MDN recommends marking required fields with text or an icon as well as styles, so meaning is not color-only.
Widely available since July 2015: Chrome 15+, Firefox 4+, Safari 5+, Edge 12+, IE 10+. It is a browser CSS selector, not a Node.js API.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful