# :optional

> The `:optional` CSS pseudo-class applies to form elements that don't have the `required` attribute set.

## What does the CSS :optional selector match?

The pseudo-class that selects form elements that are not required and can be left empty. It helps style optional form fields differently from required ones, making it easier to indicate which fields are necessary for submission.

- Topic: Pseudo Classes
- Type: pseudo-class
- Browser support: Chrome 10+, Chrome Android 18+, Edge 12+, Firefox 4+, Firefox Android 4+, Safari 5+, Safari iOS 5+

## Example

Wrong:

```css
input:optional {
  opacity: 0.5;
}
```

Right:

```css
label:has(+ input:optional)::after {
  content: ' (optional)';
  color: #555;
}
```

A text hint marks the field as optional without making it look disabled or hard to read.

## Common mistakes

- Expecting `:optional` to match buttons or custom controls built from `div` elements.
- Styling optional fields faintly so they look disabled.

## Key points

- `:optional` targets `input`, `select` and `textarea` elements without the `required` attribute.
- An optional field is labeled as optional in text, not only by a style difference.

## Interview questions

### Is :optional the opposite of :required?

Yes, for form fields. A field that can be required matches exactly one of `:required` and `:optional`.

### Should I mark required or optional fields?

Mark whichever is rarer in the form, and say it in text so every user notices.

## Related cards

- [:required](https://wonderium.dev/css/required.md)
- [:has()](https://wonderium.dev/css/has.md)
- [`<label>`](https://wonderium.dev/html/label.md)
- [Accessible Forms](https://wonderium.dev/a11y/accessible-forms.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/optional
- Last updated: 2026-11-25
