Change an HTML5 input's placeholder color with CSS
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Chrome supports the placeholder attribute on input[type=text] elements (others probably do too).
But the following CSS doesn't do anything to the placeholder's value:
input[placeholder], [placeholder], *[placeholder] { color: red !important; }
Value will still remain grey instead of red.
Is there a way to change the color of the placeholder text?
回答1:
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]
April 2017: Most modern browsers support the simple pseudo-element ::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; } ::placeholder { /* Most modern browsers support this now. */ color: #909; }
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.
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:
/* 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; }
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).
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.
Now we have a standard way to apply CSS to an input's placeholder : ::placeholder pseudo-element from this CSS Module Level 4 Draft.
回答11:
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.
回答12:
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.
No CSS or placeholder, but you get the same functionality.
回答17:
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; }
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:
You can change an HTML5 input's placeholder color with CSS. If by chance, your CSS conflict, this code note working , you can use (!important) like below.
::-webkit-input-placeholder { /* WebKit, Blink, Edge */ color:#909 !important; } :-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color:#909 !important; opacity:1 !important; } ::-moz-placeholder { /* Mozilla Firefox 19+ */ color:#909 !important; opacity:1 !important; } :-ms-input-placeholder { /* Internet Explorer 10-11 */ color:#909 !important; } ::-ms-input-placeholder { /* Microsoft Edge */ color:#909 !important; }
Hope this will help.
回答27:
try this will help you this will work in all your fav browsers :