css - 输入标题消失

标签 css html input

我有一个带占位符的文本输入。一旦您单击输入,占位符就会向上移动并保留为标题。问题是当我开始输入时,标题消失了。

.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="">

最佳答案

问题是占位符会在您开始输入时消失——我不知道有任何 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>

关于css - 输入标题消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43219015/

相关文章:

jquery - 如何在语义上将单个列表分成多列?

jquery - CSS位置问题

javascript - 当我尝试保存提交表单中的值时,本地存储给出未定义的结果

javascript - 用 CSS 生成的图像替换输入字段中的文本

html - 无法滚动到容器溢出的 flex 元素顶部

jQuery 动画高度 100% - #px

css - Ruby Rails 试图在与链接相同的行上显示标题 (h1)

html - 单击按钮显示蓝色矩形。此外,按钮在关闭模式后取消悬停

python-3.x - 从另一个函数获取数字输入的函数,如果数字为负,则应该给出错误

javascript - getElementsByClassName 更改输入值