用纯css改变下拉列表select框的默认样式

      在使用html的select标签的时候,可能我们会觉得原有的样式比较丑,想把它变的美观一点,那么这里有两种方法。

1.方法一:

        原理将浏览器默认的下拉框样式清除,然后应用上自己的,再附一张向右对齐小箭头的图片即可。
        在线示例地址:http://jsbin.com/rejebopahe/edit?html,css,output
        方法一根据此篇文章进行修改: http://uplifted.net/programming/change-default-select-dropdown-style-just-css/

<span style="font-family:KaiTi_GB2312;font-size:14px;">select {
  /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/
  border: solid 1px #000;

  /*很关键:将默认的select选择框样式清除*/
  appearance:none;
  -moz-appearance:none;
  -webkit-appearance:none;

  /*在选择框的最右侧中间显示小箭头图片*/
  background: url("http://ourjs.github.io/static/2015/arrow.png") no-repeat scroll right center transparent;


  /*为下拉小箭头留出一点位置,避免被文字覆盖*/
  padding-right: 14px;
}


/*清除ie的默认选择框样式清除,隐藏下拉箭头*/
select::-ms-expand { display: none; }</span></span>

2.方法二:

     原理:为其添加一个父容器,容器是用来覆盖小箭头的,然后为select添加一个向右的小偏移或者宽度大于父级元素。设置父级的CSS属性为超出部分不可见,即可覆盖即小箭头。然后再为父级容器添加背景图片即可。overflow: hidden并不能隐藏下拉框的显示。

注:旧版IE多用此方法。因为 IE8/9并不支持  appearance:none  CSS属性。

示例代码:

html页:

<span style="font-family:KaiTi_GB2312;font-size:14px;"><div id="parent">
  <select>
      <option>what</option>
      <option>the</option>
      <option>hell</option>
  </select>
</div></span>
css样式:

<span style="font-family:KaiTi_GB2312;font-size:14px;"> #parent {
    background: url('yourimage') no-repeat;
    width: 100px;
    height: 30px;
    overflow: hidden;
}

#parent select {
    background: transparent;
    border: none;
    padding-left: 10px;
    width: 120px;
    height: 100%;
}</span>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章