Code Snippet
Style Placeholder Text
Placeholder text in inputs has (in the browsers implementing it so far) a light gray color. To style it, you'll need vendor prefix CSS properties.
::-webkit-input-placeholder {
color: red;
}
:-moz-placeholder {
color: red;
}
123
456789
Always saving my day. ^^
Awesome Chris — thanks for saving my day again!
nice one..am adding that if you want to add a background-image (eg..search icon )..you can control the space between the place-holder text and that image using the padding of the “father” input tag :)
thanx :)
Thanks. Using Placeholders for the first time. HTML5 rocks!
Hi,
that’s cool, now i can realise placeholder without Java Script.
Thank you.
Why does doing it this way not work??…
::-webkit-input-placeholder, :-moz-placeholder {
color: red;
}
or
::-webkit-input-placeholder, input:-moz-placeholder {
color: red;
}
That would be nice, but the problem is that when a browser doesn’t understand a selector, it invalidates the entire line of selectors (except IE 7).
Hi,
On my application, when i click on the placeholder text inside the textfield,the cursor does not appear but when I click at the end/outside the text ,the cursor appears .
Enter the username
Double click on the text “Enter the username” works(can type anything inside textfield).
click on the end/outside the text works.(can type anything inside textfield).
But single click on the text and type does not work.(cannot see cursor at all)
What could be the problem here?
Thanks
Not supported in FF5? Can anybody else confirm this?
Nevermind, found the issue. If you have a more specific selector to style your regular input text color, then input:-moz-placeholder is actually overridden.
Example:
#selector input{
color:red
}
input:-moz-placeholder{
color:green
}
Result: placeholder will be red. Instead you’ll need to do:
#selector input{
color:red;
}
#selector input:-moz-placeholder{
color:green;
}
Maybe that’s obvious, but it tripped me up for a few minutes.
Noticed when testing it myself that both the webkit and mozilla prefixes needed double colons, and worked just fine when using specific input id’s.
Just to share a useful tip which rely on this rule. On iOS, the placeholder doesn’t disapear on focus. So you can use “:focus::-webkit-input-placeholder{color:transparent;}” and it works very well. :)
You might want to add the following for IE 10 support:
:-ms-input-placeholder {
color: red;
}
Don’t work for me. I had to use !important for them…
By the way, why the placeholder for webkit don’t fade away when we click on the field as it does in Firefox? Do we need to use js?
Really sorry about my last comment, I didn’t read the helpful tip from Jeremy Benaim…
Hi,
I am getting extra left space other than padding in placeholder.
Regards,
Sonam
Hi,
I am getting extra left space other than padding in placeholder.
but it is happening only in iphone browser
Regards,
Sonam
Thanks for the tip – “That would be nice, but the problem is that when a browser doesn’t understand a selector, it invalidates the entire line of selectors (except IE 7).”
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.