css - 保持输入占位符始终可见

标签 css html

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

我真的想远离 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 
  } 

最佳答案

仅使用占位符无法完成。这是示例。

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>

关于css - 保持输入占位符始终可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38366411/

相关文章:

css - 基于 css 媒体查询的设备宽度

html - 如何在内部没有额外标记的情况下创建顶部和底部填充的可滚动 div?

javascript - 使用 Javascript 仅隐藏边框的一侧

html - 边框不会在 Internet Explorer 中显示

html - 如何根据屏幕尺寸缩放 div 中的背景图像

javascript - 二维网格如何找到给定节点号的索引

html - 三里等宽

html - 在没有 SPD 的情况下在 Sharepoint 母版页中使用 Bootstrap

javascript - jQuery load() 函数 - 无法从动态加载的标题内容中获取高度

javascript - 溢出 :hidden is not correctly hiding absolutely positioned elements