Home >Web Front-end >CSS Tutorial >How Can I Make a Textarea Automatically Resize to its Content and Hide Scrollbars?

How Can I Make a Textarea Automatically Resize to its Content and Hide Scrollbars?

Linda Hamilton
Linda HamiltonOriginal
2024-12-13 02:48:10801browse

How Can I Make a Textarea Automatically Resize to its Content and Hide Scrollbars?

Textarea's Height Determined by Content, Scrollbars Removed

Problem:

Achieving a textarea's height that dynamically adjusts to fit its content, eliminating the need for scrollbars.

Solution Using JavaScript:

function auto_grow(element) {
  element.style.height = "5px";
  element.style.height = (element.scrollHeight) + "px";
}

CSS Styling:

textarea {
  resize: none;
  overflow: hidden;
  min-height: 50px;
  max-height: 100px;
}

Implementation:

<textarea oninput="auto_grow(this)"></textarea>

With this code, the textarea's height will expand or contract to accommodate the content, removing the need for scrollbars.

The above is the detailed content of How Can I Make a Textarea Automatically Resize to its Content and Hide Scrollbars?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn