我有两个带浮动占位符的输入字段。在焦点上,占位符向上移动并为用户创建输入空间。但是一旦用户开始输入它就会消失。我希望占位符在文本框中键入内容后保持在同一位置。这只能使用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>
答案 0 :(得分:2)
如评论所述,您应该使用label
。 也正确连接到其输入以保持连贯。
https://www.w3.org/wiki/HTML/Elements/label
<label>
元素表示用户界面中的标题。
- HTML属性 for = string
指定用于指示与标题关联的表单控件。 属性的值必须是与label元素在同一Document中的可标记表单关联元素的ID。
示例:
<input type="checkbox" id="lost" name="lost"> <label for="lost">Lost</label>
从那里开始,在输入后设置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>
对于infos::valid/:invalid
是CSS选择器级别4,仍处于草稿状态https://drafts.csswg.org/selectors-4/#validity-pseudos但尚未完成http://caniuse.com/#search=%3Avalid