Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textarea auto-height-increase

I have html <textarea> and css:

textarea {
  width: 100%;
  max-height: 80px;
  resize: none;
}

If there is a lot of text, I want the <textarea> to increase it's height till 80px and then show a scrollbar. The problem is that <textarea> is 25px(I don't know why, may be my browser set this property) and when there is a lot of text, it shows a scrollbar after 25px. Is there anyway to show a scrollbar only after 80px?

like image 961
Genjik Avatar asked Sep 21 '25 08:09

Genjik


1 Answers

You really need js to do this, see example below:

var textarea = document.getElementById("textarea");
var limit = 80; //height limit

textarea.oninput = function() {
  textarea.style.height = "";
  textarea.style.height = Math.min(textarea.scrollHeight, limit) + "px";
};
textarea {
    width: 100%;
}
<textarea id="textarea"></textarea>
like image 142
Dalin Huang Avatar answered Sep 22 '25 23:09

Dalin Huang