CSS :optional Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Form States

What You’ll Learn

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.

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.

📝 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
  • <select> — dropdown menus
  • <textarea> — multi-line text areas

When It Matches

HTMLMatches :optional?
<input type="email">Yes (no required attribute)
<input type="text" required>No (has required)
<textarea>Yes (optional by default)
<button type="submit">No (cannot be required)
:optional input:optional textarea:optional select:optional

Related Selectors

  • :required — opposite; matches fields with the required attribute
  • :valid / :invalid — validation state styling
  • :enabled / :disabled — interactivity state
  • [required] — attribute selector alternative for required fields

⚡ Quick Reference

QuestionAnswer
Selector typeUI state pseudo-class
Syntax:optional or input:optional
Matches whenElement lacks the required attribute
Valid elementsinput, select, textarea
Common useSoften optional fields, pair with :required styling
Browser supportAll modern browsers

When to Use :optional

:optional improves form clarity and user experience:

  • Required vs optional contrast — Blue borders for optional, red for required.
  • Signup forms — Highlight optional fields like phone number or company name.
  • Surveys — Visually soften questions users can skip.
  • Scoped stylingtextarea:optional for comment boxes only.
  • With :required — Build a consistent two-tier form visual system.

👀 Live Preview

Required name field has a red border; optional email field has a blue border via :required and :optional:

Examples Gallery

Practice :optional with required/optional pairs, scoped inputs, textareas, and full registration forms.

📜 Core Patterns

Differentiate required and optional form fields with paired styling.

Example 1 — Style optional and required fields

Give optional fields a blue border and required fields a red border.

optional-required.css
:optional {
  border: 2px solid #2563eb;
  background: #eff6ff;
}

:required {
  border: 2px solid #dc2626;
  background: #fef2f2;
}
Try It Yourself

How It Works

The name input has required, so it gets red styling. The email input lacks required, so :optional applies the blue treatment.

Example 2 — Scope to input fields only

Use input:optional so selects and textareas are not affected.

input-optional.css
input:optional {
  border-color: #94a3b8;
  background: #f8fafc;
}

input:required {
  border-color: #dc2626;
}
Try It Yourself

How It Works

Scoping to input limits the rule to input elements. A optional <select> in the same form would keep its default styling.

📄 Textareas & Full Forms

Apply optional styling to comment boxes and multi-field registration forms.

Example 3 — Soften optional textarea fields

Style optional comment boxes with a subtle border and background.

textarea-optional.css
textarea:optional {
  border: 1px dashed #94a3b8;
  background: #f8fafc;
  min-height: 5rem;
}
Try It Yourself

How It Works

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;
}
Try It Yourself

How It Works

A left border accent gives a quick visual scan: red for required, blue for optional. Labels should still include text like “(optional)” for accessibility.

💬 Usage Tips

  • Pair with :required — Create a consistent two-tier visual system.
  • Scope by typeinput:optional avoids styling all form controls.
  • Add label text — Do not rely on color alone; write “(optional)” in labels.
  • Combine with :focusinput:optional:focus for focus ring on optional fields.
  • Check contrast — Optional styling should still meet WCAG requirements.

⚠️ 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 fieldsdisplay:none required fields still affect form validation.

♿ 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.

🖥 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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All modern versions
Full support
:optional pseudo-class 99% supported

Bottom line: :optional is safe for form UX in all modern projects.

🎉 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.

💡 Best Practices

✅ Do

  • Pair :optional with :required for contrast
  • Add “(optional)” text in labels
  • Scope with input:optional when needed
  • Keep optional styling softer than required
  • Test with screen readers and keyboard nav

❌ Don’t

  • Apply to buttons or non-form elements
  • Rely on border color alone for meaning
  • Forget required on mandatory fields
  • Assume optional means no validation rules
  • Use low-contrast optional field colors

Key Takeaways

Knowledge Unlocked

Five things to remember about :optional

Use these points when styling form fields.

5
Core concepts
in 02

input/select

Form controls.

Elements
03

vs :required

Opposites.

Compare
def 04

Default

Optional by default.

Behavior
🌐 05

99% support

All browsers.

Compat

❓ Frequently Asked Questions

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.

Practice in the Live Editor

Open the HTML editor and experiment with :optional and :required form styling.

HTML Editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful