【问题标题】:Change text that is place holder in html via CSS通过 CSS 更改 html 中占位符的文本
【发布时间】:2021-07-16 20:51:15
【问题描述】:

我在一个网站上有以下信息,我无法编辑 html,但我可以编辑 CSS。

<textarea id="gui-form-comment" name="comment" placeholder="Comment" style="width:100%;" data-validate-before-update="false"></textarea>

我想将占位符更改为其他内容。

【问题讨论】:

标签: html css


【解决方案1】:

从技术上讲这是不可能的,但您可以模仿现代浏览器中的行为。但是您需要一个用于 textarea 父元素的选择器,并且您可能需要调整 #container::before 伪占位符的字体、大小和位置以匹配您的 textarea 的格式。

/* Hide real placeholder */
#container textarea::placeholder {
    color: transparent;
}

/* Add pseudo-placeholder */
#container::before {
    content: 'New placeholder';
    position: absolute;
    font-family: monospace;
    padding: 4px;
    pointer-events: none;
}

/* Show and hide the pseudo-placeholder */
#container textarea {
    position: relative;
}
#container textarea:placeholder-shown {
    position: static;
}
<div id="container">
  <textarea id="gui-form-comment" name="comment" placeholder="Comment" style="width:100%;" data-validate-before-update="false"></textarea>
</div>

【讨论】:

    【解决方案2】:

    因此,如果您可以指定 textarea 的高度,那么它会很容易实现:

    <textarea
      id="gui-form-comment"
      name="comment"
      placeholder="Comment"
      style="width: 100%; height: 100px;"
      data-validate-before-update="false"
    ></textarea>
    <style>
      #gui-form-comment::placeholder {
        line-height: 100px;
      }
    </style>
    

    ::placeholder 实际上可以安全使用 (https://caniuse.com/?search=%3A%3Aplaceholder)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 2020-05-12
      • 1970-01-01
      • 2017-03-26
      • 2020-11-28
      相关资源
      最近更新 更多