Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Pseudo-classes

The :optional pseudo-class

Reference for the CSS :optional pseudo-class: match input, select and textarea elements without the required attribute, with examples and usage notes.

The :optional pseudo-class matches <input>, <select> and <textarea> elements that do not have the required attribute. It is the counterpart of :required.

Since most forms have more optional than required fields, it is usually clearer to mark the exception with :required and leave the rest alone, but :optional is useful for a gentle "(optional)" hint.

CSS
input:optional {
  background: #f8fafc;
}

Try it

Hint on optional fields

Syntax

CSS
:optional { /* declarations */ }
  • Applies to: <input>, <select> and <textarea> without required.
  • Specificity: (0,1,0).
  • Inherited / animatable: not applicable.

Values

Syntax and arguments Description
:optional No arguments. Matches a control that can take required but does not have it.

Usage notes

  • Always paired. A control where required applies is either :required or :optional, never both.
  • Buttons and hidden inputs. Input types that can never be required (such as hidden, range or submit) match :optional, because the attribute simply does not apply to them. Narrow the selector, for example input:optional:not([type="hidden"]), if that matters.
  • Not for other elements. <div> and <label> never match.
  • Prefer marking the exception. Putting "(optional)" on most fields adds noise; it works best when optional fields are the minority.
  • Text matters. Don't signal optional state through colour alone.

Browser support

Supported in every browser.