How do you make Placeholder and Label Transitions?

后端 未结 2 1535
迷失自我
迷失自我 2020-12-10 09:59

I am looking to find an example of how to make a label / placeholder transition move up and out of placeholder position into a label position and vice versa..

Exam

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 10:17

    I found a good Codepen showing an example of how to do it in CSS.

    HTML:

    CSS:

    .row {
      position: relative;
      margin-top: 35px;
    }
    
    input {
        display: block;
        padding: 8px 12px;
        width: 100%;
        max-width: 300px;
        border-radius: 5px;
        border: 0;
    }
    
    label {
        position: absolute;
        font-weight: 600;
        color: #777777;
        top: 50%;
        left: 12px;
        transform: translateY(-50%);
        -ms-transform: translateY(-50%);
        -webkit-transform: translateY(-50%);
        cursor: text;
        user-select: none;
        transition: 0.15s ease-in-out;
    }
    
    input[data-empty="false"] + label,
    input:valid + label,
    input:focus + label {
        top: -10px;
        left: 0px;
        font-size: 10px;
        color: #ffffff;
    }
    

    Example: https://codepen.io/sivan/pen/alKwf

提交回复
热议问题