Material Design Input

by Ayyappan Sakthivadivel

HTML

<div class="container">
  <form>
    <div class="mdInput">
      <input id="lastName" name="lastName" type="text" required>
      <span></span>
      <label for="lastName">Name</label>
    </div>

    <div class="mdInput">
      <input id="firstName" name="firstName" type="text" required>
      <span></span>
      <label for="firstName">Vorname</label>
    </div>

    <div class="mdInput">
      <input id="eMail" name="eMail" type="text" required>
      <span></span>
      <label for="eMail">E-Mail</label>
    </div>

    <div class="mdInput">
      <input id="phone" name="phone" type="text" required>
      <span></span>
      <label for="phone">Telefon</label>
    </div>

    <div class="mdInput full">
      <textarea id="message" name="message" rows="8" required></textarea>
      <span></span>
      <label for="message">Nachricht</label>
    </div>
  </form>
</div>

CSS

* {
  box-sizing: border-box;
}

textarea {
  resize: vertical;
}

div.container {
  padding: 3em;
  overflow: hidden;
  background-color: #ffffff;
  font-family: arial;
}

div.mdInput {
  position: relative;
  float: left;
  margin-top: 3em;
  padding: 0 0.5em;
  width: 50%;
}

div.mdInput.full {
  width: 100%;
}

div.mdInput input,
div.mdInput textarea {
  display: block;
  padding: 1em 0.3em;
  width: 100%;
  border: none;
  border-bottom: 0.2em solid #000000;
}

div.mdInput input:focus,
div.mdInput textarea:focus {
  outline: none;
}

div.mdInput label {
  position: absolute;
  color: #9E9E9E;
  left: 0.8em;
  top: 1em;
  transition: 0.2s ease-in-out all;
  -moz-transition: 0.2s ease-in-out all;
  -webkit-transition: 0.2s ease-in-out all;
}

div.mdInput input:focus ~ label,
div.mdInput textarea:focus ~ label {
  left: 0.5em;
  top: -1.2em;
  color: #FF5722;
  font-size: 0.8em;
  font-weight: bold;
}

div.mdInput input:valid ~ label,
div.mdInput textarea:valid ~ label {
  left: 0.5em;
  top: -1.2em;
  color: #4CAF50;
  font-size: 0.8em;
  font-weight: bold;
}

div.mdInput span {
  display: block;
  position: relative;
}

div.mdInput span:before,
div.mdInput span:after,
div.mdInput textarea:before,
div.mdInput textarea:after {
  position: absolute;
  bottom: 0.01em;
  height: 0.2em;
  width: 0;
  background: #FF5722;
  content: '';
  transition: 0.2s ease all;
  -moz-transition: 0.2s ease all;
  -webkit-transition: 0.2s ease all;
}

div.mdInput span:before {
  left: 50%;
}

div.mdInput span:after {
  right: 50%;
}

div.mdInput input:focus ~ span:before,
div.mdInput input:focus ~ span:after,
div.mdInput textarea:focus ~ span:before,
div.mdInput textarea:focus ~ span:after {
  width: 50%;
}

div.mdInput input:valid ~ span:before,
div.mdInput input:valid ~ span:after,
div.mdInput textarea:valid ~ span:before,
div.mdInput textarea:valid ~ span:after {
  width: 50%;
  background-color: #4CAF50;
}