以下是三种解决方案:
解决方案#1-外观:无-使用Internet Explorer 10-11解决方案(演示)
--
要隐藏默认箭头集外观,请在select元素上选择none,然后添加带有背景图像的自定义箭头
select {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none; /* Remove default arrow */
background-image: url(...); /* Add custom arrow */
}
浏览器支持:
外观:除了Internet Explorer,没有一个浏览器支持非常好(caniuse)。
我们可以通过添加
select::-ms-expand {
display: none; /* Hide the default arrow in Internet Explorer 10 and Internet Explorer 11 */
}
如果Internet Explorer 9是一个问题,我们没有办法删除默认箭头(这意味着我们现在有两个箭头),但是,我们可以使用一个时髦的Internet Explorer 9选择器。
至少撤销我们的自定义箭头-保留默认的选择箭头不变。
/* Target Internet Explorer 9 to undo the custom arrow */
@media screen and (min-width:0\0) {
select {
background-image:none\9;
padding: 5px\9;
}
}
所有这些:
选择{边距:50px;宽度:150px;填充:5px 35px 5px 5px;字体大小:16px;边框:1px实心#CCC;高度:34px;-webkit外观:无;-moz外观:无;外观:无;背景:url(https://stackoverflow.com/favicon.ico)96%/15%无重复#EEE;}/*警告:Internet Explorer黑客攻击*/select::-ms扩展{显示:无;/*删除Internet Explorer 10和11中的默认箭头*/}/*以Internet Explorer 9为目标撤消自定义箭头*/@媒体屏幕和(最小宽度:0\0){选择{背景:无\9;填充:5px \9;}}<选择><option>苹果</option><option selected>菠萝</option><option>楔板</option><option>煎饼</option></选择>
这个解决方案很简单,并且具有良好的浏览器支持——通常应该足够了。
如果需要Internet Explorer的浏览器支持,请提前阅读。
解决方案#2截断select元素以隐藏默认箭头(演示)
--
(在此处阅读更多信息)
用固定宽度和溢出:hidden将select元素包装在div中。
然后给select元素的宽度比div。
结果是select元素的默认下拉箭头将被隐藏(由于溢出:隐藏在容器上),您可以在div的右侧放置任何需要的背景图像。
这种方法的优点是它是跨浏览器的(Internet Explorer 8和更高版本、WebKit和Gecko)。然而,这种方法的缺点是选项下拉列表在右侧突出(我们隐藏了20个像素……因为选项元素占用了选择元素的宽度)。
[然而,需要注意的是,如果自定义选择元素仅对移动设备是必要的,那么上述问题就不适用了,因为每个手机都是以本地方式打开选择元素的。因此,对于移动设备来说,这可能是最佳解决方案。]
.styled选择{背景:透明;宽度:150px;字体大小:16px;边框:1px实心#CCC;高度:34px;}.样式{边距:50px;宽度:120px;高度:34px;边框:1px实心#111;边界半径:3px;溢出:隐藏;背景:url(https://stackoverflow.com/favicon.ico)96%/20%无重复#EEE;}<div class=“styled”><选择><option>菠萝</option><option selected>苹果</option><option>楔板</option><option>煎饼</option></选择></div>
If the custom arrow is necessary on Firefox - prior to Version 35 - but you don't need to support old versions of Internet Explorer - 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: It works well in WebKit and Gecko. It looks good too (no jutting out option
elements).
Disadvantage: Internet Explorer (Internet Explorer 10 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(https://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]-->