2

I am using select2 to populate an HTML select, I want select2 options to have same background color as html select control options, is there any option in select2JS that allow us to do it or I have to do it manually?

Here is the sample code

$('.selectcls').select2({
  minimumResultsForSearch: -1,
  placeholder: "Nothing selected",
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.js"></script>

<div>
  <select id="select1" class="selectcls" style="width:100px" name="select1this" class="wrap">
        <option></option>
        <option style="background-color:green" value="A21">A21</option>
        <option style="background-color:blue" selected value="A22">A22</option>
        <option style="background-color:orange" value="A23">A23</option>
      </select>

</div>

or

jsfiddle

Jithin Raj P R
  • 6,667
  • 8
  • 38
  • 69
AddyProg
  • 2,960
  • 13
  • 59
  • 110

4 Answers4

2

You can specify whatever styles you want in your select2.css for Eg. I've overridden highlighted color from blue to yellowgreen

.select2-container--default .select2-results__option--highlighted[aria-selected] {
    background-color: yellowgreen;
    color: white;
}
Hiral
  • 119
  • 1
  • 8
1

You can use in this way,

.select2-results ul li {
    background-color: red;
}

If you want to try different color in different options. Then use CSS3 child property like li:nth-child(<child number>).