Style SELECT (Data URI)

Style Select Element in a cross-browser way.

by sstur

HTML

<form>
  <div class="dselect"><select><option>Item One</option><option>Item Two</option></select></div>
  <p>This is another <span class="dselect"><select><option>Item One</option><option>Item Two</option></select></span> select control.</p>
</form>

CSS

body {
  font-family: Arial, sans-serif;
  font-size: 12px;
}
.dselect {
  position: relative;
  display: inline-block;
  border: 1px solid #ccc;
  border-radius: 4px;
  height: 26px;
  line-height: 26px;
}
.dselect select {
  margin: 0;
  padding: 3px;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  width: inherit;
  height: 100%;
  background: transparent none;
  border: 1px solid transparent;
}
@media screen and (min-width:0) {
  .dselect select {
    padding: 0;
    border-width: 1px 34px 1px 6px;
    position: relative;
    z-index: 4;
    cursor: pointer;
    opacity: 0;
  }
  .dselect:before {
    content: attr(title);
    display: block;
    position: absolute;
    left: 0;
    top: 0;
    right: 28px;
    height: 26px;
    line-height: 26px;
    padding-left: 6px;
    z-index: 2;
    white-space: nowrap;
    overflow: hidden;
  }
  .dselect:after {
    content: " ";
    display: block;
    position: absolute;
    right: 0;
    top: 0;
    width: 28px;
    height: 100%;
    background: transparent right center no-repeat;
    background-image: url("data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAaCAYAAACkVDyJAAAAlElEQVRIx+2VwQ3AIAhFwTiFi7KC7uYGHo1z0Et7MWkjaEiaQOJBDzy/wgdqrXwHWKyAiGAZDnSgHBhC+IfCUgqbKXxgGqhY4QyRQkUK35KLoK01iZd+xZKXRuEfbvdQPPGkAABEhMeLhohE5yfaAufk93791r13zQDmnLNqcOMYg1NKmoJgTRHtmDeaWZvPQweaAi8PXSmZJU3QRAAAAABJRU5ErkJggg==");
    z-index: 3;
  }
}
@-moz-document url-prefix() {
  /* for Firefox */
  .dselect select {
    display: block;
  }
}
@media screen and (-webkit-min-device-pixel-ratio:0) {
  /* for Webkit */
  .dselect select {
    -webkit-appearance: none;
    opacity: 1;
  }
  .dselect:before {
    display: none;
  }
}

JavaScript

jQuery(function($) {
  var update = function() {
    $(this).parent().attr('title', $(this).val());
  };
  $(document).on('click change update', '.dselect>select', update);
  $('.dselect>select').trigger('update');
});