如何更改CSS输入的占位符?

发布于 2025-02-08 19:28:42 字数 480 浏览 2 评论 0原文

我有一个看起来像这样的形式的输入:< input type =“ text”占位符=“我的占位符” class =“ form-control datatable-global-filter” value =“> 我希望能够更改CSS中的占位符内容,从“我的占位符”到其他东西。我设法通过以下CSS代码来更改颜色和背景以及其他属性,但是我无法更改占位符的内容。我应该放置什么属性,而不是content


::placeholder {
  font-style: italic;
  font-size: 1em;
  color: mintcream;
  background: thistle;
  padding: 5px;
  content : "some other placeholder" !important;
}

I have an input in a form that looks like this: <input type="text" placeholder="My placeholder" class="form-control DataTable--global-filter" value="">
I want to be able to change the placeholder content in css, from "My placeholder" to something else. I managed to change the color and background and the other attributes with the following css snippet, but the content of the placeholder I cannot change. What attribute should I put instead of content ?


::placeholder {
  font-style: italic;
  font-size: 1em;
  color: mintcream;
  background: thistle;
  padding: 5px;
  content : "some other placeholder" !important;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少钕鈤記 2025-02-15 19:28:42

不能更改css中属性的值,但是如果您的输入位于表单中,则可以使用棘手的解决方法,您可以使用包装输入


简而言之:

  • 将输入用&lt; span&gt;元素
  • 重叠span :: pseudoelement 越过输入,并应用您选择的样式:Alterholder伪class
  • 更改的颜色和背景:占位符,因此不再可见
  • pointer-events none span span ::之前,因此每次点击此元素都将被忽略并从基础INPUT元素中截获。
  • 请使用所需的属性
  • 输入 表单 is is :有效时,
span::before,
::placeholder {
  font-family: arial;
  font-style: italic;
  font-size: .75rem;
  padding: 5px;
  line-height: 1;
  box-sizing: border-box;
}

::placeholder {
  color: transparent;
  background: transparent;
}

span {
  position: relative;
}

span::before {
  content : "some other placeholder very long" ;
  position: absolute;
  inset: 0 5px;
  z-index: 1;
  color: mintcream;
  background: thistle;
  width: calc(100% - 10px);
  white-space: nowrap;
  overflow: hidden;
  pointer-events: none;
}

form:valid span::before  {
  display: none;
}
<form>
  <span><input type="text" placeholder="my placeholder" required /></span>
</form>

You can't change the value of an attribute in CSS but you could use a tricky workaround if your input is inside a form and you can wrap the input.


In short:

  • wrap the input with a <span> element
  • overlap the span::before pseudoelement over the input and apply the style you chose for the :placeholder pseudoclass
  • change the color and background of :placeholder so it's not visible anymore
  • set pointer-events to none for span::before so every click to this element will be ignored and intercepted from the underlying input element.
  • use a required attribute for the input
  • when the form is :valid then hide the pseudolement

span::before,
::placeholder {
  font-family: arial;
  font-style: italic;
  font-size: .75rem;
  padding: 5px;
  line-height: 1;
  box-sizing: border-box;
}

::placeholder {
  color: transparent;
  background: transparent;
}

span {
  position: relative;
}

span::before {
  content : "some other placeholder very long" ;
  position: absolute;
  inset: 0 5px;
  z-index: 1;
  color: mintcream;
  background: thistle;
  width: calc(100% - 10px);
  white-space: nowrap;
  overflow: hidden;
  pointer-events: none;
}

form:valid span::before  {
  display: none;
}
<form>
  <span><input type="text" placeholder="my placeholder" required /></span>
</form>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文