从 Internet Explorer 10 开始,您可以使用 ::-ms-expand
伪元素选择器对下拉箭头元素进行样式和隐藏。
select::-ms-expand {
display:none;
/* or visibility: hidden; to keep it's space/hitbox */
}
您应该像在使用CSS样式设置select、optgroup和options中所示的那样进行操作。在很多情况下,background-color和color只是您通常需要为选项设置样式的内容,而不是整个select。
您还可以为下拉菜单添加悬停样式。
select {position:relative; float:left; width:21.4%; height:34px; background:#f9f9e0; border:1px solid #41533f; padding:0px 10px 0px 10px; color:#41533f; margin:-10px 0px 0px 20px; background: transparent; font-size: 12px; -webkit-appearance: none; -moz-appearance: none; appearance: none; background: url(https://alt-fit.com/images/global/select-button.png) 100% / 15% no-repeat #f9f9e0;}
select:hover {background: url(https://alt-fit.com/images/global/select-button.png) 100% / 15% no-repeat #fff;}
<html>
<head>
</head>
<body>
<select name="type" class="select"><option style="color:#41533f;" value="Select option">Select option</option>
<option value="Option 1">Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
</select>
</body>
</html>
仅使用CSS和HTML的解决方案
它似乎与Chrome、Firefox和Internet Explorer 11兼容。但请在其他网页浏览器上留下您的反馈意见。
如Danield的回答所建议的,我将选择框包裹在一个div中(甚至为了跨浏览器兼容性而使用两个div),以获得预期的行为。
HTML:
<div class="sort-options-wrapper">
<div class="sort-options-wrapper-2">
<select class="sort-options">
<option value="choiceOne">choiceOne</option>
<option value="choiceOne">choiceThree</option>
<option value="choiceOne">choiceFour</option>
<option value="choiceFiveLongTestPurpose">choiceFiveLongTestPurpose</option>
</select>
</div>
<div class="search-select-arrow-down"></div>
</div>
请注意两个div包装器。
还要注意添加额外的div,以便在任何位置放置箭头向下按钮(绝对定位),这里我们将其放在左侧。
.sort-options-wrapper {
display: inline-block;
position: relative;
border: 1px solid #83837F;
}
/* This second wrapper is needed for x-browser compatibility */
.sort-options-wrapper-2 {
overflow: hidden;
}
select {
margin-right: -19px; /* That's what is hiding the default-provided browser arrow */
padding-left: 13px;
margin-left: 0;
border: none;
background: none;
/* margin-top & margin-bottom must be set since some
browsers have default values for select elements */
margin-bottom: 1px;
margin-top: 1px;
}
select:focus {
outline: none; /* Removing default browsers outline on focus */
}
.search-select-arrow-down {
position: absolute;
height: 10px;
width: 12px;
background: url(http://i.imgur.com/pHIYN06.png) scroll no-repeat 2px 0px;
left: 1px;
top: 5px;
}
规则#1: 永远不要使用JavaScript来为HTML元素设置样式!
如果禁用或阻止了JS,你的CSS会崩溃。始终从纯CSS开始,然后再添加任何额外的“脚本粘合剂”。记住!像HTML中的所有表单字段一样,<select>
控件是替换元素,这意味着设备上的浏览器和操作系统对控件的设计和布局有一定的控制。这就是为什么Windows、Apple和移动设备的控件看起来都不同!这是有原因的。
不要通过JavaScript或CSS黑客攻击控件设计,除非你觉得必须修改UI(比如删除选择控件上的箭头)。下面的CSS并没有这样做。它接受了在选择表单控件中可以重新设计的核心限制。我仍然会从我的清洁CSS解决方案开始,然后在其上添加任何自定义的“黑客”。“黑客”如果失败,那么你的CSS将优雅地退回到一个全局CSS设计,它在20多年的浏览器中始终有效...以及未来的浏览器!
下面的CSS代码可以跨浏览器(从1990年代到现在)工作,并将所有浏览器,过去和现在,对齐到相同的基本CSS设计。你可以根据需要自定义此代码。你可以在非常旧的浏览器中测试它,它应该给你一个与现代HTML5浏览器匹配的不错的外观和感觉,这应该是目标。
/* This CSS works in 20+ years of browsers without hacks, scripts, or tricks. */
body optgroup,
body optgroup:visited,
body optgroup:hover,
body optgroup:focus,
body optgroup:active {
margin: 0;
padding: 0;
font-style: inherit;
font-weight: bold;
cursor: pointer;
background: #fff;
border: none;
}
body optgroup:visited,
body optgroup:hover,
body optgroup:focus,
body optgroup:active {
background: #f9f9ff;
}
body option,
body option:visited,
body option:hover,
body option:focus,
body option:active {
margin: 0;
padding: 0;
cursor: pointer;
background: #fff;
border: none;
}
body option:visited,
body option:hover,
body option:focus,
body option:active {
background: #f9f9ff;
}
body select,
body select:visited,
body select:hover,
body select:focus,
body select:active {
display: inline-block;
width: auto;
height: auto;
min-width: 0;
max-width: none;
padding: .17em .17em;
margin: 0;
text-transform: none;
border-radius: .2em;
border: 2px solid #bbb;
background: #fff;
cursor: pointer;
-webkit-appearance: listbox;
-moz-appearance: listbox;
line-height: normal;
}
body select:visited,
body select:hover,
body select:focus,
body select:active {
background: #f9f9ff;
border: 2px solid #999;
}
body select:focus {
background: #fff;
border: 2px solid #999;
}
body select:required:visited:valid,
body select:required:valid {
background: #fff;
border: 2px solid #7fbe96;
}
body select:required:hover:valid {
background: #f9f9ff;
border: 2px solid #278b3d;
}
body select:required:focus:valid {
background: #fff;
border: 2px solid #278b3d;
}
body select:required:visited:invalid,
body select:required:invalid {
background: #fff;
border: 2px solid #ff6565;
}
body select:required:hover:invalid {
background: #f9f9ff;
border: 2px solid #ce2f2f;
}
body select:required:focus:invalid {
background: #fff;
border: 2px solid #ce2f2f;
}
body select[disabled],
body select[readonly],
body select[disabled="disabled"],
body select[readonly="readonly"],
body select[disabled]:visited,
body select[readonly]:visited,
body select[disabled="disabled"]:visited,
body select[readonly="readonly"]:visited,
body select[disabled]:hover,
body select[readonly]:hover,
body select[disabled="disabled"]:hover,
body select[readonly="readonly"]:hover,
body select[disabled]:focus,
body select[readonly]:focus,
body select[disabled="disabled"]:focus,
body select[readonly="readonly"]:focus,
body select[disabled]:active,
body select[readonly]:active,
body select[disabled="disabled"]:active,
body select[readonly="readonly"]:active {
border: 2px solid #bbb;
background: #f0f0f0;
color: #999;
cursor: default !important;
}