Home >Web Front-end >CSS Tutorial >How Can I Change the Background Color of Select Box Options Using CSS?
How to Change the Background Color of Select Box Options
When you click a select box, it displays a list of options. By default, the background color of these options is the same as the background color of the select box itself. However, you can use CSS to change the background color of the options.
To do this, you need to apply the background-color property to the option elements, not the select element. For example, the following CSS code will change the background color of all the options in a select box to black:
select option { background-color: black; }
If you want to style each option individually, you can use the CSS attribute selector. For example, the following CSS code will change the background color of the first option in a select box to red, the second option to green, and the third option to blue:
select option:first-child { background-color: red; } select option:nth-child(2) { background-color: green; } select option:nth-child(3) { background-color: blue; }
You can also use different class attributes for each
.option-1 { background-color: red; } .option-2 { background-color: green; }
The above is the detailed content of How Can I Change the Background Color of Select Box Options Using CSS?. For more information, please follow other related articles on the PHP Chinese website!