38

I have a select element which has several items. I want to change the color of its first item, but it seems the color only shows when you click on the select dropdown. What I want is to change the color (like gray) when the page is loaded so users can see the first option color is different.

See the example here... http://jsbin.com/acucan/4/

css:

select{
  width: 150px;
  height: 30px;
  padding: 5px;
  color: green;
}
select option { color: black; }
select option:first-child{
  color: green;
}

html:

<select>
    <option>Item1</option>
    <option>Item2</option>
    <option>Item3</option>
</select>
Joshua
  • 3,055
  • 3
  • 22
  • 37
  • I do not understand what you are trying to achieve. The items are only visible when the menu is open. So how what is wrong about your solution? – Wottensprels Jul 12 '13 at 06:48
  • Does this answer your question? [How do change the color of the select box's option text?](https://stackoverflow.com/questions/8635317/how-do-change-the-color-of-the-select-boxs-option-text) – Flimm Jul 08 '20 at 12:47

9 Answers9

41

If the first item is to be used as a placeholder (empty value) and your select is required then you can use the :invalid pseudo-class to target it.

select {
  -webkit-appearance: menulist-button;
  color: black;
}

select:invalid {
  color: green;
}
<select required>
  <option value="">Item1</option>
  <option value="Item2">Item2</option>
  <option value="Item3">Item3</option>
</select>
Oscar Barrett
  • 3,135
  • 31
  • 36
28

What about this:

select{
  width: 150px;
  height: 30px;
  padding: 5px;
  color: green;
}
select option { color: black; }
select option:first-child{
  color: green;
}
<select>
  <option>one</option>
  <option>two</option>
</select>

http://jsbin.com/acucan/9

Flimm
  • 136,138
  • 45
  • 251
  • 267
mnsr
  • 12,337
  • 4
  • 53
  • 79
  • Amazing! Never thought about that, Thank you :) – Joshua Jul 12 '13 at 07:00
  • 8
    how do you change the color to black once an option is selected? – sQuijeW Oct 17 '13 at 17:45
  • 20
    all ` – MMachinegun Mar 31 '14 at 12:07
  • chrome/windows is working fine, but i'm unable to test on my mac at the moment. Try changing it to `select option:first-child{ color: green !important; }` – mnsr Mar 31 '14 at 22:08
  • 4
    To make this work in Webkit browsers on the Mac (Chrome and Safari), you'll need to disable the chrome for the select using `-webkit-appearance:none`. Of course, that will enable the style, but will remove all the rest of the select chrome as well, so you'll need to restyle everything else in the element as well. Fiddle: http://jsfiddle.net/wFP44/1/ – Ben Dyer Apr 13 '14 at 18:20
  • I wonder if having to change grandparent / grandchild styles and parent styles to different things has a name.... – adam rowe Nov 21 '16 at 13:24
  • select { color: green } makes them all green. option:first-child { color: green } is useless and doesn't do anything. chrome 55.0.2883.95 on Mac. – keif Dec 19 '16 at 20:02
  • @keif did you check the jsbin link? still works for me on chrome 55 – mnsr Dec 19 '16 at 22:38
  • @jzm I did not! The jsbin link works appropriately, I don't know how I missed that. – keif Feb 24 '17 at 19:41
  • 1
    @sQuijeW That is exactly what I want to know. Does anyone have any hint to this? – JohnnyThan Dec 19 '17 at 20:35
  • @JohnnyThan change the select (not first-child) color to black. in my code i had it as green. – mnsr Dec 25 '17 at 21:17
  • Both options are green. Mac - Google Chrome. – S J Nov 06 '21 at 12:10
9

For Option 1 used as the placeholder:

select:invalid { color:grey; }

All other options:

select:valid { color:black; }
EUGUK
  • 91
  • 1
  • 1
5

Here is a way so that when you select an option, it turns black. When you change it back to the placeholder, it turns back into the placeholder color (in this case red).

http://jsfiddle.net/wFP44/166/

It requires the options to have values.

$('select').on('change', function() {
  if ($(this).val()) {
return $(this).css('color', 'black');
  } else {
return $(this).css('color', 'red');
  }
});
select{
  color: red;
}
select option { color: black; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value="">Pick one...</option>
<option value="test1">Test 1</option>
<option value="test2">Test 2</option>
<option value="test3">Test 3</option>
</select>
Chloe
  • 25,162
  • 40
  • 190
  • 357
1

You can do this by using CSS: JSFiddle

HTML:

<select>
    <option>Text 1</option>
    <option>Text 2</option>
    <option>Text 3</option>
</select>

CSS:

select option:first-child { color:red; }

Or if you absolutely need to use JavaScript (not adviced for this): JSFiddle

JavaScript:

$(function() {
    $("select option:first-child").addClass("highlight");
});

CSS:

.highlight { color:red; }
glautrou
  • 3,140
  • 2
  • 29
  • 34
  • 8
    Unfortunately, neither of these work in Chrome 60 (pc), even with the !important modifier added – Bill_VA Aug 10 '17 at 17:26
1

I really wanted this (placeholders should look the same for text boxes as select boxes!) and straight CSS wasn't working in Chrome. Here is what I did:

First make sure your select tag has a .has-prompt class.

Then initialize this class somewhere in document.ready.

# Adds a class to select boxes that have prompt currently selected.
# Allows for placeholder-like styling.
# Looks for has-prompt class on select tag.
Mess.Views.SelectPromptStyler = Backbone.View.extend
  el: 'body'

  initialize: ->
    @$('select.has-prompt').trigger('change')

  events:
    'change select.has-prompt': 'changed'

  changed: (e) ->
    select = @$(e.currentTarget)
    if select.find('option').first().is(':selected')
      select.addClass('prompt-selected')
    else
      select.removeClass('prompt-selected')

Then in CSS:

select.prompt-selected {
  color: $placeholder-color;
}
smoyth
  • 649
  • 6
  • 13
1

It seems that the fancy way with pure css would be more expensive; let's see with ES6

CSS
select{color:#AE1250}

JS
document.querySelectorAll('select').forEach((s) => {
    s.addEventListener('change',(e)=>{
    s.style.color=s.value==''?'#AE1250':'#fff';});            
 });
Jairin
  • 11
  • 1
0

This code works on Chromium and change the color to black once an option is selected:

select {
  appearance: none;
}

select:invalid {
  color: green;
}

select option {
  color: black;
}
Flo Develop
  • 655
  • 1
  • 6
  • 9
0

Only Copy below jQuery script in your HTML:

Note: All options must have a value and the value of the first option must be empty(value="").

$(document).ready(function () {
    $("select").each(function (i, select) {
        SetSelectColor(select);
    });
    $("select option").css('color', 'black');
    $("select option:first-child[value='']").css('color', '#999999');
    $("select").change(Select_Changed);    
});

function Select_Changed() {
    SetSelectColor(this);   
}

function SetSelectColor(select) {
    var value = $(select).val();
    $(select).css('color', value != '' ? 'black' : '#999999');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<select>
    <option value="">Please select an option ...</option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
</select>