Change an HTML5 input's placeholder color with CSS

    |
  • Added:
  • |
  • In: Wordpress

Chrome supports the placeholder attribute on input[type=text] elements (others probably do too).

But the following CSS doesn't do diddly squat to the placeholder's value:

input[placeholder], [placeholder], *[placeholder] { color: red !important; }
<input type="text" placeholder="Value">

Value will still remain grey instead of red.

Is there a way to change the color of the placeholder text?

I'm already using the jQuery placeholder plugin for the browsers that don't support the placeholder attribute natively.

This Question Has 30 Answeres | Orginal Question | David Murdoch

input::-webkit-input-placeholder { color: pink; } input::-moz-placeholder { color: pink; } input:-ms-input-placeholder { color: pink; } input:-moz-placeholder { color: pink; }
<input type="text" placeholder="Value">

For Bootstrap and Less users, there is a mixin .placeholder:

// Placeholder text // ------------------------- .placeholder(@color: @placeholderText) { &:-moz-placeholder { color: @color; } &:-ms-input-placeholder { color: @color; } &::-webkit-input-placeholder { color: @color; } } 

For Bootstrap users, if you are using class="form-control", there may be a CSS specificity issue. You should get a higher priority:

.form-control::-webkit-input-placeholder { color: red; } //.. and other browsers 

Or if you are using Less:

.form-control{ .placeholder(red); } 

I don't remember where I've found this code snippet on the Internet (it wasn't written by me, don't remember where I've found it, nor who wrote it).

$('[placeholder]').focus(function() { var input = $(this); if (input.val() == input.attr('placeholder')) { input.val(''); input.removeClass('placeholder'); } }).blur(function() { var input = $(this); if (input.val() == '' || input.val() == input.attr('placeholder')) { input.addClass('placeholder'); input.val(input.attr('placeholder')); } }).blur(); $('[placeholder]').parents('form').submit(function() { $(this).find('[placeholder]').each(function() { var input = $(this); if (input.val() == input.attr('placeholder')) { input.val(''); } }) }); 

Just load this JavaScript code and then edit your placeholder with CSS by calling this rule:

form .placeholder { color: #222; font-size: 25px; /* etc. */ } 

/* do not group these rules */ *::-webkit-input-placeholder { color: red; } *:-moz-placeholder { /* FF 4-18 */ color: red; } *::-moz-placeholder { /* FF 19+ */ color: red; } *:-ms-input-placeholder { /* IE 10+ */ color: red; }
<input placeholder='hello'/> <br /> <textarea placeholder='hello'></textarea>

This will style all input and textarea placeholders.

Important Note: Do not group these rules. Instead, make a separate rule for every selector (one invalid selector in a group makes the whole group invalid).

<!DOCTYPE html> <html> <body> <style> ::-webkit-input-placeholder { color: red; } :-moz-placeholder { /* Firefox 18- */ color: red; } ::-moz-placeholder { /* Firefox 19+ */ color: red; } :-ms-input-placeholder { color: red; } </style> <form action="demo_form.asp"> <input type="text" name="fname" placeholder="First name"><br> <input type="text" name="lname" placeholder="Last name"><br> <input type="submit" value="Submit"> </form> </body> </html> 

Note: The placeholder attribute of the input tag is not supported in Internet Explorer 9 and earlier versions.

In addition to toscho's answer I've noticed some webkit inconsistencies between Chrome 9-10 and Safari 5 with the CSS properties supported that are worth noting.

Specifically Chrome 9 and 10 do not support background-color, border, text-decoration and text-transform when styling the placeholder.

The full cross-browser comparison is here.

I just realize something for Mozilla Firefox 19+ that the browser gives an opacity value for the placeholder, so the color will not be what you really want.

input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #eee; opacity:1; } input:-moz-placeholder, textarea:-moz-placeholder { color: #eee; opacity:1; } input::-moz-placeholder, textarea::-moz-placeholder { color: #eee; opacity:1; } input:-ms-input-placeholder, textarea:-ms-input-placeholder { color: #eee; opacity:1; } 

I overwrite the opacity for 1, so it will be good to go.

You may also want to style textareas:

input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #636363; } input:-moz-placeholder, textarea:-moz-placeholder { color: #636363; } 

Try this simple and easy way for target placeholder

input::-webkit-input-placeholder { /* Chrome */ color: red; } input:-ms-input-placeholder { /* IE 10+ */ color: red; } input::-moz-placeholder { /* Firefox 19+ */ color: red; } input:-moz-placeholder { /* Firefox 4 - 18 */ color: red; } 

Use the new ::placeholder if you use autoprefixer.

Note that the .placeholder mixin from Bootstrap is deprecated in favor of this.

Example:

input::placeholder { color: black; } 

When using autoprefixer the above will be converted to the correct code for all browsers.

I think this code will work because a placeholder is needed only for input type text. So this one line CSS will be enough for your need:

input[type="text"]::-webkit-input-placeholder { color: red; } 

 ::-webkit-input-placeholder { /* Chrome/Opera/Safari */ color: pink; } ::-moz-placeholder { /* Firefox 19+ */ color: pink; } :-ms-input-placeholder { /* IE 10+ */ color: pink; } :-moz-placeholder { /* Firefox 18- */ color: pink; }
<input type="text" placeholder="Value">

This short and clean code:

::-webkit-input-placeholder{color: #ddd;} :-moz-placeholder{color:#ddd;/* For Firefox 18- */} ::-moz-placeholder{color:#ddd;/* For Firefox 19+ */} :-ms-input-placeholder{color:#ddd;} 

In Firefox and Internet Explorer, the normal input text color overrides the color property of placeholders. So, we need to

::-webkit-input-placeholder { color: red; text-overflow: ellipsis; } :-moz-placeholder { color: #acacac !important; text-overflow: ellipsis; } ::-moz-placeholder { color: #acacac !important; text-overflow: ellipsis; } /* For the future */ :-ms-input-placeholder { color: #acacac !important; text-overflow: ellipsis; } 

Compass has a mixin for this out of the box.

Take your example:

<input type="text" placeholder="Value"> 

And in SCSS using compass:

input[type='text'] { @include input-placeholder { color: #616161; } } 

See docs for the input-placeholder mixin.

This will work fine. DEMO HERE:

input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { color: #666; } input:-moz-placeholder, textarea:-moz-placeholder { color: #666; } input::-moz-placeholder, textarea::-moz-placeholder { color: #666; } input:-ms-input-placeholder, textarea:-ms-input-placeholder { color: #666; }
<input type="text" placeholder="Value" />

Now we have a standard way to apply CSS to an input's placeholder : ::placeholder pseudo-element from this CSS Module Level 4 Draft.

Placeholder depends on some browser support and some CSS, but some support different CSS.

.yourClassName::-webkit-input-placeholder { color: #00bcea; } .yourClassName::-moz-placeholder { color: #00bcea; } .yourClassName:-ms-input-placeholder { color: # 00 bcea; } 

And you can also use input/ textarea as well.

For SASS/SCSS user using Bourbon, it has a built-in function.

//main.scss @import 'bourbon'; input { width: 300px; @include placeholder { color: red; } } 

CSS Output, you can also grab this portion and paste into your code.

//main.css input { width: 300px; } input::-webkit-input-placeholder { color: red; } input:-moz-placeholder { color: red; } input::-moz-placeholder { color: red; } input:-ms-input-placeholder { color: red; } 

I have tried every combination here to change the color, on my mobile platform, and eventually it was:

-webkit-text-fill-color: red; 

which did the trick.

Cross-browser solution:

/* all elements */ ::-webkit-input-placeholder { color:#f00; } ::-moz-placeholder { color:#f00; } /* firefox 19+ */ :-ms-input-placeholder { color:#f00; } /* ie */ input:-moz-placeholder { color:#f00; } /* individual elements: webkit */ #field2::-webkit-input-placeholder { color:#00f; } #field3::-webkit-input-placeholder { color:#090; background:lightgreen; text-transform:uppercase; } #field4::-webkit-input-placeholder { font-style:italic; text-decoration:overline; letter-spacing:3px; color:#999; } /* individual elements: mozilla */ #field2::-moz-placeholder { color:#00f; } #field3::-moz-placeholder { color:#090; background:lightgreen; text-transform:uppercase; } #field4::-moz-placeholder { font-style:italic; text-decoration:overline; letter-spacing:3px; color:#999; } 

Credit: David Walsh

*::-webkit-input-placeholder { color: red; } *:-moz-placeholder { color: red; } *::-moz-placeholder { color: red; } *:-ms-input-placeholder { color: red; } 

Here is one more example:

.form-control::-webkit-input-placeholder { color: red; width: 250px; } h1 { color: red; }
<div class="col-sm-4"> <input class="form-control" placeholder="Enter text here.." ng-model="Email" required/> </div>

For Sass users:

// Create placeholder mixin @mixin placeholder($color, $size:"") { &::-webkit-input-placeholder { color: $color; @if $size != "" { font-size: $size; } } &:-moz-placeholder { color: $color; @if $size != "" { font-size: $size; } } &::-moz-placeholder { color: $color; @if $size != "" { font-size: $size; } } &:-ms-input-placeholder { color: $color; @if $size != "" { font-size: $size; } } } // Use placeholder mixin (the size parameter is optional) [placeholder] { @include placeholder(red, 10px); } 

Placeholder have browser dependency, so you need to specify the browser dependency before using any style over the placeholder. I am mentioning some of the code for few browser.

::-webkit-input-placeholder{ // for chrome color: red; } :-moz-input-placeholder{ //for mozila color: green; } ::-ms-input-placeholder{ //for IE color: blue; } 

How about this

<input type="text" value="placeholder text" onfocus="this.style.color='#000'; this.value='';" style="color: #f00;"/> 

No CSS or placeholder, but you get the same functionality.

OK, placeholders behave differently in different browsers, so you need using browser prefix in your CSS to make them identical, for example Firefox gives a transparency to placeholder by default, so need to add opacity 1 to your css, plus the color, it's not a big concern most of the times, but good to have them consistent:

*::-webkit-input-placeholder { /* WebKit browsers */ color: #ccc; } *:-moz-placeholder { /* Mozilla Firefox <18 */ color: #ccc; opacity: 1; } *::-moz-placeholder { /* Mozilla Firefox 19+ */ color: #ccc; opacity: 1; } *:-ms-input-placeholder { /* Internet Explorer 10-11 */ color: #ccc; } 

Implementation

There are three different implementations: pseudo-elements, pseudo-classes, and nothing.

  • WebKit, Blink (Safari, Google Chrome, Opera 15+) and Microsoft Edge are using a pseudo-element: ::-webkit-input-placeholder. [Ref]
  • Mozilla Firefox 4 to 18 is using a pseudo-class: :-moz-placeholder (one colon). [Ref]
  • Mozilla Firefox 19+ is using a pseudo-element: ::-moz-placeholder, but the old selector will still work for a while. [Ref]
  • Internet Explorer 10 and 11 are using a pseudo-class: :-ms-input-placeholder. [Ref]

Internet Explorer 9 and lower does not support the placeholder attribute at all, while Opera 12 and lower do not support any CSS selector for placeholders.

The discussion about the best implementation is still going on. Note the pseudo-elements act like real elements in the Shadow DOM. A padding on an input will not get the same background color as the pseudo-element.

CSS selectors

User agents are required to ignore a rule with an unknown selector. See Selectors Level 3:

a group of selectors containing an invalid selector is invalid.

So we need separate rules for each browser. Otherwise the whole group would be ignored by all browsers.

::-webkit-input-placeholder { /* WebKit, Blink, Edge */ color: #909; } :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #909; opacity: 1; } ::-moz-placeholder { /* Mozilla Firefox 19+ */ color: #909; opacity: 1; } :-ms-input-placeholder { /* Internet Explorer 10-11 */ color: #909; } ::-ms-input-placeholder { /* Microsoft Edge */ color: #909; }
<input placeholder="Stack Snippets are awesome!">

Usage notes

  • Be careful to avoid bad contrasts. Firefox's placeholder appears to be defaulting with a reduced opacity, so needs to use opacity: 1 here.
  • Note that placeholder text is just cut off if it doesn’t fit – size your input elements in em and test them with big minimum font size settings. Don’t forget translations: some languages need more room for the same word.
  • Browsers with HTML support for placeholder but without CSS support for that (like Opera) should be tested too.
  • Some browsers use additional default CSS for some input types (email, search). These might affect the rendering in unexpected ways. Use the properties -webkit-appearance and -moz-appearance to change that. Example:
 [type="search"] { -moz-appearance: textfield; -webkit-appearance: textfield; appearance: textfield; } 

If you are using Bootstrap and couldn't get this working then probably you missed the fact that Bootstrap itself adds these selectors. This is Bootstrap v3.3 we are talking about.

If you are trying to change the placeholder inside a .form-control CSS class then you should override it like this:

.form-control::-webkit-input-placeholder { /* WebKit, Blink, Edge */ color: #777; } .form-control:-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: #777; opacity: 1; } .form-control::-moz-placeholder { /* Mozilla Firefox 19+ */ color: #777; opacity: 1; } .form-control:-ms-input-placeholder { /* Internet Explorer 10-11 */ color: #777; } 

Search
I am...

Sajjad Hossain

I have five years of experience in web development sector. I love to do amazing projects and share my knowledge with all.

Connect Social With PHPAns
Top