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?
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.
value + "\n"; ele. focus(); // To update cursor position to recently added character in textBox ele. setSelectionRange(value. length, value.
To center text in JavaScript, do this: element. style. textAlign = 'center' .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With