<div class="form-item">
<input required id="username" type="text">
<span class="bar"></span>
<label for="username">用户名</label>
</div>
.form-item {
margin: 100px auto;
position: relative;
width: 300px;
text-align: center;
}
input {
width: 100%;
padding: 10px 0;
font-size: 16px;
border: none;
border-bottom: 1px solid #ccc;
background: transparent;
outline: none;
transition: border-color 0.3s ease;
}
label {
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
pointer-events: none;
transition: all 0.3s ease;
color: #999;
}
.bar {
position: absolute;
bottom: -1px;
height: 2px;
width: 0;
left: 50%;
transform: translateX(-50%);
background: #0a53be;
transition: 0.4s ease;
}
input:focus ~ .bar{
width: 100%;
}
input:focus ~ label{
color: #0a53be;
transform: translateY(-40px);
font-size: 18px;
}
input:valid ~ label{
color: #0a53be;
transform: translateY(-40px);
font-size: 18px;
}
正文完