JSFiddle - React, Tailwind, and code Playground

Form Placeholder to Label

by Jennifer Perrin

HTML

<h1>Form Placeholder Labels</h1>
<p class='explanation'>
  Try clicking in the form elements to see the placeholder elegantly turning into a label, helping you remember what the field is for.
</p>
<div class='form'>
  <div class='field'>
      <input class='placeholder-input' id='email' required='required' type='text' />
    <label class='placeholder-label' for='email'>Email</label>
  </div>
  <div class='field'>
      <input class='placeholder-input' id='password' required='required' type='password' />
    <label class='placeholder-label' for='password'>Password</label>
  </div>
</div>

CSS

* {
  box-sizing: border-box;
}

.form {
  width: 320px;
  margin: 30px auto;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 16px;
  transform: translateZ(0);
}
.form .field {
  position: relative;
  margin-bottom: 10px;
  height: 40px;
}
.form .field .placeholder-label {
  position: absolute;
  top: 14px;
  left: 12px;
  font-weight: 300;
  color: #aaa;
  cursor: text;
  z-index: 200;
  transition: all 0.25s;
}
.form .field .placeholder-label:after {
  content: ":";
  opacity: 0;
  color: #333;
  transition: all 0.25s;
}
.form .field .placeholder-input {
  position: absolute;
  width: 100%;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid #aaa;
  font-size: 16px;
  z-index: 100;
  transition: all 0.25s;
}
.form .field .placeholder-input:focus, .form .field .placeholder-input:valid {
  margin-left: 100px;
  width: 220px;
  outline: none;
}
.form .field .placeholder-input:focus + .placeholder-label, .form .field .placeholder-input:valid + .placeholder-label {
  color: #333;
  cursor: default;
}
.form .field .placeholder-input:focus + .placeholder-label:after, .form .field .placeholder-input:valid + .placeholder-label:after {
  opacity: 1;
}

body {
  font-family: Helvetica, Arial, sans-serif;
  font-weight: 300;
}

h1 {
  margin-top: 50px;
  font-weight: 300;
  color: #333;
  text-align: center;
}

.explanation {
  width: 500px;
  margin: 0 auto;
  line-height: 1.5;
  text-align: center;
}