How to change the background color of selected elements in the multi-select drop-down list?

I want to give yellow color to the selected items in the multi-select drop-down list. By default it has a gray background after selection, I want to do this in HTML, CSS .

Can anyone help with this?

+5
css multi-select html.dropdownlistfor
source share
6 answers

We can use JS to select the DOM.

 $('select').change(function() { $('option').css('background', 'none'); $('option:selected').css('backgroundColor', 'red'); }).change() <select> <option>1111111</option> <option>222222222</option> <option>33333333</option> <option>44444444</option> </select> 

Demo: http://jsfiddle.net/TxbVt/1/

+1
source share

We can just do it with the css below.

select option:checked{ background: #1aab8e -webkit-linear-gradient(bottom, #1aab8e 0%, #1aab8e 100%); }

+3
source share
 <style> .select2-container--default .select2-results__option[aria-selected=true] { background-color: inherit; color: lightgray; } </style> 

Add your own style inside the block.

+2
source share

The following should work (for browsers that allow you to use style parameter tags), however, the default selection style will be overridden in most cases. However, you can find a way to disable it:

CSS

 select option.selected { font-weight: bold; color: red; } 

Javascript

 function highlight_options(field){ var i,c; for(i in field.options){ (c=field.options[i]).className=c.selected?'selected':''; } } 

Markup

 <select onchange="highlight_options(this)" multiple="multiple"> <option>One</option> <option>Two</option> <option>Three</option> </select> 
0
source share

Pure CSS will help here:

 option:checked 
-one
source share

You can not. The <option> element does not support CSS styles.

You can use a JavaScript based alternative. There are many scenarios for replacing <select> .

-3
source share

All Articles