Sandeep Sandeep - 4 months ago 67
CSS Question

option:checked is not working with IE 11

I have select tag and need to change background image of option based on selection. I am using style below, This style works with chrome but not working with IE 11. Not sure what changes I need to do to make it work on IE 11 ?

option:checked
{
background-image: url("SelectBlue.png");
color: white;
}


Full HTML Code below

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>change demo</title>
<style>
div {
color: red;
}
</style>


<style type="text/css">

select::-ms-expand
{
display:none;
}
.ListBox{
background-color: transparent;
font-family: verdana;
font-size: 8pt;
font-weight: bold;
color: black;
vertical-align:middle;
height:400px;
width:300px;
}

option:checked {
background-image: url("DataGridSelectBlue.png");
color: white;
}

</style>

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<input id="hid" type="hidden">
<select id="sweets" name="sweets" multiple="multiple" class="ListBox">
<option value="a">Chocolate</option>
<option value="b" selected="selected">Candy</option>
<option value="c">Taffy</option>
<option value="d" selected="selected">Caramel</option>
<option value="e">Fudge</option>
<option value="f"> Cookie</option>
</select>

<select name="food" multiple="multiple" class="ListBox">
<option>Idly</option>
<option selected="selected">Sambar</option>
<option>Dosa</option>
<option selected="selected">Bisibele bhath</option>
<option>chutney</option>
<option>upma</option>
</select>

<div></div>

</body>
</html>

Answer

After a deeper research

According to MDN the :checked CSS pseudo-class selector (basic feature) is compatible to IE11:

  • Chrome 1.0
  • Firefox (Gecko) 1.0
  • Internet Explorer 9.0
  • Opera 9.0
  • Safari 3.1

Source: https://developer.mozilla.org/en-US/docs/Web/CSS/:checked

If so, the reason it might not work is that the property 'background-image' isn't fully browser-compatible.

I'm using chrome and have created the following codepen (based on your code) - you can test it on several browsers to see which property is being supported:

            option:checked {    
                background-color: green; /*Won't apply*/
                color: green; /*Won't apply*/
                border:1px solid green; /*Will apply (at least on chrome)*/
                background-image:url('http://www.bostonscientific.com/content/dam/bostonscientific/utility-icons/icon_Phone.png'); /*Will apply (at least on chrome)*/
            } 

http://codepen.io/anon/pen/wWAmjK