How to add images in select list?

Mayur picture Mayur · Jun 3, 2010 · Viewed 604.4k times · Source

I have a select list of genders.

Code:

<select>
<option>male</option>
<option>female</option>
<option>others</option>
</select>  

I want to use an image in drop down list as drop-down-icon.jpeg.

I want to add a button in place of drop down icon.

How to do that?

Answer

vartec picture vartec · Jun 3, 2010

In Firefox you can just add background image to option:

<select>
  <option style="background-image:url(male.png);">male</option>
  <option style="background-image:url(female.png);">female</option>
  <option style="background-image:url(others.png);">others</option>
</select> 

Better yet, you can separate HTML and CSS like that

HTML

<select id="gender">
  <option>male</option>
  <option>female</option>
  <option>others</option>
</select>  

CSS

select#gender option[value="male"]   { background-image:url(male.png);   }
select#gender option[value="female"] { background-image:url(female.png); }
select#gender option[value="others"] { background-image:url(others.png); }

In other browsers the only way of doing that would be using some JS widget library, like for example jQuery UI, e.g. using Selectable.

From jQuery UI 1.11, Selectmenu widget is available, which is very close to what you want.