html - 设置选择下选项的背景颜色

标签 html css

我找到了以下答案 How to change select box option background color?但它对我不起作用。

我的 html 文件中有以下列表

<select>
<option value="item 1" ></option>
<option value="item 2" ></option>
<option value="item 3"></option>
</select>

和下面的CSS

select option[value="item 1"]{
    background: rgba(100,100,100,0.3);
}

但我仍然得到一个没有背景颜色的空列表。 我想要得到的是某种只有颜色没有文本的下拉列表

像这样

enter image description here

谢谢

最佳答案

改为使用 nth-child() :

select option:nth-child(1) {
    background: rgba(100,100,100,0.3);
}

然后您也不需要在每次更改选项值时都更改 CSS。


编辑 - 我想你会的

select option:nth-child(1) {
    background: red;
}
select option:nth-child(2) {
    background: yellow;
}
select option:nth-child(3) {
    background: green;
}
<select>
<option value="item 1">&nbsp;&nbsp;</option>
<option value="item 2">&nbsp;&nbsp;</option>
<option value="item 3">&nbsp;&nbsp;</option>
</select>

关于html - 设置选择下选项的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18940491/

相关文章:

javascript - highcharts 错误 : <rect> attribute width: Expected length, "NaN"

jquery - 如何创建 jQuery Mobile 主题

html - 使用子菜单实现菜单

html - 无法在宽表中的列上设置宽度

java - 访问下拉列表中选定的数据

javascript - 如何通过单击正文内部来关闭侧边栏

html - 为什么此页面无法在 Internet Explorer 中正确呈现?

javascript - 如何将innerHTML添加到打印页面

html - 位置后的 Bootstrap 字形图标

html - Css id 使用了两次