133

Is there a way to style the currently selected <option> element in a <select> element?

I could then give a background color to the currently selected option element? That way I can style the option that's currently viewable in the closed dropdown.

0

3 Answers 3

161

the :checked pseudo-class initially applies to such elements that have the HTML4 selected and checked attributes

Source: w3.org


So, this CSS works, although styling the color is not possible in every browser:

option:checked { color: red; } 

An example of this in action, hiding the currently selected item from the drop down list.

option:checked { display:none; }
<select> <option>A</option> <option>B</option> <option>C</option> </select>


To style the currently selected option in the closed dropdown as well, you could try reversing the logic:

select { color: red; } option:not(:checked) { color: black; } /* or whatever your default style is */ 
Sign up to request clarification or add additional context in comments.

5 Comments

Did you test it? It doesn't seem to work for me on FF8. update: it doesn't work in Chromium 15 either.
@emmett I too checked it in Chrome 16. As I said in my previous comment it styles the selected option in the dropdown list, but not the one in the closed viewable area.
@Web_Designer Ok, I see the issue. I edited my answer with another (wonky) solution.
accepted answer doesn't work for me in chrome. you can test here: jsfiddle.net/hk4s0ech
This doesn't work in Safari 17.4 on macOS.
23

Actually you can only style few CSS properties on :modified option elements. color does not work, background-color either, but you can set a background-image.

You can couple this with gradients to do the trick.

option:hover, option:focus, option:active, option:checked { background: linear-gradient(#5A2569, #5A2569); }
<select> <option>A</option> <option>B</option> <option>C</option> </select>

Works on gecko/webkit.

3 Comments

Note that you can also apply filter to it like this: Option:checked{filter:grayscale(1);} This is similar to how nowadays you can style checkboxes and radiobuttons in Chrome to make them non-blue.
This doesn't work either.
Seems to be a bug in Chrome, at least. option:checked is honored only after the focus leaves the selected option.
6

This worked for me :

select option { color: black; } select:not(:checked) { color: gray; }
<select> <option>A</option> <option>B</option> <option>C</option> </select>

3 Comments

This works with cypress, unlike :selected.
Interesting. But fails for background-color, which is always forced to the default (blue) in Chrome when the option state is active or focused.
You have one CSS rule that matches the <select> element and another rule that matches the <option>s. The style that <option> inherits from <select> due to being its child gets overriden by <option>'s own style that you define. And I don't think that <select> can be :checked. So you're essentially just doing option { color: black; } select { color: gray; }, which may or may not be what the asker wanted.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.