I tried to do using jQuery
.
I put the <textarea>
within a <div>
, positioning with absolute
and carrying out the calculation of the top
for jQuery
. It also makes the automatic adjustment for when to insert more words or less words.
HTML:
<div>
<textarea>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</textarea>
</div>
CSS:
div {
width: 200px;
height: 200px;
background: #fff;
position: relative;
}
textarea {
background: transparent;
border: 0;
outline: 0;
text-align: center;
width: 100%;
max-width: 100%;
position: absolute;
}
JQUERY:
$(document).ready(function(){
// função que calcula e aplica a centralização
function centralizar(){
$('textarea').css({ 'height' : '0' }); // reseta a altura para nova contagem
var textareaSHeight = $('textarea')[0].scrollHeight, // pega o valor do scroll do textarea
textareaHeight = $('textarea').height(); // pega o valor da altura do textarea
// verifica se tem scroll ou não
if (textareaHeight < textareaSHeight) {
var divHeight = $('div').height() / 2, // pega a metade altura da div
top = divHeight - (textareaSHeight / 2); // calculo final do valor top
// aplica a altura do textarea e o valor top
$('textarea').css({ 'height' : textareaSHeight+'px', 'top' : top+'px' });
}
}
centralizar(); // aplica o efeito inicial
// ajuste automático enquanto digitar
$('textarea').on('keydown', function(){
centralizar();
});
});
Demo: https://jsfiddle.net/7j99fkbs/3/
But do you need it for something or is it just a question ? It’s too ugly centered on one
textarea
uahsuhas– Diego Souza
@Gumball is doubt even, I agree rs
– Lennon S. Bueno