12

I am changing the opacity of an input field like this...

<body>
    <form>
        <input type="text" name="fname" placeholder="First name"><br>
    </form>
</body>

body{background:black}
input{height:30px;opacity:.5;}

http://jsfiddle.net/hbakrvnv/

This works but it also changes the opacity of the placeholder. How can I keep the placeholder text as white on top of the 50% opacity input field?

fightstarr20
  • 11,682
  • 40
  • 154
  • 278

2 Answers2

12

You can use this method in WebKit browsers:

body{background:black}
input{height:30px;opacity:.5;}
input::-webkit-input-placeholder {
     color: black; /*Change the placeholder color*/
     opacity: 0.5; /*Change the opacity between 0 and 1*/
}
<body>
    <form>
        <input type="text" name="fname" placeholder="First name"><br>
    </form>
</body>

If you are using IE10 (or higher) it is not possible to change the opacity, as you can see described in Internet Explorer Dev Center: Click Here

JSFiddle: http://jsfiddle.net/hbakrvnv/4/

EDIT: Fixed CSS mistake

morha13
  • 1,847
  • 3
  • 21
  • 42
5

Since ~2017 you can use the ::placeholder CSS pseudo-element like so

::placeholder {
  color: green;
}

.force-opaque::placeholder {
  opacity: 1;
}
andymel
  • 4,538
  • 2
  • 23
  • 35