Floating Label

Floating Label

by Nirvanachain

HTML

<div class="field">
    <input type="text" name="fullname" id="fullname" placeholder="Jane Appleseed">
    <label for="fullname">Name</label>
</div>

CSS

.field {
  display: flex;
  flex-flow: column-reverse;
}

label, input {
  transition: all 0.2s;
}

input {
  font-size: 1.5em;
  border: 0;
  border-bottom: 1px solid #ccc;
}

input:focus {
  outline: 0;
  border-bottom: 1px solid #666;
}

input:placeholder-shown label {
  max-width: 66.66%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: text;
  transform-origin: left bottom; 
  transform: translate(0, 2.125rem) scale(1.5);
}
::-webkit-input-placeholder {
  transition: inherit;
  opacity: 0;
}

input:focus::-webkit-input-placeholder {
  opacity: 1;
}

input:not(:placeholder-shown) + label,
input:focus + label {
  transform: translate(0, 0) scale(1); /* [1] */
  cursor: pointer; /* [2] */
}

JavaScript

// Helpful link:
// http://thatemil.com/blog/2016/01/23/floating-label-no-js-pure-css/