hurriedly% 2009-12-13 03:33 采纳率: 100%
浏览 403
已采纳

如何使用 CSS 样式来设计一个下拉菜单?

Is there a CSS-only way to style a <select> dropdown?

I need to style a <select> form as much as humanly possible, without any JavaScript. What are the properties I can use to do so in CSS?

This code needs to be compatible with all major browsers:

  • Internet Explorer 6,7 and 8
  • Firefox
  • Safari

I know I can make it with JavaScript: Example.

And I'm not talking about simple styling. I want to know, what the best we can do with CSS only.

I found similar questions on Stack Overflow.

And this one on Doctype.com.

转载于:https://stackoverflow.com/questions/1895476/how-to-style-a-select-dropdown-with-only-css

  • 写回答

24条回答 默认 最新

  • 衫裤跑路 2012-12-20 09:27
    关注

    Here are 3 solutions:

    Solution #1 - appearance: none - with ie10-11 workaround (Demo)

    To hide the default arrow set appearance: none on the select element, then add your own custom arrow with background-image

    select {
       -webkit-appearance: none; 
       -moz-appearance: none;
       appearance: none;       /* remove default arrow */
       background-image: url(...);   /* add custom arrow */
    }
    

    Browser Support:

    appearance: none has very good browser support (caniuse) - except for ie11- and firefox 34-

    We can improve this technique and add support for ie10 and ie11 by adding

    select::-ms-expand { 
        display: none; /* hide the default arrow in ie10 and ie11 */
    }
    

    If ie9 is a concern - we have no way of removing the default arrow (which would mean that we would now have two arrows), but, we could use a funky ie9 selector to at least undo our custom arrow - leaving the default select arrow intact.

    /* target Internet Explorer 9 to undo the custom arrow */
    @media screen and (min-width:0\0) {
        select {
            background-image:none\9;
            padding: 5px\9;
        } 
    }
    

    All together:

    select {
      margin: 50px;
      width: 150px;
      padding: 5px 35px 5px 5px;
      font-size: 16px;
      border: 1px solid #ccc;
      height: 34px;
      -webkit-appearance: none;
      -moz-appearance: none;
      appearance: none;
      background: url(http://www.stackoverflow.com/favicon.ico) 96% / 15% no-repeat #eee;
    }
    
    
    /* CAUTION: IE hackery ahead */
    
    
    select::-ms-expand { 
        display: none; /* remove default arrow in IE 10 and 11 */
    }
    
    /* target Internet Explorer 9 to undo the custom arrow */
    @media screen and (min-width:0\0) {
        select {
            background:none\9;
            padding: 5px\9;
        }
    }
    <select>
      <option>Apples</option>
      <option selected>Pineapples</option>
      <option>Chocklate</option>
      <option>Pancakes</option>
    </select>

    This solution is easy and has good browser support - it should generally suffice.


    If browser support for ie9- and firefox 34- is necessary then keep reading...

    Solution #2 Truncate the select element to hide the default arrow (Demo)

    (Read more here)

    Wrap the select element in a div with a fixed width and overflow:hidden.

    Then give the select element a width of about 20 pixels greater than the div.

    The result is that the default drop-down arrow of the select element will be hidden (due to the overflow:hidden on the container), and you can place any background image you want on the right-hand-side of the div.

    The advantage of this approach is that it is cross-browser (Internet Explorer 8 and later, WebKit, and Gecko). However, the disadvantage of this approach is that the options drop-down juts out on the right-hand-side (by the 20 pixels which we hid... because the option elements take the width of the select element).

    enter image description here

    [It should be noted, however, that if the custom select element is necessary only for MOBILE devices - then the above problem doesn't apply - because of the way each phone natively opens the select element. So for mobile, this may be the best solution.]

    .styled select {
      background: transparent;
      width: 150px;
      font-size: 16px;
      border: 1px solid #ccc;
      height: 34px;
    }
    .styled {
      margin: 50px;
      width: 120px;
      height: 34px;
      border: 1px solid #111;
      border-radius: 3px;
      overflow: hidden;
      background: url(http://www.stackoverflow.com/favicon.ico) 96% / 20% no-repeat #eee;
    }
    <div class="styled">
      <select>
        <option>Pineapples</option>
        <option selected>Apples</option>
        <option>Chocklate</option>
        <option>Pancakes</option>
      </select>
    </div>

    If the custom arrow is necessary on Firefox - prior to Version 35 - but you don't need to support old versions of IE - then keep reading...

    Solution #3 - Use the pointer-events property (Demo)

    (Read more here)

    The idea here is to overlay an element over the native drop down arrow (to create our custom one) and then disallow pointer events on it.

    Advantage: Works well in WebKit and Gecko. It looks good too (no jutting out option elements)

    Disadvantage: Internet Explorer (IE10 and down) doesn't support pointer-events, which means you can't click the custom arrow. Also, another (obvious) disadvantage with this method is that you can't target your new arrow image with a hover effect or hand cursor, because we have just disabled pointer events on them!

    However, with this method you can use Modernizer or conditional comments to make Internet Explorer revert to the standard built in arrow.

    NB: Being that Internet Explorer 10 doesn't support conditional comments anymore: If you want to use this approach, you should probably use Modernizr. However, it is still possible to exclude the pointer-events CSS from Internet Explorer 10 with a CSS hack described here.

    .notIE {
      position: relative;
      display: inline-block;
    }
    select {
      display: inline-block;
      height: 30px;
      width: 150px;
      outline: none;
      color: #74646e;
      border: 1px solid #C8BFC4;
      border-radius: 4px;
      box-shadow: inset 1px 1px 2px #ddd8dc;
      background: #fff;
    }
    /* Select arrow styling */
    
    .notIE .fancyArrow {
      width: 23px;
      height: 28px;
      position: absolute;
      display: inline-block;
      top: 1px;
      right: 3px;
      background: url(http://www.stackoverflow.com/favicon.ico) right / 90% no-repeat #fff;
      pointer-events: none;
    }
    /*target Internet Explorer 9 and Internet Explorer 10:*/
    
    @media screen and (min-width: 0\0) {
      .notIE .fancyArrow {
        display: none;
      }
    }
    <!--[if !IE]> -->
    <div class="notIE">
      <!-- <![endif]-->
      <span class="fancyArrow"></span>
      <select>
        <option>Apples</option>
        <option selected>Pineapples</option>
        <option>Chocklate</option>
        <option>Pancakes</option>
      </select>
      <!--[if !IE]> -->
    </div>
    <!-- <![endif]-->

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(23条)

报告相同问题?

悬赏问题

  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止