Change select box option background color

后端 未结 10 757
挽巷
挽巷 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:11

    I don't know if you've considered it or not but if your application is based on coloring various groupings of items you should probably use the <optgroup> tag coupled with a class for further referencing. For example:

    <select>
        <optgroup label="Numbers" class="green">
            <option value="1">One</option>
            <option value="2">Two</option>
            <option value="3">Three</option>
        </optgroup>
    
        <optgroup label="Letters" class="blue">
            <option value="a">A</option>
            <option value="b">B</option>
            <option value="c">C</option>
        </optgroup>
    </select>
    

    and then in the head of your document write the css like this:

    <style type="text/css">
        .green option{
            background-color:#0F0;
        }
    
        .blue option{
            background-color:#00F;
        }
    </style>
    
    0 讨论(0)
  • 2020-11-22 09:12

    You need to put background-color on the option tag and not the select tag...

    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 tags.. use the css attribute selector:

    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>

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

    If you really want to style the within a , consider switching to a Javascript/CSS based drop down such as http://getbootstrap.com/2.3.2/components.html#dropdowns or https://silviomoreto.github.io/bootstrap-select/examples/. This because browsers such as IE do not allow styling of options within elements. Chrome/OSX also has this problem - you cannot style options.

    However a warning is attached to that approach. These types of menus work very differently on mobile platforms because native elements aren't used. They can have annoying quirks on desktop as well. My advice is 1) don't write your own and 2) find a library that's been really well tested.

    0 讨论(0)
  • 2020-11-22 09:15
    $htmlIngressCss='<style>';
    $multiOptions = array("" => "All");
    $resIn = $this->commonDB->getIngressTrunk();
    while ($row = $resIn->fetch()) {
        if($row['IsActive']==0){
            $htmlIngressCss .= '.ingressClass select, option[value="'.$row['TrunkInfoID'].'"] {color:red;font-weight:bold;}';
        }
        $multiOptions[$row['TrunkInfoID']] = $row['IngressTrunkName'];
    }
    $htmlIngressCss.='</style>';
    

    add $htmlIngressCss in your html portion :)

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

    Simply change bg color

    select { background: #6ac17a; color:#fff; }

    0 讨论(0)
  • Here it goes what I've learned about the subject!

    The CSS 2 specification did not address the problem of how form elements should be presented to users period!

    Read here: smashing magazine

    Eventually, you will never find any technical article from w3c or other addressed to this topic. Styling form elements in particular select boxes is not fully supported however, you can drive around... with some effort!

    Styling HTML Form Elements

    Building Custom Widgets

    Don't waste time with hacks e such read the links and learn how pros get the job done!

    0 讨论(0)
提交回复
热议问题