是否有CSS唯一的方式来设置<select>下拉列表的样式?

我需要尽可能人性化地设置<select>表单的样式,而不需要任何JavaScript。在CSS中,我可以使用哪些财产来执行此操作?

此代码需要与所有主要浏览器兼容:

Internet Explorer 6、7和8Firefox浏览器游猎

我知道我可以用JavaScript实现:示例。

我说的不是简单的造型。我想知道,我们只能用CSS做什么。

我在Stack Overflow上发现了类似的问题。

还有Doctype.com上的这个。


当前回答

纯交叉浏览器CSS选择控件样式

规则#1:切勿使用JAVASCRIPT来设置HTML元素的样式!

如果JS被禁用或阻止,CSS将崩溃并烧录。始终从纯CSS开始,然后在您喜欢的任何额外“脚本胶”上分层。记住<select>控件(如HTML中的所有表单字段)被替换为元素,这意味着设备上的浏览器和操作系统对控件的设计和布局具有一定的控制权。这就是为什么Windows、苹果和移动设备控件看起来都不同!这是有原因的。

不要用JavaScript或CSS黑客劫持控件设计,除非您觉得UI由于某种原因(如删除选择控件上的箭头)必须进行修改。下面的CSS不这样做。它接受了它可以在选择表单控件中重新设计的核心限制。我仍然会从我干净的CSS解决方案开始,然后在顶部覆盖任何自定义“黑客”。如果它们失败了,那么您的CSS将优雅地退回到一个始终适用于20多年浏览器的全局CSS设计。。。加上未来的!

下面的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;
}

其他回答

在设置选择下拉列表样式时,我注意到的最大的不一致是Safari和Google Chrome渲染(Firefox可以通过CSS完全自定义)。在互联网的黑暗深处进行了一些搜索后,我发现了以下内容,这几乎完全解决了我对WebKit的疑虑:

Safari和Google Chrome修复程序:

select {
  -webkit-appearance: none;
}

但是,这会删除下拉箭头。您可以使用带有背景、负边距或绝对位于select下拉列表上方的附近div添加下拉箭头。

*CSS属性:-webkit外观中提供了更多信息和其他变量。

使用clip属性裁剪选择元素的边框和箭头,然后将自己的替换样式添加到包装中:

<!DOCTYPE html><html><head><style>选择{位置:绝对;剪辑:矩形(2px 49px 19px 2px);z索引:2;}body>span{display:块;位置:相对;宽度:64px;高度:21px;边框:2px纯绿色;背景:url(http://www.stackoverflow.com/favicon.ico)右1px不重复;}</style></head><span><选择><option value=“”>Alpha</option><option value=“”>Beta版</option><option value=“”>Charlie</option></选择></span></html>

使用不透明度为零的第二次选择使按钮可单击:

<!DOCTYPE html><html><head><style>#实{位置:绝对;剪辑:矩形(2px 51px 19px 2px);z索引:2;}#假{位置:绝对;不透明度:0;}body>span{display:块;位置:相对;宽度:64px;高度:21px;背景:url(http://www.stackoverflow.com/favicon.ico)右1px不重复;}</style></head><span><select id=“real”><option value=“”>Alpha</option><option value=“”>Beta版</option><option value=“”>Charlie</option></选择><select id=“fake”><option value=“”>Alpha</option><option value=“”>Beta版</option><option value=“”>Charlie</option></选择></span></html>

Webkit和其他浏览器的坐标不同,但@media查询可以涵盖这一点。

工具书类

Dojo FX测试:dojox.FX.ext-Dojo.complexCSS掩码:使用rect函数测试剪辑属性,并自动将值剪辑到边框框

以下是三种解决方案:

解决方案#1-外观:无-使用Internet Explorer 10-11解决方案(演示)

--

要隐藏默认箭头集外观,请在select元素上选择none,然后添加带有背景图像的自定义箭头

select {
   -webkit-appearance: none;
   -moz-appearance: none;
   appearance: none;       /* Remove default arrow */
   background-image: url(...);   /* Add custom arrow */
}

浏览器支持:

外观:除了Internet Explorer,没有一个浏览器支持非常好(caniuse)。

我们可以通过添加

select::-ms-expand {
    display: none; /* Hide the default arrow in Internet Explorer 10 and Internet Explorer 11 */
}

如果Internet Explorer 9是一个问题,我们没有办法删除默认箭头(这意味着我们现在有两个箭头),但是,我们可以使用一个时髦的Internet Explorer 9选择器。

至少撤销我们的自定义箭头-保留默认的选择箭头不变。

/* Target Internet Explorer 9 to undo the custom arrow */
@media screen and (min-width:0\0) {
    select {
        background-image:none\9;
        padding: 5px\9;
    }
}

所有这些:

选择{边距:50px;宽度:150px;填充:5px 35px 5px 5px;字体大小:16px;边框:1px实心#CCC;高度:34px;-webkit外观:无;-moz外观:无;外观:无;背景:url(https://stackoverflow.com/favicon.ico)96%/15%无重复#EEE;}/*警告:Internet Explorer黑客攻击*/select::-ms扩展{显示:无;/*删除Internet Explorer 10和11中的默认箭头*/}/*以Internet Explorer 9为目标撤消自定义箭头*/@媒体屏幕和(最小宽度:0\0){选择{背景:无\9;填充:5px \9;}}<选择><option>苹果</option><option selected>菠萝</option><option>楔板</option><option>煎饼</option></选择>

这个解决方案很简单,并且具有良好的浏览器支持——通常应该足够了。


如果需要Internet Explorer的浏览器支持,请提前阅读。

解决方案#2截断select元素以隐藏默认箭头(演示)

--

(在此处阅读更多信息)

用固定宽度和溢出:hidden将select元素包装在div中。

然后给select元素的宽度比div。

结果是select元素的默认下拉箭头将被隐藏(由于溢出:隐藏在容器上),您可以在div的右侧放置任何需要的背景图像。

这种方法的优点是它是跨浏览器的(Internet Explorer 8和更高版本、WebKit和Gecko)。然而,这种方法的缺点是选项下拉列表在右侧突出(我们隐藏了20个像素……因为选项元素占用了选择元素的宽度)。

[然而,需要注意的是,如果自定义选择元素仅对移动设备是必要的,那么上述问题就不适用了,因为每个手机都是以本地方式打开选择元素的。因此,对于移动设备来说,这可能是最佳解决方案。]

.styled选择{背景:透明;宽度:150px;字体大小:16px;边框:1px实心#CCC;高度:34px;}.样式{边距:50px;宽度:120px;高度:34px;边框:1px实心#111;边界半径:3px;溢出:隐藏;背景:url(https://stackoverflow.com/favicon.ico)96%/20%无重复#EEE;}<div class=“styled”><选择><option>菠萝</option><option selected>苹果</option><option>楔板</option><option>煎饼</option></选择></div>


If the custom arrow is necessary on Firefox - prior to Version 35 - but you don't need to support old versions of Internet Explorer - then keep reading...

Solution #3 - Use the pointer-events property (demo)

--

(Read more here)

The idea here is to overlay an element over the native drop down arrow (to create our custom one) and then disallow pointer events on it.

Advantage: It works well in WebKit and Gecko. It looks good too (no jutting out option elements).

Disadvantage: Internet Explorer (Internet Explorer 10 and down) doesn't support pointer-events, which means you can't click the custom arrow. Also, another (obvious) disadvantage with this method is that you can't target your new arrow image with a hover effect or hand cursor, because we have just disabled pointer events on them!

However, with this method you can use Modernizer or conditional comments to make Internet Explorer revert to the standard built in arrow.

NB: Being that Internet Explorer 10 doesn't support conditional comments anymore: If you want to use this approach, you should probably use Modernizr. However, it is still possible to exclude the pointer-events CSS from Internet Explorer 10 with a CSS hack described here.

.notIE {
  position: relative;
  display: inline-block;
}
select {
  display: inline-block;
  height: 30px;
  width: 150px;
  outline: none;
  color: #74646E;
  border: 1px solid #C8BFC4;
  border-radius: 4px;
  box-shadow: inset 1px 1px 2px #DDD8DC;
  background: #FFF;
}
/* Select arrow styling */

.notIE .fancyArrow {
  width: 23px;
  height: 28px;
  position: absolute;
  display: inline-block;
  top: 1px;
  right: 3px;
  background: url(https://stackoverflow.com/favicon.ico) right / 90% no-repeat #FFF;
  pointer-events: none;
}
/*target Internet Explorer 9 and Internet Explorer 10:*/

@media screen and (min-width: 0\0) {
  .notIE .fancyArrow {
    display: none;
  }
}
<!--[if !IE]> -->
<div class="notIE">
  <!-- <![endif]-->
  <span class="fancyArrow"></span>
  <select>
    <option>Apples</option>
    <option selected>Pineapples</option>
    <option>Chocklate</option>
    <option>Pancakes</option>
  </select>
  <!--[if !IE]> -->
</div>
<!-- <![endif]-->

不建议编辑此元素,但如果您想尝试,它与任何其他HTML元素一样。

编辑示例:

/* Edit select */
select {
    /* CSS style here */
}

/* Edit option */
option {
    /* CSS style here */
}

/* Edit selected option */
/* element  attr    attr value */
option[selected="selected"] {
    /* CSS style here */
}
<select>
    <option >Something #1</option>
    <option selected="selected">Something #2</option>
    <option >Something #3</option>
</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;backbackground:透明;字体大小:12px;-webkit外观:无;-moz外观:无(https://alt-fit.com/images/global/select-button.png)100%/15%无重复选择:悬停{background:url(https://alt-fit.com/images/global/select-button.png)100%/15%无重复#fff;}<html><head></head><body><select name=“type”class=“select”><option style=“color:#41533f;”value=“select option”>选择选项</option><option value=“选项1”>选项1</option><option value=“option 2”>选项2</option><option value=“选项3”>选项3</option></选择></body></html>