Style is not influenced by the css for – Aprillion Feb 14 '16 at 09:39

  • @Aprillion I just tried the fiddler on `Chrome 48.0.2564.109 m` and `IE 11.0.9600.17239` it still worked. Can you confirm you don't have so other CSS or JS influencing the select element? – Justin Feb 15 '16 at 14:04
  • did you compare with accepted answer? the fiddle here works the same as in question. but the select itself is not styled. – Aprillion Feb 15 '16 at 14:11
  • @Aprillion Ahh, I see what you mean. I'm looking at this, but I think Chrome and IE treat the select and option list as seperate elements. That is why the selected option's background color doesn't get properly rendered. My have to resort to some JS trickery to get the desired effect. – Justin Feb 15 '16 at 14:39
  • 1

    Pure CSS Method:-

    You can style it if you want to apply style if empty value selected using :valid selector like the following code

    Display in Red If Selected value is Empty

    select > option {
      color: black;
      font-weight:initial;
    }
    select option[value=""] {
      color: red;
      font-weight:bold;
    }
    select[required]:invalid {
      color: red;
      font-weight:bold;
    }
    <select required name="fontSize">
    <option value="">Please select</option>
    <option value="9">9 px</option>
    <option value="10">10 px</option>
    <option value="11">11 px</option>
    <option value="12">12 px</option>
    <option value="13">13 px</option>
    <option value="14">14 px</option>
    <option value="15">15 px</option>
    <option value="16">16 px</option>
    </select>
    <select required name="fontColor">
    <option value="">Please select</option>
    <option value="red" selected>Red</option>
    <option value="green">Green</option>
    <option value="blue">Blue</option>
    </select>
    jafarbtech
    • 6,842
    • 1
    • 36
    • 55
    • That does not answer the question exactly. The problem becomes apparent as the form is attempted submitted with the above required control with the placeholder (`value=""`) option chosen -- it won't submit because according to HTML 5 the [placeholder label option](https://html.spec.whatwg.org/#the-select-element:placeholder-label-option) does not make the control valid when chosen. Effectively you're just styling a `select` element based on validity, and even though it worked for you and is a useful answer, it should be made clear this does not really answer the original question. – Armen Michaeli Jun 23 '21 at 14:59
    • @amn the original question is to "style a select element based on what option" not about form get submitted or not. he has clearly indicated it as a css question. – jafarbtech Jun 24 '21 at 04:04
    • You must have misinterpreted the question -- it asks "based on what option is selected". What your answer explains instead is how to style the select element based on its validity (use of `:valid` pseudo-class). Styling a control based on validity is not the same as styling it based on *what* option is selected. Whether this is a CSS question is of no significance and not something I specifically argued. – Armen Michaeli Jun 24 '21 at 11:02
    • @amn true. in CSS this is the maximum thing one can style based on the value. because `select` element doesn't even support pattern. that is why I have indicated the maximum CSS styling – jafarbtech Jun 25 '21 at 02:09
    • Warning! This answer is misleading and may seem as to be working if you run the snippet. **But the select is red only because of the rule :invalid**, not because of the first – Martin L. Jun 09 '22 at 20:40