This question already has answers here:
How to set caret(cursor) position in contenteditable element (div)?
                                
                                    (9个答案)
                                
                        
                                在11个月前关闭。
            
                    
我在几个DIV上使用Display flex。我将内部DIV文本内容垂直居中。但是,当我在没有内容的内容中单击内容可编辑内部DIV时,它会正确居中,我可以看到插入符号从左上角(对于Chrome)或左下角(对于FF)开始。当我开始键入插入符号时,内容会跳到中心。只要有内容,插入号就会居中,但是当没有内容时,插入号将从顶部(或底部)开始。是什么赋予了?

CodePen Example

的HTML

<div id="input_area">
  <div id="inner" class="content" contenteditable="true" data-hint="Click Here and Type"></div>
</div>


的CSS

#input_area
{
    background-color: yellow;
    display: flex;
    overflow-y:auto;
    max-height:300px;
    min-height:4em;
    cursor:auto;
}

#input_area > div.content
{
    width: 100%;
    display:flex;
    align-items:center;
}

#input_area .content[contentEditable=true]:empty:not(:focus):before
{
    content: attr(data-hint);
    color: #aaaaaa;
}

最佳答案

Try this

#input_area > div.content {
width: 100%;
display:flex;
align-items:center;
margin:auto;
outline:none
}

10-04 15:23
查看更多