Placeholder in contenteditable - focus event issue

后端 未结 11 2108
无人共我
无人共我 2020-12-07 10:05

I have been trying to ask this before, without any luck of explaining/proving a working example where the bug happens. So here is another try:

I’m trying to replicat

11条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-07 10:45

    I found that the best way to do this is to use the placeholder attribute like usual and add a few lines of CSS.

    HTML

    CSS

    [contenteditable][placeholder]:empty:before {
        content: attr(placeholder);
        color: #bababa;
    }
    

    Note: the CSS :empty selector only works if there is literally nothing in-between the opening and closing tag. This includes new lines, tabs, empty space, etc.

    Codepen

提交回复
热议问题