Change the placeholder color of an HTML input using CSS
P粉068510991
2023-08-27 11:17:26
<p>Chrome v4 supports placeholder attributes on the <code>input[type=text]</code> element (others may do the same). </p>
<p>However, the following CSS does nothing with the placeholder value: </p>
<p><br /></p>
<pre class="brush:css;toolbar:false;">input[placeholder], [placeholder], *[placeholder] {
color: red !important;
}</pre>
<pre class="brush:html;toolbar:false;"><input type="text" placeholder="Value"></pre>
<p><br /></p>
The <p><code>value</code> will remain <code>grey</code> instead of <code>red</code>. </p>
<p><strong>Is there a way to change the color of the placeholder text? </strong></p>
This will style all
input
andtextarea
placeholders.Important Note: Do not group these rules. Instead, make separate rules for each selector (an invalid selector in a group invalidates the entire group).
Implementation
There are three different implementations: pseudo-element, pseudo-class and none.
::-webkit-input-placeholder
. [refer to]:-moz-placeholder
(a colon). [refer to]::-moz-placeholder
, but the old selector will still work for a while. [refer to]:-ms-input-placeholder
. [refer to]::placeholder
[Ref]Internet Explorer 9 and lower does not support the
placeholder
attribute at all, and Opera 12 and lower does not support any placeholder CSS selector.Discussions on best implementation options continue. Note that pseudo-elements behave just like real elements in the shadow of the DOM. The
padding
oninput
will not get the same background color as the pseudo element.CSS Selector
User agents are required to ignore rules with unknown selectors. SeeSelector Level 3:
So we need to develop separate rules for each browser. Otherwise the entire group will be ignored by all browsers.