Open In App
Related Articles

CSS optional Selector

Improve Article
Save Article
Like Article

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 that are not declared as “required”. 


:optional {
      /* css declarations; */



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



Supported Browser: 

  • Google Chrome 10.0
  • Edge 12.0
  • Firefox 4.0
  • Safari 5.0
  • Opera 10.0

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 06 Jul, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials