Search code examples
htmlcsshtml-selectbackground-color

Change select box option background color


I have a select box and I'm trying to change the background color of the options when the select box has been clicked and shows all the options.

body {
  background: url(http://subtlepatterns.com/patterns/black_linen_v2.png) repeat;
}

select {
  margin: 40px;
  background: rgba(0, 0, 0, 0.3);
  color: #fff;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
}
<select>
  <option val="">Please choose</option>
  <option val="1">Option 1</option>
  <option val="2">Option 2</option>
  <option val="3">Option 3</option>
  <option val="4">Option 4</option>
</select>


Solution

  • You need to apply the background-color to the option elements and not the select element:

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

    If you want to style each one of the option elements, use the CSS attribute selector. You can also use different class attributes for each <option>, if desired.

    select option {
      margin: 40px;
      background: rgba(0, 0, 0, 0.3);
      color: #fff;
      text-shadow: 0 1px 0 rgba(0, 0, 0, 0.4);
    }
    
    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);
    }
    <select>
      <option value="">Please choose</option>
      <option value="1">Option 1</option>
      <option value="2">Option 2</option>
      <option value="3">Option 3</option>
      <option value="4">Option 4</option>
    </select>