5

In this example : https://jsfiddle.net/pfc1qauz/10/ when I select 2 and changing focus to C (anything on the right part), the background color of 2 has become grey. How to keep it red when it is not focus ?

HTML:

<select class="multiselect-left" size=4>
  <option>1</option>
  <option>2</option>
  <option>3</option>
</select>
<select class="multiselect-right" size=4>
  <option>A</option>
  <option>B</option>
  <option>C</option>
</select>

CSS:

option{
  background: #F00;
}

Thanks,

Benjamin
  • 3,350
  • 4
  • 24
  • 49

3 Answers3

3

This is likely the result of a browser-specific style. As you can see if you use the Developer Tools (F12) within Google Chrome :

select:-internal-list-box option:checked {
    background-color: -internal-inactive-list-box-selection;
    color: -internal-inactive-list-box-selection-text;
}

Even if you were to add this same style using your own values, it still would not override it properly :

select:-internal-list-box option:checked {
    background-color: red!important;
    color: #FFF!important;
}

This is applying the style that you are seeing to the checked options within any <select> elements and it cannot really be directly overridden.

<select> elements are notoriously difficult to style in any kind of cross-browser sense. If it's something that you really need to handle, you would likely need to use a Javascript-based solution like select that would override the default <select> as a facade that uses <div> or other elements for styling purposes.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
1

This can be done with some JavaScript that draws a strip over the select when it loses focus. The strip is positioned over the selected option so that it has the same position and size, also font/padding etc, but it has a different background color. I managed to code a script that does the trick for an intranet site of mine, so it works well with multiple selects with size>1 on one page. When the selected option is not completely visible, the strip is also drawn only partially. This problem obviously does not happen in selects with size=1.

KS74
  • 156
  • 5
0

When you change only background color, it will not change. Try this,

select[multiple]:focus option:checked {
    background: red linear-gradient(0deg, red 0%, red 100%);
}

For more info, please refer this link

Bala555
  • 119
  • 1
  • 6