Select dropdown color changing [duplicate]


Select dropdown color changing [duplicate]



This question already has an answer here:



I have created a sample dropdown list (the code is below). I want to change the font color of the "Pick a Country" which is displaying in the select option: The default text color is black and I want to change it to red.


<select id="select-id">
<option value="" selected="">Pick a Country</option>
<option value="">India</option>
<option value="">Sri Lanka</option>
<option value="">Sweden</option>





This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





You need to integrate JavaScript/Jquery script for this task. I think you want to show red color only for selected country?
– sohan verma
Jul 2 at 9:51






@ ZenOut ,I want to change the color of the text pick a country on which we use to click to get dropdown options, Hope you understood my concern
– Keerthi Kamarthi
Jul 2 at 9:55






You can not style options cross browser.
– Turnip
Jul 2 at 9:59


option




3 Answers
3



I believe below solution gets what you want. Just few lines of CSS (explanation below snippet)



Styling <select> and <option> elements is not supported across all browsers, because they are rendered by OS, not browser. There are external libraries that create select-like elements composed from HTML elements that can be styled. Below solution is not 100% safe.


<select>


<option>




#select-id {
color: red;
}

#select-id option:not(:checked) {
color: initial;
}


<select id="select-id">
<option value="" selected="">Pick a Country</option>
<option value="">India</option>
<option value="">Sri Lanka</option>
<option value="">Sweden</option>
</select>


#select-id {
color: red;
}



Makes select and all options have color: red.


color: red


#select-id option:not(:checked) {
color: initial;
}



Makes not-selected options have initial color, which is black.





Andrzej Ziółek, thank you for your answer, but i want to change the color of the text pick a country on which we use to click to get dropdown options, Hope you understood my concern
– Keerthi Kamarthi
Jul 2 at 9:55






I updated my snippet. Does it solve your problem now?
– Andrzej Ziółek
Jul 2 at 9:57





Have you tested this? Try it on a Mac. It will not work in either Chrome or Safari.
– Turnip
Jul 2 at 10:08





I have. Working on Chrome right now.
– Andrzej Ziółek
Jul 2 at 10:09





On a Mac? I don't believe you. You can not style options cross browser.
– Turnip
Jul 2 at 10:10


option



You can either style the first option or you can apply a class to it.



With first selector:


#select-id option:first-child {
color: red;
}



With class selector:


#select-id .placeholder {
color: red;
}





hello furkan, its not working
– Keerthi Kamarthi
Jul 2 at 10:01



You can try this:


select option:first-child{
color: red;
}

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages