How to make a dynamic input, where the placeholder becomes the title?

Asked

Viewed 266 times

1

I would like to create an input in which, when the user chooses to write, the placeholder becomes the title of the same (as in the Google login form). However, I have no idea how to do it. I have done some research and found nothing. Someone could help me?

esquema

1 answer

3


Hello, Matheus. This effect you want is called: Float Label. And it’s really cool. You can perform it with CSS3 quietly. I will add a code below so you can study and improve your projects.

Good studies.

.label-float {
  position: relative;
  padding-top: 13px;
}

.label-float input {
  border: 1px solid lightgrey;
  border-radius: 5px;
  outline: none;
  min-width: 250px;
  padding: 15px 20px;
  font-size: 16px;
  transition: all .1s linear;
  -webkit-transition: all .1s linear;
  -moz-transition: all .1s linear;
  -webkit-appearance: none;
}

.label-float input:focus {
  border: 2px solid #3951b2;
}

.label-float input::placeholder {
  color: transparent;
}

.label-float label {
  pointer-events: none;
  position: absolute;
  top: calc(50% - 8px);
  left: 15px;
  transition: all .1s linear;
  -webkit-transition: all .1s linear;
  -moz-transition: all .1s linear;
  background-color: white;
  padding: 5px;
  box-sizing: border-box;
}

.label-float input:required:invalid+label {
  color: red;
}

.label-float input:focus:required:invalid {
  border: 2px solid red;
}

.label-float input:required:invalid+label:before {
  content: '*';
}

.label-float input:focus+label,
.label-float input:not(:placeholder-shown)+label {
  font-size: 13px;
  top: 0;
  color: #3951b2;
}
<div class="label-float">
  <input type="text" placeholder=" " />
  <label>Telefone</label>
</div>
<br/>
<div class="label-float">
  <input type="text" placeholder=" " required/>
  <label>Nome de Usuário</label>
</div>

Good studies, I hope I helped. Hug

  • 1

    Thank you very much, really helped me a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.