html - css - 如何更改输入占位符的位置

标签 html css

<分区>

我想将输入占位符放在输入边框上。

enter image description here

我设法改变了颜色,但没有改变位置。

.filter::placeholder {
   color: gray;
}
<input type="text" class="filter">

有办法吗?

最佳答案

你不能通过设置 ::placeholder 的样式来做到这一点,它只支持有限数量的样式:https://css-tricks.com/almanac/selectors/p/placeholder/#article-header-id-4

不过,您可以添加一个 span 来实现类似的效果。

.filter {
  position: relative;
  width: 195px;
  height: 30px;
  border-radius: 5px;
  outline: none;
  padding: 5px;
  border: 1px solid lightblue;
}

.placeholder {
  position: absolute;
  left: 20px;
  top: 0px;
  background-color: white;
  padding: 0 20px;
}

.filter:focus ~ .placeholder,
.filter:valid ~ .placeholder {
  display: none;
}
<input required type="text" class="filter" />
<span class="placeholder">First Name..</span>

关于html - css - 如何更改输入占位符的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48260921/

上一篇:html - 始终将工具提示定位在右下角

下一篇:html - 当我的宽度设置为 100% 时,为什么我的网站会横向滚动?

相关文章:

Javascript 工具提示跨浏览器兼容性

html - 使HTML背景视频高度变小宽度100%

javascript - 由于 td 宽度的变化,点击不起作用

html - 从 DOM 的特定元素选择性地复制 HTML+CSS+JS 的工具

javascript - 尽管我添加了高度 : 100%?,为什么我的 div 没有扩展到页面的全长

javascript - 从网站构建 html/css/javascript 代码以提高可读性

html - 将两个全尺寸的 div 排列在彼此下方

jquery - 如何将类添加到具有特定数据 ID 的跨度

javascript - 如何在不使用位置属性的情况下重叠表格(td)文本

html - Chrome开发者窗口能否同时覆盖css规则?