我希望当用户来try 在"输入"中输入文本时,标签会上升,如果他们将该区域留空,标签会返回到原始位置.如果用户在那里输入了什么,它就会留在那里.
我用输入框try 了这段代码,要求它可以正常工作,但当我try 用输入框做这件事,并在输入框中添加一些内容时,标签需要保持在上面.
div{
margin-top:40px;
}
.btn-add input[type="text"]{
width: 90%;
padding: 10px 20px 0 20px;
border: none;
border-bottom:1px solid #999;
font-size: 140%;
color: #000;
}
.btn-add input:focus ~ .floating-label{
top: -20px;
bottom: 10px;
left: 10px;
font-size: 20px;
opacity: 1;
color: rgb(100, 6, 6);
}
.floating-label {
position: absolute;
pointer-events: none;
left: 20px;
top:10px;
transition: 0.2s ease all;
color: #999999;
font-size: 120%;
}
.form-float{
position: relative;
}
<div class="form-float btn-add">
<input type="text" class="inputText" placeholder="" >
<label class="floating-label" >Button text</label>
</div>
我删除了input:not(:focus):valid~.floating-label
,这样它就可以处理非必填字段