I want to center align textarea with placeholder. so I am using following code
<textarea style="text-align: center" placeholder="Type Here..."></textarea>
But issue is with design. I want to start cursor from starting position of placeholder something like this as like instagram story
but with text-align center it looks like this
I tried different CSS option like padding
and setSelectionRange
but not working
2
Answers
I think that its not directly possible using just the textarea and placeholder and way you can try is by adding the textarea in a div
You can further style the element as per your need
You can achieve this with java-script included. Here is a solution:
Html:
Css:
Java-script:
The Html structure is wrapped in a container div with
.centered-textarea
. This container will ensure that thetextarea
is centered within its parent container, the Css makes sure that thetextarea
class will be centered horizontally & takes up the full width of it’s container, and the Java-script code adjusts the cursor position to the beginning of thetextarea
whenever thetextarea
receives input and gets the length of the placeholder text and sets the cursor position accordingly.Hope this helps 🙂