Move placeholder above the input on focus

后端 未结 4 1601
南方客
南方客 2020-12-14 23:41

I\'m looking for css code that move the placeholder text above the input on focus. I found this code here. This code is perfect but my input tag is wrapped inside <

4条回答
  •  [愿得一人]
    2020-12-15 00:38

    You can use the CSS pseudo-selector :placeholder-shown in this case to detect when to move a fake placeholder out of the way. See example below:

    label {
      margin:20px 0;
      position:relative;
      display:inline-block;
    }
      
    span {
      padding:10px;
      pointer-events: none;
      position:absolute;
      left:0;
      top:0;
      transition: 0.2s;
      transition-timing-function: ease;
      transition-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
      opacity:0.5;
    }
    
    input {
      padding:10px;
    }
    
    input:focus + span, input:not(:placeholder-shown) + span {
      opacity:1;
      transform: scale(0.75) translateY(-100%) translateX(-30px);
    }
    
    /* For IE Browsers*/
    input:focus + span, input:not(:-ms-input-placeholder) + span {
      opacity:1;
      transform: scale(0.75) translateY(-100%) translateX(-30px);
    }

提交回复
热议问题