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

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

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

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

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

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

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

还有Doctype.com上的这个。


当前回答

select元素及其下拉功能很难设置样式。

Chris Heilmann的select元素的风格属性证实了Ryan Dohery对第一个答案的评论:

“select元素是操作系统,而不是浏览器chrome。因此风格不可靠,尝试也不一定有意义无论如何"

其他回答

不建议编辑此元素,但如果您想尝试,它与任何其他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>

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

Safari和Google Chrome修复程序:

select {
  -webkit-appearance: none;
}

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

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

我有这个确切的问题,除了我不能使用图像,而且不受浏览器支持的限制。这应该是“符合规范”,幸运的话,最终开始在各地工作。

它使用分层旋转的背景层来“剪切”下拉箭头,因为伪元素不适用于select元素。

编辑:在这个更新版本中,我使用了CSS变量和一个小型主题系统。

:根目录{--半径:2px;--baseFg:灰色;--baseBg:白色;--重音Fg:#006fc2;--重音Bg:#bae1ff;}.主题粉色{--半径:2米;--baseFg:#c70062;--baseBg:#ffe3f1;--重音Fg:#c70062;--重音Bg:#ffaad4;}.主题结构{--半径:0;--baseFg:白色;--baseBg:黑色;--accentFg:黑色;--accentBg:橙色;}选择{字体:400 12px/1.3无衬线;-webkit外观:无;外观:无;颜色:var(--baseFg);border:1px实体变量(--baseFg);线高:1;大纲:0;衬垫:0.65em 2.5em 0.55em 0.75em;边界半径:var(--radius);背景色:var(--baseBg);背景图像:线性梯度(var(--baseFg)、var(--baseFg)),线性梯度(-135度,透明50%,var(--accentBg)50%),线性梯度(-225度,透明50%,var(--accentBg)50%),线性梯度(var(--accentBg)42%,var(--accentFg)42%);背景重复:无重复,无重复,不重复,无反复;背景大小:1px 100%,20px 22px,20px 22 px,20px 100%;背景位置:右20px中心,右下,右下和右下;}选择:悬停{背景图像:线性梯度(var(--accentFg),线性梯度(-135度,透明50%,var(--accentFg)50%),线性梯度(-225度,透明50%,var(--accentFg)50%),线性梯度(var(--accentFg)42%,var(--accentBg)42%);}选择:活动{背景图像:线性梯度(var(--accentFg),线性梯度(-135度,透明50%,var(--accentFg)50%),线性梯度(-225度,透明50%,var(--accentFg)50%),线性梯度(var(--accentFg)42%,var(--accentBg)42%);颜色:var(--accentBg);边框颜色:var(--accentFg);背景色:var(--accentFg);}<选择><option>这么多选项</option><选项></选项></选择><select class=“主题粉色”><option>这么多选项</option><选项></选项></选择><select class=“主题构建”><option>这么多选项</option><选项></选项></选择>

这里有一个基于我在这次讨论中最喜欢的想法的解决方案。这允许直接设置<select>元素的样式,而无需任何附加标记。

它可以使用Internet Explorer 10(及更高版本),并为Internet Explorer 8/9提供安全回退。这些浏览器需要注意的一点是,背景图像必须定位并足够小,以便隐藏在本机扩展控件后面。

HTML

<select name='options'>
  <option value='option-1'>Option 1</option>
  <option value='option-2'>Option 2</option>
  <option value='option-3'>Option 3</option>
</select>

SCSS

body {
  padding: 4em 40%;
  text-align: center;
}

select {
  $bg-color: lightcyan;
  $text-color: black;
  appearance: none; // Using -prefix-free http://leaverou.github.io/prefixfree/
  background: {
    color: $bg-color;
    image: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/1255/caret--down-15.png");
    position: right;
    repeat: no-repeat;
  }
  border: {
    color: mix($bg-color, black, 80%);
    radius: .2em;
    style: solid;
    width: 1px;
    right-color: mix($bg-color, black, 60%);
    bottom-color: mix($bg-color, black, 60%);
  }
  color: $text-color;
  padding: .33em .5em;
  width: 100%;
}

// Removes default arrow for Internet Explorer 10 (and later)
// Internet Explorer 8/9 gets the default arrow which covers the caret
// image as long as the caret image is smaller than and positioned
// behind the default arrow
select::-ms-expand {
    display: none;
}

代码笔

http://codepen.io/ralgh/pen/gpgbGx

<select>标签可以通过CSS进行样式设置,就像在浏览器中呈现的HTML页面上的任何其他HTML元素一样。下面是一个(过于简单)的示例,它将在页面上放置一个select元素,并将选项的文本呈现为蓝色。

示例HTML文件(选择Example.HTML):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Select Styling</title>
  <link href="selectExample.css" rel="stylesheet">
</head>
<body>
<select id="styledSelect" class="blueText">
  <option value="apple">Apple</option>
  <option value="orange">Orange</option>
  <option value="cherry">Cherry</option>
</select>
</body>
</html>

示例CSS文件(selectExample.CSS):

/* All select elements on page */
select {
  position: relative;
}

/* Style by class. Effects the text of the contained options. */
.blueText {
  color: #0000FF;
}

/* Style by id. Effects position of the select drop down. */
#styledSelect {
  left: 100px;
}