The :optional pseudo-class targets form fields that are not required — those without the required attribute.
01
Not required
No required attr.
02
input
Text, email.
03
select
Dropdowns.
04
textarea
Long text.
05
vs :required
Opposites.
06
Form UX
Visual cues.
Fundamentals
Introduction
The :optional selector in CSS is used to target form elements that are not required — meaning those that do not have the required attribute.
This pseudo-class is particularly useful for differentiating optional and mandatory fields, helping users understand which inputs they can skip.
Definition and Usage
Write :optional or scope it to a control type like input:optional to style only optional input fields.
💡
Beginner Tip
Fields are optional by default. Only elements with required match :required instead. Pair both pseudo-classes for a clear required vs optional form design.
Foundation
📝 Syntax
The syntax for the :optional pseudo-class is:
syntax.css
:optional{/* CSS properties */}input:optional{/* optional inputs only */}
:optional works with form elements that can have the required attribute:
<input> — text, email, number, tel, url, and other input types
Without required, the textarea matches :optional. A dashed border signals that filling it in is not mandatory.
Example 4 — Registration form with mixed fields
Combine :required and :optional across a full signup form.
optional-form.css
form input:required,
form select:required{border-left:4px solid #dc2626;}form input:optional,
form select:optional,
form textarea:optional{border-left:4px solid #2563eb;opacity:0.95;}
A left border accent gives a quick visual scan: red for required, blue for optional. Labels should still include text like “(optional)” for accessibility.
Tips
💬 Usage Tips
Pair with :required — Create a consistent two-tier visual system.
Scope by type — input:optional avoids styling all form controls.
Add label text — Do not rely on color alone; write “(optional)” in labels.
Combine with :focus — input:optional:focus for focus ring on optional fields.
Check contrast — Optional styling should still meet WCAG requirements.
Watch Out
⚠️ Common Pitfalls
Wrong elements — :optional has no effect on <button> or <div>.
All fields optional by default — Forgetting required makes every field match :optional.
Color-only cues — Always supplement with label text for screen reader users.
Overriding :invalid — Optional fields can still be invalid if format rules fail.
Hidden required fields — display:none required fields still affect form validation.
A11y
♿ Accessibility
Label every field — Use <label for="..."> linked to each input.
Text indicators — Include “required” or “optional” in visible label text.
Do not rely on color alone — Pair border colors with words or icons.
aria-required — Use aria-required="true" on required fields for assistive tech.
Contrast — Optional field borders and backgrounds must meet WCAG contrast ratios.
🧠 How :optional Works
1
Checks element type
The browser verifies the element can have a required attribute.
Eligible
2
Reads required attribute
If required is absent, the field is considered optional.
No required
3
Applies :optional styles
CSS rules using :optional match and style the field.
Style
=
📝
Clearer form UX
Users can quickly see which fields they may skip.
Compatibility
🖥 Browser Compatibility
:optional is supported in all modern browsers and has been available since HTML5 form validation.
✓ Baseline · Universal support
Form state selectors everywhere
:optional works reliably in Chrome, Firefox, Safari, Edge, and Opera.
99%Universal support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll modern versions
Full support
:optional pseudo-class99% supported
Bottom line::optional is safe for form UX in all modern projects.
Wrap Up
🎉 Conclusion
The CSS :optional selector provides an easy way to style form elements that do not require user input. It helps users quickly distinguish optional from mandatory fields.
Paired with :required, it builds intuitive, accessible forms without extra JavaScript or wrapper classes.
The :optional pseudo-class matches form elements that do not have the required attribute — meaning user input is not mandatory for those fields.
:optional applies to form controls that can have the required attribute: input, select, and textarea. It does not match buttons, divs, or other non-form elements.
:optional matches fields without the required attribute. :required matches fields that have required set. They are opposites for form fields that support the attribute.
Yes. If you omit the required attribute, the field is optional and matches :optional. Only fields with required explicitly set match :required instead.
Yes. All modern browsers support :optional. It has been available since HTML5 form validation and works reliably alongside :required, :valid, and :invalid.