2014-03-03 141 views
35

我需要你的帮助。如何更改选择框箭头

我似乎无法将我的头围绕在这一个并找出它。如何使用下面的自定义箭头更改选择框中的默认Windows 7,IE 10默认箭头:enter image description here,使其看起来像这样:enter image description here

这里是我希望用箭头:enter image description here

这里是我的HTML标记:

<!DOCTYPE html> 
<html> 
<head> 
    <style type="text/css"> 
    select { font: normal 13px Arial; color: #000;} 
    .container { 
     border: 1px solid red; 
     position: relative; width: 124px; height: 18px; overflow: hidden; 
    } 
    .inpSelect { 
     color: black; background: #ffa; 
     position: absolute; width: 128px; top: -2px; left: -2px; 
    } 
    </style> 

<script type="text/javascript"> 
</script> 

</head> 

<body> 

<div class="container"> 
    <select class="inpSelect" name="xxx"> 
     <option value="0" selected="selected">actual browser</option> 
     <option value="1">IE</option> 
     <option value="2">Firefox</option> 
     <option value="3">Opera</option> 
     <option value="4">Safari</option> 
    </select> 
</div> 
</body> 

</html> 
+1

固定''width'标签option',在包装元素上使用'background-image',在'select'标签上使用'background-transparent',并且在父元素上使用'overflow:hidden;',在那里你去做... –

+2

为什么你需要这样做?只为所有浏览器的像素完美? – epascarello

回答

11

CSS

select.inpSelect { 
    //Remove original arrows 
    -webkit-appearance: none; 
    //Use png at assets/selectArrow.png for the arrow on the right 
    //Set the background color to a BadAss Green color 
    background: url(assets/selectArrow.png) no-repeat right #BADA55; 
} 
23

只有一个选择工作:

select { 
    width: 268px; 
    padding: 5px; 
    font-size: 16px; 
    line-height: 1; 
    border: 0; 
    border-radius: 5px; 
    height: 34px; 
    background: url(http://cdn1.iconfinder.com/data/icons/cc_mono_icon_set/blacks/16x16/br_down.png) no-repeat right #ddd; 
    -webkit-appearance: none; 
    background-position-x: 244px; 
} 

fiddler

+7

这似乎只适用于Chrome。在Firefox中它根本不起作用,而在IE中它给出了一个混合的结果。 – Taylor

+0

你没有使用类 – quemeful

+1

@泰勒只需添加select :: - ms-expand {display:none; } in IE – crh225

43

可以跳过纯CSS箭头容器或背景图像:一个``fixed` width`包装元素内

select { 

    /* make arrow and background */ 

    background: 
    linear-gradient(45deg, transparent 50%, blue 50%), 
    linear-gradient(135deg, blue 50%, transparent 50%), 
    linear-gradient(to right, skyblue, skyblue); 
    background-position: 
    calc(100% - 21px) calc(1em + 2px), 
    calc(100% - 16px) calc(1em + 2px), 
    100% 0; 
    background-size: 
    5px 5px, 
    5px 5px, 
    2.5em 2.5em; 
    background-repeat: no-repeat; 

    /* styling and reset */ 

    border: thin solid blue; 
    font: 300 1em/100% "Helvetica Neue", Arial, sans-serif; 
    line-height: 1.5em; 
    padding: 0.5em 3.5em 0.5em 1em; 

    /* reset */ 

    border-radius: 0; 
    margin: 0;  
    -webkit-box-sizing: border-box; 
    -moz-box-sizing: border-box; 
    box-sizing: border-box; 
    -webkit-appearance:none; 
    -moz-appearance:none; 
} 

样品here

+5

在IE中不起作用,因为它依赖于-webkit外观和-mox外观。虽然听起来像Edge支持它,“微软Edge和IE Mobile支持这个属性,它的前缀是-webkit-,而不是-ms-,这是为了interop的原因。” - caniuse.com尽管可以通过使用“select :: - ms-expand {display:none;}”来解决这个问题。 –

+0

太好了!我还发现给选择框一个与2.5em背景大小相同的填充权限是有用的;对于带有大量文本的小盒子或选项,文本将落在“按钮”的后面,而不是放在上面。 –

+0

感谢@MarkBoltuc提示。 select :: - ms-expand {display:none; }帮助Edge。 – somecallitblues