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

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

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

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

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

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

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

还有Doctype.com上的这个。


当前回答

自定义选择CSS样式

在Internet Explorer(10和11)、Edge、Firefox和Chrome中测试

select::-ms扩展{显示:无;}选择{显示:内联块;框大小调整:边框框;衬垫:0.5em 2em 0.5em 0.5em;边框:1px实心#eee;字体:inherit;线条高度:inherit;-webkit外观:无;-moz外观:无;-ms外观:无;外观:无;背景重复:无重复;背景图像:线性梯度(45deg,透明50%,currentColor 50%),线性梯度(135deg,当前Color 50%,透明50%);背景位置:右15px顶部1em,右10px顶部1nm;背景尺寸:5px 5px,5px 5px;}<select name=“”><option value=“”>Lorem</option><option value=“”>Lorem Ipsum</option></选择><select name=“”已禁用><option value=“”>禁用</option></选择><select name=“”style=“color:red;”><option value=“”>颜色</选项><option value=“”>Lorem Ipsum</option></选择>

其他回答

这是可能的,但不幸的是,根据开发人员的要求,大多数情况下都是基于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的浏览器中的任何页面上运行此代码时,它应该更改选择框的外观,删除标准OS箭头并添加PNG箭头,在标签前后放置一些间距,几乎是您想要的任何内容。

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

它在几乎所有基于WebKit的浏览器中都能完美工作,包括移动浏览器,尽管Gecko似乎不像WebKit那样支持外观。

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

我用Bootstrap找到了你的案子。这是最简单的解决方案:

选择表单控件{-moz外观:无;-webkit外观:无;外观:无;背景位置:右中心;背景重复:无重复;背景尺寸:1ex;背景原点:内容框;背景图像:url(“数据:image/svg+xml;base64,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Q9InBhdGgzMzQ1IggICAgICBzdHls ZT0iZmlsbDojMzMzMz MzO2ZpbGwtb3BhY2l0eToxIggICAgICBkPSJtIDizNy41NjY5Niw0MTMuMjU1MDcgYyAwLjU1ODA0LC0wLjU1OD A0IDAuNTU4MDQsLTEuNDczMjIgMCwtMi4wMzEynSBsIC0zLjcwNTM1LC0zLjY4MzA0IGMgLTauNTU4MDQsLTauNTU4MDQgLTEuNDuwOSwtMC41NTgwNCAtMi4wMDg5MywwIEwgMjIwLDQxOS4zOTM0NiAyMDguMTQ3MzIsNDA3LjU0MDc4IGMgLTAUNTU4MDDMsLTauntU4MDDQgLTeuNDuwodksLTauNTu4MDQgLTEuMDA4OTMsMCBsiC0zLjcw NTM1LDMuNjgzMDQgYyAtMC41NTgwNCwwLjU1ODD AzIC0wLjU1ODA0LDEuNDczMjEgMCwyLjAzMTI1IGwgMTYuNTYyNSwxNi41NDAxNyBjIDAuNTU4MMSC41NTWGNCAxLjQ1MDg5LDAuNTU4MDQgMi4wMDg5MiwwIGwgMTYUNTYyNSSwtMTYuNTQwMTcgeiIgLz4KICAgIDwZz4KICA8L2c+Cjwvc3ZnPgo=“);}<link href=“https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css“rel=”stylesheet“/><section class=“container”><form class=“form horizontal”><select class=“表单控件”><option>一个</option><option>两个</option></选择></form></section>

注意:base64在SVG中是一个V字形。

博客文章HowtoCSS表单下拉样式没有JavaScript适合我,但在Opera中失败了:

选择{边框:0无;颜色:#FFFFFF;背景:透明;字体大小:20px;字号:粗体;填充:2px 10px;宽度:378px;*宽度:350px;*背景:#58B14C;}#主要选举{溢出:隐藏;宽度:350px;-moz边框半径:9px 9px 9px 9px;-webkit边框半径:9px 9px 9px 9px;边框半径:9px 9px 9px 9px;方框阴影:1px 1px 11px#3330033;background:url(“arrow.gif”)无重复滚动319px 5px#58B14C;}<div id=“mainselection”><选择><option>选择选项</option><option>选项1</option><option>选项2</option></选择></div>

本机解决方案

下面是一个简单的HTML/CSS示例:https://jsfiddle.net/dkellner/e1jdspvb/

诀窍:由于某种原因,当你给select标记一个size属性时,它会突然理解CSS。通常,此属性用于创建始终可见的固定高度列表,但作为一个副作用,您现在可以对其进行样式设置。因此,我们所做的只是给它一个大小,然后实现显示/隐藏机制,以返回下拉列表的感觉。

简约版,不像示例那样时尚,但更容易理解:

<style>

    .stylish > span {position:relative;}
    .stylish select {position:absolute;left:0px;transform:scaleY(0);transform-origin:top center;}
    .stylish.dropped-down select {transform:scaleY(1);}

</style>
...
<div class="stylish">
    <label> Choose your superhero: </label>
    <span>
        <input   onclick  = "this.closest('div').classList.toggle('dropped-down');"><br>
        <select  onclick  = "this.closest('div').classList.remove('dropped-down');"
                 onchange = "this.closest('div').querySelector('input').value=this.value;"
                 size=9
        >
            <optgroup label="Fantasy"></optgroup>

            <option value="gandalf">  Gandalf       </option>
            <option value="harry">    Harry Potter  </option>
            <option value="jon">      Jon Snow      </option>

            ...                
            
        </select>
    </span>
</div>

附加说明

这实际上实现了一个可编辑的下拉列表;使用只读以避免编辑<optgroup>标记是空的(不是围绕选项标记),这样更具风格