'How to change placeholder color on focus?
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; }
Solution 1:[1]
This works for me:
input:focus::placeholder {
color: blue;
}
Solution 2:[2]
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 3:[3]
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:[4]
The following worked for me:
input:focus::-webkit-input-placeholder
{
color: red;
}
Solution 5:[5]
Try this:
HTML
<input type='text' placeholder='Enter text' />
CSS
input[placeholder]:focus { color: red; }
Solution 6:[6]
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:[7]
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:[8]
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:[9]
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:[10]
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:[11]
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/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow