Chrome supports the placeholder attribute on input[type=text]
elements (others probably do too).
But the following CSS
doesn\'t do anything
Here is the solution with CSS selectors
::-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 { /* Microsoft Edge */
color: #909;
}
:-ms-input-placeholder { /* Internet Explorer 10-11 */
color: #909;
}
Adding an actual very nice and simple possibility: css filters!
It will style everything, including the placeholder.
The following will set both input
elements on the same palette, using the hue filter for color changes. It render very well now in browsers (except ie...)
input {
filter: sepia(100%) saturate(400%) grayscale(0) contrast(200%) hue-rotate(68deg) invert(18%);
}
<input placeholder="Hello world!" />
<input type="date" /><br>
<input type="range" />
<input type="color" />
To allow users to change it dynamically, using an input type color for changes, or to find nuances, check out this snippet:
From: https://codepen.io/Nico_KraZhtest/pen/bWExEB
function stylElem() {
stylo.dataset.hue = ((parseInt(stylo.value.substring(1), 16))/46666).toFixed(0)
Array.from(document.querySelectorAll('input, audio, video')).forEach(function(e){
e.style.cssText += ";filter:sepia(100%) saturate(400%)grayscale(0)contrast(200%)hue-rotate("+ stylo.dataset.hue+"deg)invert("+(stylo.dataset.hue/3.6)+"%)"
out.innerText = e.style.cssText
})()}
stylElem()
body {background: black; color: white}
Choose a color!
<input type="color" id="stylo" oninput="stylElem()">
<br>
<div id="out"></div> <p>
<input placeholder="Hello world!" />
<input type="date" /><br>
<input type="range" />
<br>
<audio controls src="#"></audio> <br><br>
<video controls src="#"></video>
Css filters docs: https://developer.mozilla.org/en-US/docs/Web/CSS/filter
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);
}
You can use this for input and focus style:
input::-webkit-input-placeholder { color:#666;}
input:-moz-placeholder { color:#666;}
input::-moz-placeholder { color:#666;}
input:-ms-input-placeholder { color:#666;}
/* focus */
input:focus::-webkit-input-placeholder { color:#eee; }
input:focus:-moz-placeholder { color:#eee } /* FF 4-18 */
input:focus::-moz-placeholder { color:#eee } /* FF 19+ */
input:focus:-ms-input-placeholder { color:#eee } /* IE 10+ */
A part of HTML:
<form action="www.anything.com">
<input type="text" name="name"
placeholder="Enter sentence"/>
</form>
I gonna show how to change color of expression of 'Enter sentence' by CSS:
::placeholder{
color:blue;
}
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.