1

I know how to achive that by writing a bit of code in typescript when (change) accures but I'm wondering what is the reason why this css code not working:

select[value="green"]{
  color:green;
}
select[value="red"]{
  color:red;
}

and the html example:

     <select class="custom-select">
        <option selected value="green" >my green option</option>
        <option value="red" >my red option</option>
     </select>

note: I don't mean color of text in dropdown menu itself but the color of text in select-space

1 Answer 1

3

Because you should use option element for CSS, not select (it's parent element):

option[value="green"]{
  color:green;
}
option[value="red"]{
  color:red;
}
<select class="custom-select">
  <option selected value="green" >my green option</option>
  <option value="red" >my red option</option>
</select>

Sign up to request clarification or add additional context in comments.

5 Comments

I guess I was unclear - I don't mean changing color of text in a drop down window I mean changing color of text in select-window depending on option selected- if U understan what I mean hehe:)
@pb4now, ok, I understand now. I'm searching for solution, but as I know, it's not possible via pure HTML/CSS
ok, strangely enough there is a 'value' attribute of <select> that changes with changing of option - but it exist under 'target' so I thought that maybe I should put something like that in css [target.value="green"] put that is not alowed syntax in css :s P.s if U put value="green" programaticly in select-tag then that css code works:S
@pb4now, looks like my suggestions were right and it's not possible. For example, here is an answer to similar question: stackoverflow.com/a/16345225/6053654. I also played around with some CSS trick such as :has(), :parent, ! and so on, but with no luck. Sorry (
@pb4now, I tried the way with value, but yes, as you said, it doesn't work, works only with static value attribute, declared in HTML. select is a little more complex, than it looks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.