How to move placeholder to top on focus AND while typing?

前端 未结 7 1227
野的像风
野的像风 2020-11-29 17:38

I want the placeholder to move to the top when the textbox is on focus and also while the user is typing.

I\'m not sure if this is just html/css or any javascript to

7条回答
  •  粉色の甜心
    2020-11-29 18:12

    You could do it like this

    HTML:

    Your email address

    CSS:

    input:focus ~ .floating-label,
    input:not(:focus):valid ~ .floating-label{
      top: 8px;
      bottom: 10px;
      left: 20px;
      font-size: 11px;
      opacity: 1;
    }
    
    .inputText {
      font-size: 14px;
      width: 200px;
      height: 35px;
    }
    
    .floating-label {
      position: absolute;
      pointer-events: none;
      left: 20px;
      top: 18px;
      transition: 0.2s ease all;
    }
    

    Working JSFiddle here https://jsfiddle.net/273ntk5s/2/

提交回复
热议问题