Change select box option background color

后端 未结 10 741
挽巷
挽巷 2020-11-22 08:55

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.

相关标签:
10条回答
  • 2020-11-22 09:30

    Another option is to use Javascript:

    if (document.getElementById('selectID').value == '1') {
           document.getElementById('optionID').style.color = '#000';
    

    (Not as clean as the CSS attribute selector, but more powerful)

    0 讨论(0)
  • 2020-11-22 09:32

    enter image description here

    Similar to some of the answers, but not really stated, is to add a class to the actual option tag and use css classes...this is currently working for me without issue on IE (see above ss).

    <select id="reviewAction">
    <option class="greenColor">Accept and Advance Status</option>
    <option class="redColor">Return for Modifications</option>
    </select>
    

    CSS:

    .greenColor{
        background-color: #33CC33;
    }
    .redColor{
        background-color: #E60000;
    }
    
    0 讨论(0)
  • 2020-11-22 09:33

    Yes, you can set this by the opposite way:

    select { /* desired background */ }
    option:not(:checked) { background: #fff; }
    

    Check it working bellow:

    select {
      margin: 50px;
      width: 300px;
      background: #ff0;
      color: #000;
    }
    
    option:not(:checked) {
      background-color: #fff;
    }
    <select>
      <option val="">Select Option</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>

    0 讨论(0)
  • 2020-11-22 09:33

    My selects would not color the background until I added !important to the style.

        input, select, select option{background-color:#FFE !important}
    
    0 讨论(0)
提交回复
热议问题