保持输入占位符始终可见

时间:2016-07-14 05:51:32

标签: html css html5

我试图在我的网站中设置所有输入的样式,当他们有一个值或有焦点时,palceholder成为输入的标题(它移动到输入的顶部)但是默认行为当有数据时,浏览器会隐藏占位符。

我真的想远离JS解决方案。

目前的结果:

enter image description here enter image description here

我的css:

input:focus::-webkit-input-placeholder {
    transform: translate(-1rem,calc(-100% - 0.8rem));
    font-size: 0.9rem !important;
    opacity: 1 !important; //this doesnt seem to keep the placeholder visible
    display:block !important; //this doesnt seem to keep the placeholder visible
}

input::-webkit-input-placeholder{
    display:block !important; //this doesnt seem to keep the placeholder 
    opacity: 1 !important;  //this doesnt seem to keep the placeholder 
  } 

2 个答案:

答案 0 :(得分:8)

只能使用占位符来完成。这是样本。



body {
  padding: 25px 10px
}
* {
  margin: 0
}
.fieldOuter {
  position: relative;
  margin: 0 0 30px 0;
  font-family: impact;
  font-size: 16px
}
.fieldOuter input {
  padding: 10px;
  width: 250px;
  transition: all 1s;
  border: 2px solid #999;
  font-size: 17px;
  color: #666
}
.fieldOuter label {
  position: absolute;
  left:0px;
  top: 0;
  line-height:15px;
  transition: all 0.5s;
  overflow: hidden;
  color: #999;
  white-space: nowrap;
  z-index: 1;
  opacity: 0;
}
.fieldOuter input:focus + label {
  opacity: 1;
  top: -18px;  
}
.fieldOuter input:focus {
  outline: none;
  border-color: rgba(82, 168, 236, 0.8);
}

<div class="fieldOuter">
<input id="Name" placeholder="Name" type="text" />
  <label for="Name">Name</label>
</div>
<div class="fieldOuter">
<input id="LastName" placeholder="Last Name" type="text" />
  <label for="LastName">Last Name</label>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你可以尝试这段代码。

.git/config
body {
  padding: 25px 10px
}
* {
  margin: 0
}
.fieldOuter {
  position: relative;
  margin: 0 0 30px 0;
  font-size: 16px
}
.fieldOuter input {
  padding: 10px;
  width: 250px;
  transition: all 1s;
  border: 2px solid red;
  font-size: 17px;
  color: #666
}
.fieldOuter label {
  position: absolute;
  left:0px;
  top: 0;
  line-height:15px;
  transition: all 0.5s;
  overflow: hidden;
  color: red;
  white-space: nowrap;
  z-index: 1;
  opacity: 0;
}
.fieldOuter input:focus + label {
  opacity: 1;
  top: -18px;  
}
.fieldOuter input:focus {
  outline: none;
  border-color: rgba(82, 168, 236, 0.8);
}