HTML 选择具有多种颜色的下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2720532/
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:51:30  来源:igfitidea点击:

HTML Select DropDown list with multiple colours

htmlcolorshtml-select

提问by mouthpiec

Is it possible to have different colours for different items in the dropdown list?

下拉列表中的不同项目是否可以有不同的颜色?

For example:

例如:

Option 1 = green
Option 2 = blue
etc

选项 1 = 绿色
选项 2 = 蓝色

回答by ACP

here is what you want Styling Dropdown Lists

这是您想要的样式下拉列表

    <style type="text/css">
    option.red {background-color: #cc0000; font-weight: bold; font-size: 12px;}
    option.pink {background-color: #ffcccc;}
    </style>

    <select name=colors>
    <option class="red" value= "../getting_started/">Getting Started </option>
    <option class="pink" value= "../getting_started/html_intro1.htm">- Intro to HTML
     </option>
    </select>

回答by Boris Guéry

CSS and HTML

CSS 和 HTML

#option-1 {
  color: red;
}
#option-2 {
  color: green;
}

#option-3 {
  color: yellow;
}

#option-4 {
  color: blue;
}
<select>
  <option id="option-1">Option 1</option>
  <option id="option-2">Option 2</option>
  <option id="option-3">Option 3</option>
  <option id="option-4">Option 4</option>
</select>

回答by Jacob Rask

I guess you mean a select? This should work:

我猜你是说select? 这应该有效:

option:nth-child(1) { background: green; }
option:nth-child(2) { background: blue; }

etc

等等