简体   繁体   English

如何使用CSS设置<option>的样式?

[英]How to style the <option> with only CSS?

Note: This question is not about making a custom dropdown. 注意:此问题与制作自定义下拉列表无关。 It's only about possibilities of styling <option> elements within the select element in CSS 它只是关于在CSS中的select元素中设置<option>元素样式的可能性

How can I style <option> s of a <select> element with cross-browser compatibility? 如何设置具有跨浏览器兼容性的<select>元素的<option> I know many JavaScript ways which customize the dropdown to convert into <li> , which I'm not asking about. 我知道很多JavaScript方法可以自定义下拉列表以转换为<li> ,我不会问。

<select class="select">
    <option selected>Select</option>
    <option>Blue</option>
    <option >Red</option>
    <option>Green</option>
    <option>Yellow</option>
    <option>Brown</option>
</select>

I'm asking what could be possible with CSS only, with compatibility for IE9+, Firefox, and Chrome. 我问的是,只有CSS可以实现什么,兼容IE9 +,Firefox和Chrome。

在此输入图像描述

I want to style like this or as close as possible. 我希望这样或尽可能接近样式。

在此输入图像描述

I tried here http://jsfiddle.net/jitendravyas/juwz3/3/ , but Chrome doesn't show any styling except font color, while Firefox shows some more. 我在这里试过http://jsfiddle.net/jitendravyas/juwz3/3/ ,但Chrome除了显示字体颜色外没有显示任何样式,而Firefox则显示更多。 How to get border and padding work in Chrome too? 如何在Chrome中获得边框和填充功能?

EDIT 2015 May 编辑2015年5月

Disclaimer: I've taken the snippet from the answer linked below: 免责声明:我从以下链接的答案中摘取了片段:

Important Update! 重要更新!

In addition to WebKit, as of Firefox 35 we'll be able to use the appearance property: 除了WebKit之外,从Firefox 35开始,我们将能够使用appearance属性:

Using -moz-appearance with the none value on a combobox now remove the dropdown button 在组合框上使用-moz-appearancenone值现在删除下拉按钮

So now in order to hide the default styling, it's as easy as adding the following rules on our select element: 所以现在为了隐藏默认样式,就像在select元素上添加以下规则一样简单:

select {
   -webkit-appearance: none;
   -moz-appearance: none;
   appearance: none;
}

For IE 11 support, you can use [ ::-ms-expand ][15]. 对于IE 11支持,您可以使用[ ::-ms-expand ] [15]。

select::-ms-expand { /* for IE 11 */
    display: none;
}

Old Answer 老答案

Unfortunately what you ask is not possible by using pure CSS. 不幸的是,使用纯CSS无法满足您的要求。 However, here is something similar that you can choose as a work around. 但是,这里有类似的东西,你可以选择作为一种解决方法。 Check the live code below. 检查下面的实时代码。

 div { margin: 10px; padding: 10px; border: 2px solid purple; width: 200px; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; } div > ul { display: none; } div:hover > ul {display: block; background: #f9f9f9; border-top: 1px solid purple;} div:hover > ul > li { padding: 5px; border-bottom: 1px solid #4f4f4f;} div:hover > ul > li:hover { background: white;} div:hover > ul > li:hover > a { color: red; } 
 <div> Select <ul> <li><a href="#">Item 1</a></li> <li><a href="#">Item 2</a></li> <li><a href="#">Item 3</a></li> </ul> </div> 

EDIT 编辑

Here is the question that you asked some time ago. 这是你前一段时间提出的问题。 How to style a <select> dropdown with CSS only without JavaScript? 如何在没有JavaScript的情况下使用CSS设置<select>下拉列表的样式? As it tells there, only in Chrome and to some extent in Firefox you can achieve what you want. 正如它所告诉的那样,只有在Chrome中,在某种程度上在Firefox中,你才能实现你想要的。 Otherwise, unfortunately, there is no cross browser pure CSS solution for styling a select. 否则,遗憾的是,没有用于样式化选择的跨浏览器纯CSS解决方案。

There is no cross-browser way of styling option elements, certainly not to the extent of your second screenshot. 没有跨浏览器的样式选项元素的方式,当然不是你的第二个截图的范围。 You might be able to make them bold, and set the font-size, but that will be about it... 您可以将它们设置为粗体,并设置字体大小,但这将是关于它...

I've played around with select items before and without overriding the functionality with JavaScript, I don't think it's possible in Chrome. 我已经玩过选择项目之前和不用JavaScript覆盖功能,我认为这不可能在Chrome中。 Whether you use a plugin or write your own code, CSS only is a no go for Chrome/Safari and as you said, Firefox is better at dealing with it. 无论您是使用插件还是编写自己的代码,CSS都不适用于Chrome / Safari,正如您所说,Firefox更擅长处理它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM