我有一个带占位符的文本输入。单击输入后,占位符会向上移动并保持为标题。问题是,当我开始输入时,标题就会消失。
.material-input-login {
margin: 8px 10px;
width: 200px;
/* display: block; */
border: none;
padding: 10px 0;
border-bottom: solid 1px #fff;
-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) 96%, #fff 4%);
background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 96%, #fff 4%);
background-position: -200px 0;
background-size: 200px 100%;
background-repeat: no-repeat;
color: #fff;
}
.material-input-login:focus, .material-input-login:valid {
box-shadow: none;
outline: none;
background-position: 0 0;
}
.material-input-login:focus::-webkit-input-placeholder, .material-input-login:valid::-webkit-input-placeholder {
color: #fff;
font-size: 11px;
-webkit-transform: translateY(-20px);
transform: translateY(-20px);
visibility: visible !important;
}
.material-input-login::-webkit-input-placeholder, .material-button {
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}

<input id="searchBox" type="search" placeholder="Search" class="material-input-login" required="">
&#13;
答案 0 :(得分:2)
问题是当您开始输入时占位符会消失 - 我不知道任何可以避免此行为的CSS技巧。
我已经开始尝试使用输入标签代替:
https://jsfiddle.net/zug5j8m5/
CSS:
body {
background: #333;
}
.material-input-login {
margin: 0 10px 8px;
width: 200px;
border: none;
padding: 10px 0;
border-bottom: solid 1px #fff;
-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) 96%, #fff 4%);
background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 96%, #fff 4%);
background-position: -200px 0;
background-size: 200px 100%;
background-repeat: no-repeat;
color: #fff;
}
.material-input-login:focus, .material-input-login:valid {
box-shadow: none;
outline: none;
background-position: 0 0;
}
.label {
color: #fff;
display: block;
font-family: sans-serif;
font-size: 13px;
margin: 0 10px;
position: absolute;
-webkit-transform: translateY(-35px);
transform: translateY(-35px);
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
.material-input-login:focus+.label {
color: #fff;
font-size: 11px;
-webkit-transform: translateY(-50px);
transform: translateY(-50px);
display:block !important;
}
HTML:
<input id="searchBox" type="search" title="Search" class="material-input-login" required="">
<label for="searchBox" class="label">Search</label>
答案 1 :(得分:-1)
这是让它向上移动的原因:
-webkit-transform: translateY(-20px);
transform: translateY(-20px);
translateY允许您移动占位符-20px以上。只需将其删除或更改数字即可。