我尝试使用此代码来创建自定义文本字段,如 Material Design。我不知道我将如何处理该错误,因为当您尝试在其标签(您的电子邮件)的确切位置单击文本字段时,文本字段不允许我输入。但是,当您尝试在文本字段的标签外单击时,它将允许您键入。我想要做的是,当您单击标签外部或标签的确切位置时,文本字段将允许您键入。
<div class="form__group">
<input type="email" id="email" class="form__field" placeholder="Your Email">
<span for="email" class="span-header form__label">Your Email</span>
</div>
当我试图点击标签(您的电子邮件)时,图片不允许我输入
当我试图在标签外单击时,图片允许我输入
Click outside the label -image
CSS:
.form__group {
position: relative;
padding: 15px 0 0;
margin-top: 10px;
}
.form__field {
font-family: inherit;
width: 100%;
border: 0;
border-bottom: 1px solid #d2d2d2;
outline: 0;
font-size: 16px;
color: #212121;
padding: 7px 0;
background: transparent;
transition: border-color 0.2s;
}
.form__field::-webkit-input-placeholder {
color: transparent;
}
.form__field:-ms-input-placeholder {
color: transparent;
}
.form__field::-ms-input-placeholder {
color: transparent;
}
.form__field::placeholder {
color: transparent;
}
.form__field:placeholder-shown ~ .form__label {
font-size: 16px;
cursor: text;
top: 20px;
}
.span-header,
.form__field:focus ~ .form__label {
position: absolute;
top: 0;
display: block;
transition: 0.2s;
font-size: 12px;
color: #9b9b9b;
}
.form__field:focus ~ .form__label {
color: #009788;
}
.form__field:focus {
padding-bottom: 6px;
border-bottom: 2px solid #009788;
}
最佳答案
使用 <label for="email">
而不是 span
.
The
id
of a labelable form-related element in the same document as the<label>
element. The first element in the document with an id matching the value of thefor
attribute is the labeled control for this label element, if it is a labelable element. If it is not labelable then thefor
attribute has no effect. If there are other elements which also match the id value, later in the document, they are not considered.
.form__group {
position: relative;
padding: 15px 0 0;
margin-top: 10px;
}
.form__field {
font-family: inherit;
width: 100%;
border: 0;
border-bottom: 1px solid #d2d2d2;
outline: 0;
font-size: 16px;
color: #212121;
padding: 7px 0;
background: transparent;
transition: border-color 0.2s;
}
.form__field::-webkit-input-placeholder {
color: transparent;
}
.form__field:-ms-input-placeholder {
color: transparent;
}
.form__field::-ms-input-placeholder {
color: transparent;
}
.form__field::placeholder {
color: transparent;
}
.form__field:placeholder-shown~.form__label {
font-size: 16px;
cursor: text;
top: 20px;
}
.span-header,
.form__field:focus~.form__label {
position: absolute;
top: 0;
display: block;
transition: 0.2s;
font-size: 12px;
color: #9b9b9b;
}
.form__field:focus~.form__label {
color: #009788;
}
.form__field:focus {
padding-bottom: 6px;
border-bottom: 2px solid #009788;
}
<div class="form__group">
<input type="email" id="email" class="form__field" placeholder="Your Email">
<label for="email" for="email" class="span-header form__label">Your Email</label>
</div>
关于html - 定制 Material 设计表格(前端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55390522/