21

我想从选择框中删除默认箭头并想使用自定义图标。从之前关于 SO 的答案中,我发现这是不可能的(使其适用于主要浏览器)。唯一的可能是将选择包装在 div 内,并将其宽度设置为大于 div 宽度并设置溢出:隐藏。我正在尝试关注,但它不起作用。我做错了什么?

.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/

4

2 回答 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>

于 2018-04-11T08:32:55.803 回答
0

尝试更换

padding: 2px 30px 2px 2px;

padding: 2px 2px 2px 2px;

它应该工作。

于 2012-10-01T06:34:05.387 回答