Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to style selected option color separately from disabled option

I have a HTML dropdown select menu with first option is disabled. I would like to show that default disabled option in gray color, but once I selected another value, I would like this appear as blue. How can I achieve this ?

enter image description here

I managed to get the selected option appear as gray, and the selectable options to appear as blue in the list. But both disabled and not disables options will appear as gray anyway once they are selected :

select:not(:checked) {
  color: gray;
}
select option:not(:disabled){
  color: #000098;
}
like image 843
Neoweiter Avatar asked Mar 03 '14 13:03

Neoweiter


People also ask

How do I change the color of selected options in CSS?

The “:checked” selector of pseudo-class is used to change the selected option color. The :checked is utilized along with the “option” of the drop-down menu, and after that, you can set the color of the selected option.

How do I change the color of my options?

Change Select List Option background colour on hover. Changing <select> highlight color. Change color of selection.


1 Answers

You can do that if you put required tag to select element.

HTML:

<select name="number" required>
  <option value="" disabled selected hidden>Placeholder</option>
  <option value="1">1</option>
  <option value="2">2</option>
</select>

CSS:

select:required:invalid {
  color: gray;
}
select, option {
  color: blue;
}

https://jsfiddle.net/p63eg7d8/

like image 133
thepi Avatar answered Dec 10 '22 09:12

thepi