CSS :optional Selector

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

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

CSS Syntax

syntax.css
:optional { … }

Often scoped: input:optional, select:optional, textarea:optional.

Selector Parts

PartMeaningTry it
:optionalControl has no required attributeTry it
input:optionalOptional inputs onlyTry it
select / textareaSame rule for other form controlsTry it
:requiredOpposite state when required is setTry it

How to Use :optional

Mark required fields in HTML, then style both states in CSS.

  1. Add required only on fields that must be filled.
  2. Leave other controls without required (they match :optional).
  3. Style :optional and/or :required for a clear visual difference.
  4. Also show required status in the label text for accessibility.
GoalTypical selector
Soft optional lookinput:optional
Emphasize requiredinput:required
Optional selectselect:optional
Optional notes fieldtextarea:optional

Live Preview

Toggle which state to highlight. Styles use real :optional / :required rules.

Highlight

Name is required. Email is optional. Only the matching state lights up.

Result

Highlighting :optional

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.

CSS
<style>
  input:optional {
    border: 2px dashed #64748b;
  }
</style>

<input type="text" required value="Required">
<input type="email" value="[email protected]">
Try It Yourself

How It Works

  1. Omit required. The email field has no required attribute.
  2. Match with :optional. Only that field gets the dashed border.
  3. Required stays out. The first input has required, so it does not match.
  4. 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.

CSS
<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>
Try It Yourself

How It Works

  1. Set HTML truth. Name has required; email does not.
  2. Style both pseudo-classes. Blue for optional, red for required.
  3. Keep a text cue. The asterisk on Name supports accessibility.
  4. 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.

CSS
<style>
  select:optional,
  textarea:optional {
    border: 2px dashed #64748b;
  }
</style>

<select>
  <option>Optional topic</option>
</select>
<textarea>Optional notes</textarea>
Try It Yourself

How It Works

  1. Same rule set. select and textarea support required too.
  2. No attribute = optional. Both controls match :optional.
  3. Shared style. One rule styles both with a dashed border.
  4. Add required later. Then they would match :required instead.

Edge Cases & Pitfalls

If optional styling looks wrong, check these first.

default

Most fields are already optional

You may only need :required styles. Use :optional when optional fields need a distinct look.

types

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

a11y

Do not use color alone

Pair border styles with label text such as “optional” or a required asterisk.

aria

aria-required is not the same

:optional / :required follow the HTML required attribute, not ARIA alone.

valid

Optional is not the same as valid

An optional email can still be :invalid if the typed value is malformed.

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.

97% Global support
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.

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.

Next: :out-of-range

Style number and date controls whose value sits outside min/max.

:out-of-range tutorial →

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.

8 people found this page helpful