Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering the placeholder caret in contentEditable element

I have the following: http://jsfiddle.net/mb76koj3/.

[contentEditable=true]:empty::before{
  content:attr(data-ph);
  color: #CCC;
  text-align: center;
}

h1 {
  text-align: center;
}
<h1 contenteditable="true" data-ph="Name"></h1>

The problem is the <h1> is centered, but the placeholder caret isn't centered (until you start typing). The caret is on the left side, how can I change this?

like image 477
Muhambi Avatar asked Mar 26 '15 21:03

Muhambi


People also ask

How do I center align an element?

Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How to set cursor position in textBox in HTML?

value + "\n"; ele. focus(); // To update cursor position to recently added character in textBox ele. setSelectionRange(value. length, value.

How do I center an item in JavaScript?

To center text in JavaScript, do this: element. style. textAlign = 'center' .


1 Answers

Not an ideal solution but we can fake the effect by applying a padding-left of 50% to the :empty element to make the cursor appear at the middle.

And then align the placeholder (the pseudo-element) at the middle by using absolute positioning and a negative value of translateX transform function.

* {
  margin: 0;
  padding: 0;
}

@-moz-document url-prefix() { /* CSS Hack to fix the position of cursor on FF */
  [contentEditable=true]:empty {
    padding-top: 1em;
    padding-bottom: .25em;
    -moz-box-sizing: content-box;
  }
}

[contentEditable=true]:empty {
  padding-left: 50%;
  text-align: left; /* Fix the issue on IE */
}

[contentEditable=true]:empty::before{
  content:attr(data-ph);
  color: #CCC;
  position: absolute;
  top: 0;
  left: 50%;
  -webkit-transform: translateX(-50%);
  -moz-transform:  translateX(-50%);
  -ms-transform:  translateX(-50%);
  -o-transform:  translateX(-50%);
  transform:  translateX(-50%);
}

h1 {
  text-align: center;
  position: relative;
}
<h1 contenteditable="true" data-ph="Name"></h1>

The only problem that remains — as can be seen in the OP's provided code — is that on Firefox, an empty <br> element is appended to contentEditable elements which causes :empty pseudo-class not matching the element anymore. Hence the placeholder won't be restored.

However since the question is about the position of the cursor, I assume this behavior is just fine for the OP.

like image 75
Hashem Qolami Avatar answered Oct 13 '22 19:10

Hashem Qolami