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.

Summary

CSS Version
3

CSS Syntax

:optional {
   style properties 
}




Browser compatibility

:optional Yes 10.0 Yes Yes Yes

Example

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

</body>
</html>

Click to view the demo