Home > Web Front-end > CSS Tutorial > How Can I Change the Background Color of Select Box Options Using CSS?

How Can I Change the Background Color of Select Box Options Using CSS?

Patricia Arquette
Release: 2024-12-27 12:22:14
Original
528 people have browsed it

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;
}
Copy after login

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;
}
Copy after login

You can also use different class attributes for each

.option-1 {
  background-color: red;
}

.option-2 {
  background-color: green;
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template