How do you make Placeholder and Label Transitions?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 16:41:13

General sibling selectors & :focus does the trick in a very simple way ;)

input{
 position:absolute;
 top:20px;
}
input ~ span{
 transition:top .7s ease;
 position:absolute;
 top:20px;
}
input:focus ~ span{
 top:0px;
}
<label>
<input>
<span>Text</span>
</label>

here is an example with multiple fields

https://jsfiddle.net/shLe3107/1/

hope this is enough else just ask

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

HTML:

<div class="row">
    <input id="name" type="text" name="name">
    <label for="name">Full Name</label>
</div>

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!