【问题标题】:Retain the placeholder after typing键入后保留占位符
【发布时间】:2018-03-06 18:56:13
【问题描述】:

我有 2 个带有浮动占位符的输入字段。在焦点上,占位符向上移动并为用户键入创建空间。但是一旦用户开始输入,它就会消失。在文本框中输入内容后,我希望占位符保持在同一个位置。仅使用 CSS 可以做到这一点吗?

input {
  width: 100%;
  display: block;
  border: none;
  padding: 20px 0 10px 0;
  border-bottom: solid 1px #343a40;
  -webkit-transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
  transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
  background: -webkit-linear-gradient(top, rgba(255, 255, 255, 0) 99%, #007bff 1%);
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 99%, #007bff 1%);
  background-position: -1000px 0;
  background-size: auto 100%;
  background-repeat: no-repeat;
  color: #000;
}

input:focus,
input:valid {
  box-shadow: none;
  outline: none;
  background-position: 0 0;
}

input::-webkit-input-placeholder {
  font-family: 'roboto', sans-serif;
  -webkit-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}

input:focus::-webkit-input-placeholder,
input:valid::-webkit-input-placeholder {
  color: #007bff;
  font-size: 12px;
  -webkit-transform: translateY(-20px);
  transform: translateY(-20px);
  visibility: visible !important;
}
<input placeholder="Username" type="text" required>
<input placeholder="Password" type="password" required>

【问题讨论】:

  • 使用标签而不是占位符并设置标签样式
  • 不是真的,因为一旦输入有任何内容,占位符就不会再显示了。如果您使用标签而不是占位符 - placeholders are considered harmful anyway,您也许可以实现这一点。但是,公平地说 - so is the floating label pattern。因此,如果可访问性是您的重点,而不仅仅是设计师的虚荣心……那么您也许不应该使用其中任何一个。
  • 这真的很有意义.. 谢谢@CBroe

标签: html css placeholder floating


【解决方案1】:

如评论所述,您应该使用label还正确连接到其输入以保持一致

https://www.w3.org/wiki/HTML/Elements/label

&lt;label&gt; 元素表示用户界面中的标题。

  • HTML 属性 for = string

指定为与标题关联的表单控件。 该属性的值必须是与标签元素在同一文档中的可标记表单关联元素的 ID。

示例:&lt;input type="checkbox" id="lost" name="lost"&gt; &lt;label for="lost"&gt;Lost&lt;/label&gt;

从那里开始,在输入之后设置label,它将可以通过+ 选择器设置样式。

CSS 示例

label {
  position: absolute;
  margin-top: -1.75em;/*climbs under the input */
  transition: all 0.3s ease-in-out;
}
input {
  position: relative;
  z-index: 1;/* set input on top of label. opaque white bg color can be used to lighten label's color */
  width: 100%;
  display: block;
  border: none;
  padding: 20px 0 10px 0;
  border-bottom: solid 1px #343a40;
  transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
  transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
  background: linear-gradient(
    to top,
    rgba(255, 255, 255, 0) 99%,
    #007bff 1%
  );
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 99%, #007bff 1%)
    rgba(255, 255, 255, 0.4);
  background-position: -1000px 0;
  background-size: auto 100%;
  background-repeat: no-repeat;
  color: transparent;/* hide value if any when label is standing here too */
}
input:focus {
  color: #000;
}
input:valid + label {
  color: #06a31b;
  z-index: 1;/* let's show this field is filled */
}
input:focus + label, input:active + label {
  font-size: 12px;
  color: #007bff;
  font-size: 12px;
  transform: translateY(-2em);/* move it up more */
}

input:focus,
input:valid {
  box-shadow: none;
  outline: none;
  background-position: 0 0;
}
<input placeholder="" id="User" type="text" required><label for="User">Username</label>
<input id="pwd" placeholder="" type="password" required><label for="pwd">Password</label>

其他 CSS 示例,一旦字段填充,标签就位于顶部

label {
  position: absolute;
  margin-top: -1.75em;/*climbs under the input */
  transition: all 0.3s ease-in-out;
}
input {
  position: relative;
  z-index: 1;/* set input on top of label. opaque white bg color can be used to lighten label's color */
  width: 100%;
  display: block;
  border: none;
  padding: 20px 0 10px 0;
  border-bottom: solid 1px #343a40;
 transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
  transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);
  background:linear-gradient(
    to top,
    rgba(255, 255, 255, 0) 99%,
    #007bff 1%
  );
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 99%, #007bff 1%)
    rgba(255, 255, 255, 0.4);
  background-position: -1000px 0;
  background-size: auto 100%;
  background-repeat: no-repeat;
}
input:focus {
  color: #000;
}
input:valid + label,
input:focus + label, input:active + label {
  font-size: 12px;
  color: #007bff;
  font-size: 12px;
  transform: translateY(-2em);/* move it up more */
}

input:focus,
input:valid {
  box-shadow: none;
  outline: none;
  background-position: 0 0;
}
<input placeholder="" id="User" type="text" required><label for="User">Username</label>
<input id="pwd" placeholder="" type="password" required><label for="pwd">Password</label>

关于信息::valid/:invalid 是 CSS 选择器级别 4,仍处于草稿状态 https://drafts.csswg.org/selectors-4/#validity-pseudos 但尚未得到很好的实施 http://caniuse.com/#search=%3Avalid

【讨论】:

    猜你喜欢
    • 2014-12-07
    • 2020-12-09
    • 2013-05-02
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多