Basj Basj - 6 months ago 78
Javascript Question

Edit cursor not displayed on Chrome in contenteditable

When you open this page (see Live demo) with Chrome :

<span id="myspan" contenteditable=true></span>


CSS :

#myspan { border: 0; outline: 0;}


JS :

$(myspan).focus();


the
contenteditable
span
has focus (you can start to write things and you will see that it already had focus), but we don't see the "
I
" edit cursor.

How to make that this cursor is displayed ? (Remark :
outline:0
is needed, as well as the fact that the span is empty even with no white space).

Note : With Firefox, the cursor is displayed.

Answer

The solution was to change <span> to <div> (I've seen that this solves many contenteditable problems in other questions here and there) + to add a min-width.


Indeed, with the following code, the size of the <div> would be 0px x 18px ! That explains why the caret (edit cursor) would be hidden !

HTML

<div id="blah" contenteditable=true></div>

CSS

#blah {
    outline: 0;
    position: absolute;
    top:10px;
    left:10px;
}

JS

$("#blah").focus();

Then, adding

min-width: 2px;

in the CSS will allow the caret to be displayed, even with Chrome : http://jsfiddle.net/38e9mkf4/2/

Comments