以下是三种解决方案:
解决方案 #1 - 外观:无 - 使用 Internet Explorer 10 - 11 解决方法(Demo http://codepen.io/danield770/pen/bgJOyV?editors=1100)
--
隐藏默认箭头集appearance: none
在选择元素上,然后添加您自己的自定义箭头background-image
select {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
/* Remove default arrow */
background-image: url(...);
/* Add custom arrow */
}
浏览器支持:
appearance: none
有很好的浏览器支持(caniuse http://caniuse.com/#feat=css-appearance) - Internet Explorer 除外。
我们可以通过添加以下内容来改进此技术并添加对 Internet Explorer 10 和 Internet Explorer 11 的支持
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;
}
}
全部一起:
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(https://stackoverflow.com/favicon.ico) 96% / 15% no-repeat #EEE;
}
/* CAUTION: Internet Explorer hackery ahead */
select::-ms-expand {
display: none; /* Remove default arrow in Internet Explorer 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>
这个解决方案很简单,并且具有良好的浏览器支持 - 通常应该足够了。
如果需要 Internet Explorer 的浏览器支持,请继续阅读。
解决方案#2 截断选择元素以隐藏默认箭头 (demo http://jsfiddle.net/danield770/YvCHW/10613/)
--
(在这里阅读更多) http://bavotasan.com/2011/style-select-box-using-only-css/
包裹住select
div 中带有 a 的元素固定宽度 and overflow:hidden
.
然后给出select
元素宽度约为比 div 大 20 像素.
结果是默认的下拉箭头select
元素将被隐藏(由于overflow:hidden
容器上),您可以将任何您想要的背景图像放置在 div 的右侧。
The 优势这种方法的特点是它是跨浏览器的(Internet Explorer 8 及更高版本,WebKit http://en.wikipedia.org/wiki/WebKit, and Gecko http://en.wikipedia.org/wiki/Gecko_%28layout_engine%29)。但是,那坏处这种方法的特点是选项下拉菜单在右侧突出(我们隐藏了 20 个像素......因为选项元素采用了选择元素的宽度)。
[但是,应该注意的是,如果自定义选择元素仅对于mobile设备 - 那么上述问题不适用 - 因为每个手机本机打开选择元素的方式。因此对于移动设备来说,这可能是最好的解决方案。]
.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(https://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>
如果 Firefox 上需要自定义箭头 - 之前版本35 https://developer.mozilla.org/en-US/Firefox/Releases/35- 但您不需要支持旧版本的 Internet Explorer - 然后继续阅读...
解决方案 #3 - 使用pointer-events
财产 (demo http://jsfiddle.net/danield770/sNwrs/3158/)
--
(在这里阅读更多) http://lea.verou.me/2011/03/custom-select-drop-downs-with-css3/
这里的想法是将一个元素覆盖在本机下拉箭头上(以创建我们的自定义箭头),然后禁止其上的指针事件。
优势:它在 WebKit 和 Gecko 中运行良好。看起来也不错(没有突出option
元素)。
坏处:Internet Explorer(Internet Explorer 10 及更低版本)不支持pointer-events
,这意味着您无法单击自定义箭头。此外,此方法的另一个(明显)缺点是您无法使用悬停效果或手形光标来定位新的箭头图像,因为我们刚刚禁用了它们上的指针事件!
不过,通过这种方法,您可以使用 Modernizer 或条件注释使 Internet Explorer 恢复为标准内置箭头。
NB:由于 Internet Explorer 10 不支持conditional comments
不再:如果你想使用这种方法,你可能应该使用现代化。但是,仍然可以通过描述的 CSS 黑客从 Internet Explorer 10 中排除指针事件 CSShere https://stackoverflow.com/a/14916454/703717.
.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]-->