如何仅使用 CSS 设置 <select> 下拉菜单的样式?

新手上路,请多包涵

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

我需要尽可能多地设计一个 <select> 形式,而不需要任何 JavaScript。在 CSS 中我可以使用哪些属性来做到这一点?

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

  • Internet Explorer 6、7 和 8
  • 火狐
  • 苹果浏览器

我知道我可以用 JavaScript 做到这一点: Example

我不是在谈论简单的样式。我想知道,我们只能用 CSS 做些什么。

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

这个 在 Doctype.com 上。

原文由 Jitendra Vyas 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 771
2 个回答

以下是三种解决方案:

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

要在选择元素上隐藏默认箭头集 appearance: none ,然后添加您自己的自定义箭头 background-image

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

浏览器支持:

appearance: none 具有很好的浏览器支持 ( caniuse ) - 除了 Internet Explorer。

我们可以改进此技术并添加对 Internet Explorer 10 和 Internet Explorer 11 的支持,方法是添加

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

全部一起:

 select {
  margin: 50px;
  width: 150px;
  padding: 5px 35px 5px 5px;
  font-size: 16px;
  border: 1px solid #CCC;
  height: 34px;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  background: url(https://stackoverflow.com/favicon.ico) 96% / 15% no-repeat #EEE;
}

/* CAUTION: Internet Explorer hackery ahead */

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

/* Target Internet Explorer 9 to undo the custom arrow */
@media screen and (min-width:0\0) {
    select {
        background: none\9;
        padding: 5px\9;
    }
}
 <select>
  <option>Apples</option>
  <option selected>Pineapples</option>
  <option>Chocklate</option>
  <option>Pancakes</option>
</select>

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


如果需要浏览器支持 Internet Explorer,请继续阅读。

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

(在这里阅读更多)

select 元素包装在具有 固定宽度overflow:hidden 的 div 中。

然后给 select 元素一个 比 div 大大约 20 像素 的宽度。

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

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

在此处输入图像描述

[然而,应该注意的是,如果自定义选择元素仅对 移动 设备是必需的——那么上述问题不适用——因为每部手机本机打开选择元素的方式。所以对于移动端,这可能是最好的解决方案。]

 .styled select {
  background: transparent;
  width: 150px;
  font-size: 16px;
  border: 1px solid #CCC;
  height: 34px;
}
.styled {
  margin: 50px;
  width: 120px;
  height: 34px;
  border: 1px solid #111;
  border-radius: 3px;
  overflow: hidden;
  background: url(https://stackoverflow.com/favicon.ico) 96% / 20% no-repeat #EEE;
}
 <div class="styled">
  <select>
    <option>Pineapples</option>
    <option selected>Apples</option>
    <option>Chocklate</option>
    <option>Pancakes</option>
  </select>
</div>

如果自定义箭头在 Firefox 上是必需的 - 在 版本 35 之前 - 但你不需要支持旧版本的 Internet Explorer - 然后继续阅读……

解决方案 #3 - 使用 pointer-events 属性( 演示

(在这里阅读更多)

这里的想法是在本机下拉箭头上覆盖一个元素(以创建我们的自定义箭头),然后禁止其上的指针事件。

优点: 它在 WebKit 和 Gecko 中运行良好。它看起来也不错(没有突出 option 元素)。

缺点: Internet Explorer(Internet Explorer 10 及以下)不支持 pointer-events ,这意味着您无法单击自定义箭头。此外,此方法的另一个(明显的)缺点是您无法使用悬停效果或手形光标来定位新箭头图像,因为我们刚刚禁用了它们的指针事件!

但是,使用此方法,您可以使用 Modernizer 或条件注释使 Internet Explorer 恢复为标准内置箭头。

注意: 由于 Internet Explorer 10 不再支持 conditional comments :如果你想使用这种方法,你应该使用 Modernizr 。但是,仍然可以使用 此处 描述的 CSS hack 从 Internet Explorer 10 中排除指针事件 CSS。

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

原文由 Danield 发布,翻译遵循 CC BY-SA 4.0 许可协议

这是可能的,但不幸的是,在我们作为开发人员需要的范围内,主要是在基于 WebKit 的浏览器中。以下是通过内置开发人员工具检查器从 Chrome 选项面板收集的 CSS 样式示例,经过改进以匹配大多数现代浏览器当前支持的 CSS 属性:

 select {
  -webkit-appearance: button;
  -moz-appearance: button;
  -webkit-user-select: none;
  -moz-user-select: none;
  -webkit-padding-end: 20px;
  -moz-padding-end: 20px;
  -webkit-padding-start: 2px;
  -moz-padding-start: 2px;
  background-color: #F07575; /* Fallback color if gradients are not supported */
  background-image: url(../images/select-arrow.png), -webkit-linear-gradient(top, #E5E5E5, #F4F4F4); /* For Chrome and Safari */
  background-image: url(../images/select-arrow.png), -moz-linear-gradient(top, #E5E5E5, #F4F4F4); /* For old Firefox (3.6 to 15) */
  background-image: url(../images/select-arrow.png), -ms-linear-gradient(top, #E5E5E5, #F4F4F4); /* For pre-releases of Internet Explorer  10*/
  background-image: url(../images/select-arrow.png), -o-linear-gradient(top, #E5E5E5, #F4F4F4); /* For old Opera (11.1 to 12.0) */
  background-image: url(../images/select-arrow.png), linear-gradient(to bottom, #E5E5E5, #F4F4F4); /* Standard syntax; must be last */
  background-position: center right;
  background-repeat: no-repeat;
  border: 1px solid #AAA;
  border-radius: 2px;
  box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.1);
  color: #555;
  font-size: inherit;
  margin: 0;
  overflow: hidden;
  padding-top: 2px;
  padding-bottom: 2px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

当您在基于 WebKit 的浏览器中的任何页面上运行此代码时,它应该更改选择框的外观,删除标准操作系统箭头并添加一个 PNG 箭头,在标签前后放置一些间距,几乎任何您想要的东西。

最重要的部分是 appearance 属性,它改变了元素的行为方式。

它在几乎所有基于 WebKit 的浏览器中都能完美运行,包括移动浏览器,尽管 Gecko 似乎不支持 appearance 以及 WebKit。

原文由 Matthew Morek 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏