Middle (vertically) align text inside a textarea
Back in the days when i was asking this, i tried achieving this with textarea which is really not possible without scripting. The answer was adding HTML5's contenteditable="true" on a table cell with style="vertical-align: middle". If one's project allows modern HTML, this is a rather simple and effective solution.
If instead of adding <table>
and <tr>
and <td>
to your markup, you would like something regular like a <div>
, you would still need two of them:
<div style="display: table">
<div
style="display: table-cell; vertical-align: middle"
contenteditable="true">
I am vertically aligned
</div>
</div>
How do I vertically center the cursor in this text area?
var textInput = document.getElementById('textInput');
var textDuplicate = document.getElementById('textDuplicate');
textInput.addEventListener('keyup', function(e) {
textDuplicate.innerHTML = textInput.value;
});
#img-wrapper {
width: 400px;
height: 350px;
background-image: url('https://images.unsplash.com/photo-1553095066-5014bc7b7f2d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8d2FsbCUyMGJhY2tncm91bmR8ZW58MHx8MHx8&w=1000&q=80');
background-size: cover;
background-repeat: no-repeat;
background-position: center;
display: flex;
align-items: center;
justify-content: center;
}
#textDuplicate {
color: #fff;
text-align: center;
padding: 10px;
}
<textarea id="textInput"></textarea>
<div id="img-wrapper">
<span id="textDuplicate"></span>
</div>
Related Topics
Allow Certain Style Attributes with Ngsanitize
How to Make Multiple Read More Buttons in Same Page Using One Jquery
Android - Parse Js Generated Urls with Jsoup
Disable Vertical Bounce Effect in an iPad Web App
How to Have a Non-Rectangular Div
Highlight Multiple Items on Hover's Condition
Detect Android Phone via JavaScript/Jquery
Angular: Why Doesn't CSS Justification Work with Ng-Repeat
Align Select-Options Text to Right
How to Detect If Element Has 'Auto' Height
Android 4.2.1, Webview and JavaScript Interface Breaks
CSS Select All Previous Siblings for a Star Rating
How to Change CSS Href="" Using JavaScript
How to Put a Eventlistener for All Divs Generated by JavaScript
How to Detect When an HTML Element's Class Changes
Determine If Point Is Within Bounding Box
How to Center the Text in the Headers for an Ag-Grid Control