The optional pseudo-class

The optional pseudo-class specifies the style of an input that does not have the required attribute specified, i.e. this pseudo-class applies to optional inputs.

Syntax

cssselector:optional {

}

Example

Let's give the optional input a gray background:

html<form>
  <p>
    <label>Enter number</label>
    <input type="number" required>
  </p>
  <p>
    <label>Enter text</label>
    <input type="text">
  </p>
</form>
cssinput:optional {
  background-color: #D8DFE8;
}

Результат:

See also

  • the :default pseudo-class
    that sets a default style for elements
  • the :indeterminate pseudo-class
    that styles switches that are in an indeterminate state
  • the :placeholder-shown pseudo-class
    that sets a style for elements when displaying tooltip text
  • the :read-only pseudo-class
    that sets a style for inputs that cannot be changed
  • the :required pseudo-class
    that sets a style of the input that has the required attribute set
  • the :focus-within pseudo-class
    that styles the elements in focus
  • the caret-color property
    that sets a color of the text cursor in input