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

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

Mary-Kate Olsen
Release: 2024-12-22 06:45:11
Original
127 people have browsed it

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

Change Select Box Option Background Color

Question:

I need to change the background color of select box options when the box is clicked and displays all the options. How can I achieve this?

Answer:

Here's the code you need to modify:

select option {
  margin: 40px;
  background: rgba(0, 0, 0, 0.3);
  color: #fff;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
}
Copy after login

By applying the background-color property to the option elements instead of the select element, you can change the background color of the options when the box is clicked.

Styling Individual Option Elements:

To style each option element differently, you can use the CSS attribute selector or assign different class attributes to each

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

select option[value="2"] {
  background: rgba(150, 150, 150, 0.3);
}

select option[value="3"] {
  background: rgba(200, 200, 200, 0.3);
}

select option[value="4"] {
  background: rgba(250, 250, 250, 0.3);
}
Copy after login

The above is the detailed content of How Can I Change the Background Color of Select Box Options?. 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