CSS Selector - CSS :optional

The :optional selector selects form elements with no required attribute.

The :optional selector only applies to the form elements: input, select and textarea.

We can use the :required selector to select form elements which are required.


CSS Version

CSS Syntax

:optional {
   style properties 

Browser compatibility

:optional Yes 10.0 Yes Yes Yes


<!DOCTYPE html>
input:optional {<!--  ww w  . j  a  v  a 2s.  com-->
    background-color: red;
    <p>An optional input element:<br><input></p>
    <p>A required input element:<br><input required></p>


Click to view the demo