【发布时间】: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