html - 如何在焦点和打字时将占位符移至顶部

标签 html css reactjs

我正在尝试实现一个简单的输入框,当我单击该框时,占位符值会移动到顶部,但有一个问题,它总是显示在顶部。

这是代码
https://codesandbox.io/s/magical-glitter-7ykhm?file=/src/styles.css

最佳答案

另一种方法是创建您自己的自定义占位符(标签),这更容易使用 css 进行操作。

body {
  background: #111;
}

.input-wrap {
  position: relative;
  margin: 40px;
}

.input-wrap input {
  background: none;
  color: #c6c6c6;
  font-size: 18px;
  padding: 10px;
  display: block;
  width: 320px;
  border: none;
  border-bottom: 1px solid #ccc;
}

.input-wrap label {
  position: absolute;
  color: #c6c6c6;
  font-size: 16px;
  font-weight: normal;
  pointer-events: none;
  left: 10px;
  top: 10px;
  transition: 300ms ease all;
}

input:focus {
  outline: none;
}

.input-wrap input:focus~label,
.input-wrap input:valid ~ label{
  top: -14px;
  font-size: 12px;
  color: #328dd2;
}
<div class="input-wrap">
  <input required/>
  <label>Name</label>
</div>

关于html - 如何在焦点和打字时将占位符移至顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70952379/

相关文章:

javascript - 在其外部单击关闭 DIV 弹出窗口

javascript - jQuery - 隐藏具有 span 元素且内部包含某些文本的 DIV

javascript - 在大量对象中改变单个对象属性的最快方法是什么?

reactjs - 样式化的组件会导致不需要的重新渲染

javascript - 带有汉堡图标的响应式侧面导航菜单

html - 如何限制内容宽度

javascript - 甚至在调用 document.ready 函数之前就存在 jQuery 表单输入字段

html - <h>标签在不同的页面有不同的颜色

node.js - express 发帖请求给出 ERR_EMPTY_RESPONSE

html - CSS 绝对位置子菜单在父容器上显示滚动条