Last Updated: February 25, 2016
·
9.739K
· mrisaacs

CSS3 | Placeholder - Animation

An illustrative example HERE on jsFiddle.

#Update 2014-08-31: Added support for different Firefox versions.


placeholder-default-style

The first thing you need to do is to design your placeholder-style, if it you want it. In this case I choosed the color.

Syntax:

input[placeholder],
input:-moz-placeholder, /* Mozilla Firefox 4 to 18 */
input::-moz-placeholder { /* Mozilla Firefox 19+ */
  color: #8D8D8D;
}
input::-webkit-input-placeholder {
  color: #8D8D8D;
}
input:-ms-input-placeholder {
  color: #8D8D8D;
}

animation-settings

The whole behaviour of the placeholder-value is referenced to a keyframe rule named slidein.
I choosed 400ms for the animation-duration is 400ms and the animation starts after 100ms.
fill-mode (forward) means that the last keyframe will retained after the animation is completed.

Syntax:

input[placeholder]:focus,
input:focus:-moz-placeholder, /* Mozilla Firefox 4 to 18 */
input:focus::-moz-placeholder { /* Mozilla Firefox 19+ */
  -moz-animation-name: slidein;
  -moz-animation-delay: 0.1s;
  -moz-animation-duration: 0.4s;
  -moz-animation-fill-mode: forwards;
  -moz-animation-timing-function: ease-out;
}
input:focus::-webkit-input-placeholder {
  -webkit-animation-name: slidein;
  -webkit-animation-delay: 0.1s;
  -webkit-animation-duration: 0.4s;
  -webkit-animation-fill-mode: forwards;
  -webkit-animation-timing-function: ease-out;
}
input:focus:-ms-input-placeholder {
  opacity: 0;
}

create animation

With the animation-name you set before to be your keyframe-rule now you need the same name to create this keyframe-rule and have a linkage between the settings you made before with the rules you want.

Syntax:

@-webkit-keyframes slidein {
  from {
    opacity: 1;
    padding-left: 0px;
  }
  to {
    opacity: 0;
    padding-left: 10px;
  }
}
@-moz-keyframes slidein {
  from {
    opacity: 1;
    padding-left: 0px;
    padding-right: 10px;
  }
  to {
    opacity: 0;
    padding-left: 10px;
    padding-right: 0px;
  }
}​

text-style

Last but not least you need to define which style the characters in your input-field will be designed.

Syntax

input {
  color: black;
  padding-left: 0px;
  padding-right: 10px;
}

Note:

For Mozilla you must set the padding-right value on the start to the same as the end-value of padding-left, because it resizes the whole input-box. And you also need to padding-right it the same value as above. Because Firefox will switch between two sizes if you're onfocus.