HTML:如何在选择元素中设置项目的背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2789741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:59:16  来源:igfitidea点击:

HTML: how to set background color of item in select element

htmlselectbackground-color

提问by mike

how do you set the background color of an item in an HTML select list?

如何在 HTML 选择列表中设置项目的背景颜色?

采纳答案by mike

select.list1 option.option2
{
    background-color: #007700;
}

<select class="list1">
  <option value="1">Option 1</option>
  <option value="2" class="option2">Option 2</option>
</select>

回答by Pekka

I assume you mean the <select>input element?

我假设你的意思是<select>输入元素?

Support for that is pretty new, but FF 3.6, Chrome and IE 8 render this all right:

对此的支持是相当新的,但 FF 3.6、Chrome 和 IE 8 可以正常显示:

<select name="select">
  <option value="1" style="background-color: blue">Test</option>
  <option value="2" style="background-color: green">Test</option>
</select>

回答by Karl Stulik

I had this problem too. I found setting the appearance to none helped.

我也有这个问题。我发现将外观设置为无帮助。

.class {
    appearance:none;
    -moz-appearance:none;
    -webkit-appearance:none;

    background-color: red;
}

回答by nickf

Just like normal background-color: #f0f

和平常一样 background-color: #f0f

You just need a way to target it, eg: <option id="myPinkOption">blah</option>

你只需要一种方法来定位它,例如: <option id="myPinkOption">blah</option>