我正在尝试用我自己的照片替换选择的箭头。我包括选择在一个div与相同的大小,我设置的背景选择为透明,我包括一个图片(与箭头大小相同)在div的右上角作为背景。

它只适用于Chrome浏览器。

我如何让它在Firefox和IE9中工作,我得到了这个:

.styled-select { width: 100px; height: 17px; overflow: hidden; overflow: -moz-hidden-unscrollable; background: url(images/downarrow_blue.png) no-repeat right white; border: 2px double red; display: inline-block; position: relative; } .styled-select select { background: transparent; -webkit-appearance: none; width: 100px; font-size: 11px; border: 0; height: 17px; position: absolute; left: 0; top: 0; } body { background-color: #333333; color: #FFFFFF; } .block label { color: white; } <HTML> <HEAD> </HEAD> <BODY> <p/> <form action="/prepareUpdateCategoryList.do?forwardto=search"> <fieldset class="block" id="searchBlock"> <p> <label style="width:80px">Class</label> <div class="styled-select"> <select property="voucherCategoryClass"> <option value="0">Select </option> <option value="7382">steam </option> </select> </div> </p> </fieldset> </form> </BODY> </HTML>


当前回答

好吧,这里似乎有很多答案。有些可能比其他的更好。如果有人绝望到想往下翻,这是我的意见:

如果你真的想要样式你的下拉菜单,不要使用<select>。在Div中使用常规的<ul>将会更加灵活。然而,如果你真的坚持使用select,因为你只是想要最小的下拉列表,这是我的:

select { appearance: none; background: white; background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16px' height='16px' viewBox='0 0 24 24' fill='none'%3E%3Cpath d='M6.1018 8C5.02785 8 4.45387 9.2649 5.16108 10.0731L10.6829 16.3838C11.3801 17.1806 12.6197 17.1806 13.3169 16.3838L18.8388 10.0731C19.5459 9.2649 18.972 8 17.898 8H6.1018Z' fill='%23212121'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 0.5rem center; border-radius: 2px; padding: 0.25rem 2rem 0.25rem 0.75rem; } <select> <option value="bottom-right">Bottom right</option> <option value="bottom-left">Bottom left</option> <option value="top-right">Top right</option> <option value="top-left">Top left</option> </select>

关键部分是找到一个插入符号svg,你可以把它URL转换成字符串。我可以稍微加强它,也许以某种方式旋转插入符号,但真的-我很高兴它不太丑。

其他回答

我想澄清一件之前没人提到过的事。

首先获取svg图像或图标 在那里,你会得到一些xml代码,如这些<svg width="24" height="25" viewBox="0 0 24 25" fill="none" xmlns="http://www.w3.org/2000/svg"> <path d="M4 10.127L12 18.127L20 10.127H4Z" fill="#8E8E93"/> </svg>试着找到它。 然后在这段代码后面加上data:image/svg+xml;utf8, 替换填充颜色fill="#8E8E93"到this fill="%238E8E93"如果你想添加十六进制颜色,你应该将#改为%23

下面是html代码:

<fieldset>
  <label for="editName">Country</label>
  <select class="ra-select">
    <option value="bangladesh" selected>Bangladesh</option>
    <option value="saudi arabia">Saudi Arabia</option>
    <option value="us">Uinited State Of America</option>
    <option value="india">India</option>
  </select>
</fieldset>

下面是css代码:

.ra-select {
   width: 30%;
   padding: 10px;
   /* Replace Default styling (arrow) */
   appearance: none;
   -webkit-appearance: none;
   -moz-appearance: none;
   background-image: url('data:image/svg+xml;utf8,<svg width="24" height="25" viewBox="0 0 24 25" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 10.127L12 18.127L20 10.127H4Z" fill="%238E8E93"/></svg>');
   background-repeat: no-repeat;
   background-position-y: 50%;
   background-position-x: 98%;
}

.ra-select:focus,
.ra-select:hover {
   outline: none;
   border: 1px solid #bbb;
}

.ra-select option {
   background-color: #fff;
}

这里有几个例子

它是基于这个答案,但我添加了一个列表,以使设计更加简洁

select.moreMinimal {

    background-color: inherit;
    display: inline-block;
    font: inherit;
    padding: 0 2.2em 0 1em;    
    margin: 0;      
    cursor: pointer;

    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    -webkit-appearance: none;
    -moz-appearance: none;
  }

select.moreMinimal {
    background-image:
      linear-gradient(45deg, transparent 50%, gray 50%),
      linear-gradient(135deg, gray 50%, transparent 50%);
    background-position:
      calc(100% - 20px), 
      calc(100% - 15px);
    background-size:
      5px 5px,
      5px 5px;
    background-repeat: no-repeat;
}
  
//TODO: Probably shouldn't be focus, cause when you click it again it's still green
select.moreMinimal:focus {
    background-image:
        linear-gradient(45deg, green 50%, transparent 50%),
        linear-gradient(135deg, transparent 50%, green 50%);
    background-position:
        calc(100% - 15px),
        calc(100% - 20px);
    background-size:
        5px 5px,
        5px 5px;
    background-repeat: no-repeat;
    border-color: green;
    outline: 0;
}

它可以在所有浏览器中工作:

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;
    appearance:none;
    -moz-appearance:none; /* Firefox */
    -webkit-appearance:none; /* Safari and Chrome */
    background-position-x: 244px;

}

只使用一个类:

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;
}

http://jsfiddle.net/qhCsJ/4120/

下面是替换箭头的最优雅的方法:

select {
  padding: 15px 5px 15px 15px;
  border: none;
  font-family: Verdana;
  font-weight: bold;
  background-color: DodgerBlue;
  color: white;
  outline: none;
  min-width: 150px;
  cursor: pointer;

  /*Remove decorations to replace arrow with custom arrow */   
  appearance: none;
  -moz-appearance: none;
  -webkit-appearance: none;  
  &::-ms-expand { display: none };  
}
.custom-select:after {
    /* Here we add font taken from libre office to content */
    content: "▼";
    position: absolute;
    top: 4;
    /* Here we need to calculate fonts place so that if select box width changes the custom button font will be always at same place */
    left: calc(100% - 30px);
    font-size: 80%;
    padding: 12px 7px;
    background-color: DodgerBlue;
    color: white;
    pointer-events: none;
}