74

How to change the color of placeholder when focus the input field? I use this CSS code to set the default color, but how to change it on focus?

::placeholder { color: blue; }
Davide
  • 1,635
  • 1
  • 16
  • 29

11 Answers11

134

Try this, this should work :

input::-webkit-input-placeholder {
    color: #999;
}
input:focus::-webkit-input-placeholder {
    color: red;
}

/* Firefox < 19 */
input:-moz-placeholder {
    color: #999;
}
input:focus:-moz-placeholder {
    color: red;
}

/* Firefox > 19 */
input::-moz-placeholder {
    color: #999;
}
input:focus::-moz-placeholder {
    color: red;
}

/* Internet Explorer 10 */
input:-ms-input-placeholder {
    color: #999;
}
input:focus:-ms-input-placeholder {
    color: red;
}

Here is an example : http://jsfiddle.net/XDutj/27/

Pranav 웃
  • 8,469
  • 6
  • 38
  • 48
  • why there is not :-ms-input-placeholder? – Davide Nov 01 '12 at 19:10
  • 1
    I don't have IE to test the above, it would be great if someone could test it. AFAIK, IE9 lacks placeholder support : http://caniuse.com/#search=placeholder – Pranav 웃 Nov 01 '12 at 19:17
  • Works like a charm for me. Didn't test in IE though, obviously. – Sethen May 05 '13 at 18:31
  • 1
    No, sadly it does not work in IE 10 because IE uses the pseudo class `:-ms-input-placeholder` instead of a pseudo element `::-ms-placeholder`. Can't test the focus behavior, because IE per default hides the placeholder on focus (and unfortunately I didn't find a way to avoid that). [See this update to your Fiddle](http://jsfiddle.net/XDutj/27/). – Linus Caldwell May 09 '13 at 20:33
14

This works for me:

input:focus::placeholder {
  color: blue;
}
David Villegas
  • 458
  • 3
  • 18
5

In addition to Pranav answer I refined the code with textarea compatibility:

::-webkit-input-placeholder { color: #999; }
:-moz-placeholder { color: #999; }

:focus::-webkit-input-placeholder { color: #ccc; }
:focus:-moz-placeholder { color: #ccc; }​
Davide
  • 1,635
  • 1
  • 16
  • 29
2

The following worked for me:

input:focus::-webkit-input-placeholder
{
    color: red;
}
JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
1

Try this:

HTML

<input type='text' placeholder='Enter text' />

CSS

input[placeholder]:focus { color: red; }
Kevin Boucher
  • 16,426
  • 3
  • 48
  • 55
  • 2
    That wasn't necessary, but I suspect it's because, on further inspection, your code would color the actual value of the input on focus, rather than its placeholder text only. – BoltClock Nov 01 '12 at 18:46
1

I've found this solution with JQuery:

 $('input[type="text"]').each(function(){

    $(this).focus(function(){
      $(this).addClass('input-focus');
    });

    $(this).blur(function(){
      $(this).removeClass('input-focus');
    });

  });

with this css:

.input-focus::-webkit-input-placeholder { color: #f00; }    
.input-focus:-moz-placeholder { color: #f00; }
.input-focus:-ms-input-placeholder { color: #f00; }
Davide
  • 1,635
  • 1
  • 16
  • 29
1

Use star * to select everything

*::-webkit-input-placeholder { color: #999; }


*:-moz-placeholder { color: #999; }


*::-moz-placeholder { color: #999; }


*:-ms-input-placeholder { color: #999; }
Ari
  • 4,643
  • 5
  • 36
  • 52
  • 1
    Note: for some reason, you cannot use commas to apply multiple rules. These have to be declared separately as shown. (though you probably only need ms-input and ::placeholder now) – Vael Victus Jul 24 '18 at 15:11
1

Complete and updated (2021) example:

input::placeholder {
  color: blue;
}

input:focus::placeholder {
  color: green;
}
<label for="city">City:</label><br>
<input type="text" id="city" name="city" placeholder="your favorite city">
Davide
  • 1,635
  • 1
  • 16
  • 29
0

From Firefox 19: The :-moz-placeholder pseudo-class that matches form elements with the placeholder attribute has been removed, and the ::-moz-placeholder pseudo-element has been added instead.

input:focus::-moz-placeholder { color: transparent; }
-1

You can create a material design animated placeholder that shrinks on top when input field is focused.

<div class="field">
 <input type="text" name="user" required><br>
 <label>Enter Username</label>
 </div>

Basically the label field is going to act like a placeholder. We can do this only using css. Explained here http://www.voidtricks.com/create-material-design-animated-placeholder/

Anand Roshan
  • 335
  • 4
  • 2
-1

Try this, It definitely works -

input:focus::placeholder {
  color: blue;
}

Code example result -

input::placeholder {
  color: blue;
}

input:focus::placeholder {
  color: red;
}
<input type="text" placeholder="text here">
Vihan Gammanpila
  • 346
  • 4
  • 10