Styling HTML Combo Boxes

We should mention at the very outset that this technique is of limited practical utility since it relies on altering the opacity of the select element - pre IE7 this is not possible.

Luxembourg

The technique works thus

This technique works in Firefox, Opera and IE7 because the dropdown list shown by the select element is not transparent even though the actual element is. The skeleton code required to provide this functionality is shown below.

<html>
   <head>
      <script>
       function ButChange(AButs,AValue)
       {
        var  i,e;
        for (i=0;i < AButs.length;i++)
          {
           e = document.getElementById(AButs[i]);
        e.style.borderStyle = AValue;
       }
      }  
        
       function WhenChosen()
       {
        var e1 = document.getElementById("selcb");
        var e2 = document.getElementById("selection");
        e2.innerHTML = e1.options[e1.selectedIndex].value;
       }
      </script>
      <style>
      .cbCB,.cbD{position:absolute;background-color:#F2BA71;font-size:0.8em;height:1.5emtop:1.5em}
      .cbCB{opacity:0}
      .cbArr{float:right;background-color:#F2C99B;width:1em;height:1.2em;margin:0.1em;border:0.05em gray solid}
      </style>
      <!--[if lte IE 6]>
        <style>.cbD{display:none}</style>
      <![endif]-->
   </head>
   <body>
      <div class="cbD"id="seld">
         <span class="cbArr" id="selarr" >&#x25BC;</span><span  id="selection">Luxembourg </span>
      </div>
      <select id="selcb" onchange="WhenChosen()" onmouseover="ButChange(['selcd','selarr'],'outset')"
        onmouseout="ButChange(['selcd','selarr'],'solid')">
         <optgroup label="Europe">
           <option>United Kingdom</option>
            <option selected="selected">Luxembourg</option>
         </optgroup>    
         <optgroup label="Asia">
           <option>India</option>
           <option>Dubai</option>
        </optgroup>
      </select>
   </body>
<html>
Jump To...

Colophon