简体   繁体   English

Css :: textarea 上的第一个字母不起作用

[英]Css ::first-letter on textarea does not work

I want to make every first letter typed in a textarea be uppercase.我想让 textarea 中输入的每个第一个字母都大写。 But when I try this code, it doesn't work:但是当我尝试这段代码时,它不起作用:

HTML HTML

<textarea name="content" rows="18" placeholder="Your Message"
          title="Give Your Advice To Us" wrap="soft"></textarea>

CSS CSS

textarea::first-letter {
  text-transform: uppercase;
}

How can I fix this?我怎样才能解决这个问题?

A textarea cannot have a ::first-letter pseudo-element. textarea 不能有::first-letter伪元素。 Only a block container can have such a pseudo-element, but due to its nature a textarea cannot be a block container and therefore you can't emulate this behavior with CSS.只有块容器可以有这样的伪元素,但由于它的性质,textarea 不能是块容器,因此你不能用 CSS 模拟这种行为。

Applying text transforms to form fields is generally never a good idea anyway.无论如何,将文本转换应用于表单字段通常从来都不是一个好主意。 You're only changing the appearance of the text input — the text that actually gets submitted isn't going to have the transform applied to it.您只是在更改文本输入的外观 — 实际提交的文本不会对其应用转换。 If you really want the text to always start with a capital letter, validate it on the server side, or just enforce it with JavaScript.如果您确实希望文本始终以大写字母开头,请在服务器端对其进行验证,或者仅使用 JavaScript 强制执行。 How you do that is outside the scope of this question since it depends entirely on your use case but I'm sure you can find another question that meets your needs.你如何做到这一点超出了这个问题的范围,因为它完全取决于你的用例,但我相信你可以找到另一个满足你需求的问题。

You need a block container in order to use the ::first-letter pseudo-element:您需要一个块容器才能使用::first-letter伪元素:

<p>
    <textarea name="content" rows="18" placeholder="Your Message"
              title="Give Your Advice To Us" wrap="soft"></textarea>
</p>

And your CSS:还有你的 CSS:

p::first-letter {
  text-transform: uppercase;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM