CSS | optional Selector

The :optional selector in CSS is used to select and style the form input elements which are optional. That is it can select those input elements from an HTML form which are not declared as “required”.

Syntax:

:optional {
  /* css declarations; */
}

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>optional Selector</title>
          
        <style>
            /* Select optional fields and change 
                color to Yellow */
            input:optional {
                background-color: yellow;
            }
              
            h1 {
                color:green;
            }
              
            h1, h2 {
                text-align:center;
            }
        </style>
    </head>
  
    <body>
        <h1>GeeksForGeeks</h1>
          
        <h3>A demonstration of the :optional selector.</h3>
          
        <!-- Create an HTML form -->
        <form>
            <!-- Optional Input -->
            <label>An optional input element:</label><br />
            <input>
              
            <br /><br />
              
            <!-- Required Input -->
            <label>A required input element:</label><br />
            <input required>
        </form>
          
        <p>
            The :optional selector selects form elements
            with no "required" attribute.
        </p>
    </body>
</html>                    

chevron_right


Output:

Supported Browser: The browser supported by “:optional” selector are listed below:

  • Google Chrome 50.0, 4.0 -webkit-
  • Internet Explorer 10.0
  • Firefox 52.0, 2.0 -moz-
  • Safari 9.0, 3.1 -webkit-
  • Opera 37.0, 15.0 -webkit-, 11.1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.