CSS选择框箭头样式

21

我想从下拉框中删除默认的箭头,并使用自定义图标。 根据SO上以前的答案,我发现这是不可能的(在主流浏览器中无法实现)。唯一的可能性是将选择框包装在一个div内,将其宽度设置为大于div宽度,并设置overflow:hidden。

我正在尝试以下内容,但它不起作用。我做错了什么?

.selectParent {
  width: 80px;
  overflow: hidden;
}

.selectParent select {
  width: 100px;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  padding: 2px 30px 2px 2px;
  border: none;
  background: transparent url("http://cdn1.iconfinder.com/data/icons/cc_mono_icon_set/blacks/16x16/br_down.png") no-repeat right center;
}
<div class="selectParent">
  <select>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>           
   </select>
</div>

JSFiddle: http://jsfiddle.net/gcPmC/

2个回答

2
请按照以下方式操作:

请按照以下步骤进行:

.selectParent {
 width:120px;
 overflow:hidden;   
}
.selectParent select { 
 display: block;
 width: 100%;
 padding: 2px 25px 2px 2px; 
 border: none; 
 background: url("http://cdn1.iconfinder.com/data/icons/cc_mono_icon_set/blacks/16x16/br_down.png") right center no-repeat; 
 appearance: none; 
 -webkit-appearance: none;
 -moz-appearance: none; 
}
.selectParent.left select {
 direction: rtl;
 padding: 2px 2px 2px 25px;
 background-position: left center;
}
/* for IE and Edge */ 
select::-ms-expand { 
 display: none; 
}
<div class="selectParent">
  <select>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>           
   </select>
</div>
<br />
<div class="selectParent left">
  <select>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>           
   </select>
</div>


0
尝试替换

标签

padding: 2px 30px 2px 2px;

使用

padding: 2px 2px 2px 2px;

应该可以工作。


2
谢谢,但是我的自定义图标仍然没有出现。 - user1251698

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接