CSS 实现 textArea 的 placeholder 换行
原文:http://segmentfault.com/a/1190000000362621
目前还没有实验过,只是突然想到,mark一下。
textArea的placeholder不能换行。例如:
<textarea placeholder="第1行 \n 第2行 <br> 第3行 \A 第4行
第5行"></textarea>
这是不会起作用的,会原封不动地输出。
官方不认为这是一个bug:
The placeholder attribute represents a short hint (a word or short phrase)
For a longer hint or other advisory text, the title attribute is more appropriate.
意思就是说placeholder表示的是一个简单的提示(一个词或者一个短语),根本不需要换行。如文本太长,那就用title。
但是实际应用中,我们有时需要换行。如何解决?很多时候我们用JavaScript来解决,其实CSS也可以实现。
由于placeholder属性是可以用css操作的,所以我们可以用:after来把placeholder的内容写到CSS中,曲线救国。
textarea::-webkit-input-placeholder:after{
display:block;
content:"line@ \A line#";/* \A 表示换行 */
color:red;
};
以上是webkit的代码,Firefox类也有相应的版本:
textarea::-moz-placeholder:after{
content:"line@ \A line#";/* \A 表示换行 */
color:red;
};
可以参考stackoverflow上的讨论。
植入部分
如果您觉得文章不错,可以通过赞助支持我。
如果您不希望打赏,也可以通过关闭广告屏蔽插件的形式帮助网站运作。