How to change placeholder color on focus?

CssInputFocusPlaceholderOnfocus

Css Problem Overview


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; }

Css Solutions


Solution 1 - Css

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/

Solution 2 - Css

This works for me:

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

Solution 3 - Css

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; }​

Solution 4 - Css

The following worked for me:

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

Solution 5 - Css

Try this:

HTML

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

CSS

input[placeholder]:focus { color: red; }

Solution 6 - Css

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; }

Solution 7 - Css

Use star * to select everything

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


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


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


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

Solution 8 - Css

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">

Solution 9 - Css

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; }

Solution 10 - Css

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">

Solution 11 - Css

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/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavideView Question on Stackoverflow
Solution 1 - CssPranav 웃View Answer on Stackoverflow
Solution 2 - CssDavid VillegasView Answer on Stackoverflow
Solution 3 - CssDavideView Answer on Stackoverflow
Solution 4 - CssBelView Answer on Stackoverflow
Solution 5 - CssKevin BoucherView Answer on Stackoverflow
Solution 6 - CssDavideView Answer on Stackoverflow
Solution 7 - CssAriView Answer on Stackoverflow
Solution 8 - CssDavideView Answer on Stackoverflow
Solution 9 - CssLeszek PietrzakView Answer on Stackoverflow
Solution 10 - CssVihan GammanpilaView Answer on Stackoverflow
Solution 11 - CssAnand RoshanView Answer on Stackoverflow