1

I have a dropdown select element with car names and their available colors inside parentheses. I would like to be able to do something like this:

<select class="cars">
   <option>Rolls Royce Phantom <span class="car-color">(silver)</span></option>
   <option>Ferrari 488GTB <span class="car-color">(yellow)</span></option>
   <option>Chevrolet Camaro <span class="car-color">(black)</span></option>
</select>

where

.cars {
   color: black;
   font-weight: bold;
}
.car-color {
   color: grey;
   font-weight: normal;
}

This code above is not working, unfortunately. How do I reach a desired effect?

aynber
  • 22,380
  • 8
  • 50
  • 63
Ota Zich
  • 45
  • 1
  • 8
  • Possible duplicate of [How to style the option of a html "select"?](https://stackoverflow.com/questions/7208786/how-to-style-the-option-of-a-html-select) – julianstark999 Dec 26 '17 at 10:51

3 Answers3

2

You can't style option elements because it's rendered by the OS, not HTML. That is why it can't be styled via CSS.

Either you could use some plugins to reach what you are trying to do,

W3schools has a simple way to style the drop down, check it here.

Mhd Alaa Alhaj
  • 2,438
  • 1
  • 11
  • 18
0

You can use something like this

.carcolor{
    color: grey;
}

$('<option>').val('india').text('india').attr('class','carcolor')
.appendTo('#groupid');
redhatvicky
  • 1,912
  • 9
  • 8
0

Try like below css , First example is for all option and second is for specific nth child, if you have multiple styles

.cars1,.cars2 {
   color: green;
   font-weight: bold;
}
.cars1 option{
   color: red;
   font-weight: normal;
   
}
.cars2 option:nth-child(2) {
   color: red;
   font-weight: normal;
}
<select class="cars1">
   <option>Rolls Royce Phantom <span class="car-color">(silver)</span></option>
   <option>Ferrari 488GTB <span class="car-color">(yellow)</span></option>
   <option>Chevrolet Camaro <span class="car-color">(black)</span></option>
</select>
<br/><br/><br/>
<div></div>

<select class="cars2">
   <option>Rolls Royce Phantom <span class="car-color">(silver)</span></option>
   <option>Ferrari 488GTB <span class="car-color">(yellow)</span></option>
   <option>Chevrolet Camaro <span class="car-color">(black)</span></option>
</select>
Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109