gpt4 book ai didi

html - 当输入类型设置为电子邮件时,带有 CSS 的 float 标签不起作用

转载 作者:行者123 更新时间:2023-11-28 19:26:22 26 4
gpt4 key购买 nike

当我们输入标签 float 到顶部的内容时,我有一个输入文本框。当输入类型为“文本”时它工作正常但如果输入文本框设置为类型“电子邮件”它停止工作,我需要一个解决方案来让它工作。

.relPos {
position: relative;
}

.upLabel {
position: absolute;
top: 0px;
left: 0;
transition: .3s;
pointer-events: none;
}

.upInputs input {
box-shadow: none;
}

.upInputs input:focus~.upLabel,
.upInputs input:valid~.upLabel {
top: -15px;
border: none;
}
<br>
<div class="relPos upInputs">
<input type="text" required>
<label class="upLabel">Type="Text"</label>
</div>
<br>
<div class="relPos upInputs">
<input type="email" required>
<label class="upLabel">Type="Email"</label>
</div>

<br>

最佳答案

:valid 只会在输入有效时触发,因此您需要一封电子邮件 (xxx@yyy.zz) 才能正常工作。相反,您可以考虑使用 :placeholder-shown ref 并有一个空的占位符:

.relPos {
position: relative;
}

.upLabel {
position: absolute;
top: 0px;
left: 0;
transition: .3s;
pointer-events: none;
}

.upInputs input {
box-shadow: none;
}

.upInputs input:focus~.upLabel,
.upInputs input:not(:placeholder-shown)~.upLabel {
top: -15px;
border: none;
}
<br>
<div class="relPos upInputs">
<input type="text" required placeholder=" ">
<label class="upLabel">Type="Text"</label>
</div>
<br>
<div class="relPos upInputs">
<input type="email" required placeholder=" ">
<label class="upLabel">Type="Email"</label>
</div>

<br>

:valid来说明问题

.relPos {
position: relative;
}

.upLabel {
position: absolute;
top: 0px;
left: 0;
transition: .3s;
pointer-events: none;
}

.upInputs input {
box-shadow: none;
}

.upInputs input:valid~.upLabel {
top: -15px;
border: none;
}
<div class="relPos upInputs">
<input type="email" required value="not an email">
<label class="upLabel">Type="Email"</label>
</div>
<br>
<div class="relPos upInputs">
<input type="email" required value="example@email.com">
<label class="upLabel">Type="Email"</label>
</div>

关于html - 当输入类型设置为电子邮件时,带有 CSS 的 float 标签不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56089271/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com