Style element with no "required" attribute with CSS


Use the CSS :optional selector to style <input> elements with no "required" attribute.

Example

You can try to run the following code to implement the :optional selector:

Live demo

<!DOCTYPE html>
<html>
   <head>
      <style>
         input:optional {
            background-color: blue;
         }
      </style>
   </head>
   <body>
      <form>
         Subject: <input type = "text" name = "subject"><br>
         Student: <input type = "text" name = "student" required><br>
      </form>
   </body>
</html>

Updated on: 01-Jul-2020

168 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements