我有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>
使用标签,而不是一个占位符和风格标签 –
不是真的,因为一旦输入有任何内容,占位符不显示了。如果您使用标签而不是占位符,则可以实现此目的 - [占位符无论如何都被认为是有害的](https://www.nngroup.com/articles/form-design-placeholders/)。但是,公平地说 - 浮动标签模式也是如此(https://medium.com/simple-human/floating-labels-are-a-bad-idea-82edb64220f6)。所以,如果可用性是你关注的焦点,而不仅仅是设计师的虚荣心......那么你也许不应该使用其中的任何一个。 – CBroe
这真的有道理..谢谢@CBroe – Venky