If you're looking to change the color of options, using color: red;
directly won't work.
The proper way to achieve this is by utilizing the :invalid
option and making sure your <select>
is marked as required.
Check out this code snippet:
select.form-control option{
color: black;
}
select.form-control:invalid {
color: red;
}
<select class="form-control" required>
<option value="" hidden="">Select Month</option>
<option value="JAN">JAN</option>
<option value="FEB">FEB</option>
<option value="MAR">MAR</option>
</select>
Note that by setting it as required, the <select>
becomes mandatory. If this doesn't align with your requirements, consider using a jQuery solution instead:
Solution 2
$(".form-control").change(function(){
var x = $( ".form-control option:selected" ).val();
if(x!="") {
$("select").css("color","black");
} else {
$("select").css("color","red");
}
});
select {
color: red;
}
option {
color: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-control">
<option value="" hidden="">Select Month</option>
<option value="JAN">JAN</option>
<option value="FEB">FEB</option>
<option value="MAR">MAR</option>
</select>
This alternative approach allows you to modify the color without enforcing a required field constraint.