html - 定制 Material 设计表格(前端)

标签 html css

我尝试使用此代码来创建自定义文本字段,如 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 on the label -image

当我试图在标签外单击时,图片允许我输入

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 the for attribute is the labeled control for this label element, if it is a labelable element. If it is not labelable then the for 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/

相关文章:

javascript - 是 tabIndex : 0 in links considered a good practice in order to avoid href ="javascript: void(0);"?

当要替换两个以上相同的单词时,Javascript .replace 不适用于 .match 单词

php - 随机样式标 checkout 现在简单的 php 模板网站中?

html - 增加 DOM 元素或改用 http 请求

html - 如何并排放置按钮?

android - 单击以更改图像在 Android 2.2 应用程序中不起作用

javascript - Modernizr 与 HTML Shiv

html - 将导航栏从内联列表转换为移动版网站的 block 类型列表 - 缺少元素符号

html - 字体不会改变,id 高度不会显示

javascript - 将 CSS 分配给 <div> 元素