Skip to content

A Textarea Element That Autoresizes

Due to the fact that the HTML <textarea> element does not automatically grow to fit its content, many developer look for plugins and scripts to enable this feature. This bit of JavaScript will cause any applicable <textarea> elements to resize automatically to fit the content typed or pasted into it.

The code has the following features:

  • Uses a hidden clone element (a <div>) that has the same typographical properties as the <textarea>
  • Has no dependencies (e.g. jQuery)
  • Will autosize any <textarea> that has a class of txta
  • Works in all browsers, even old IE

Here’s the HTML:

<textarea placeholder="Type multiple lines of content here." class="txta"></textarea>Code language: HTML, XML (xml)

Here’s the CSS:

.txta {
    width: 100%;
    max-width: 500px;
    min-height: 100px;
    font-family: Arial, sans-serif;
    font-size: 16px;
    overflow: hidden;
    line-height: 1.4;
}Code language: CSS (css)

And the JavaScript (commented):

Note: To the best of our knowledge, the information above and the snippet are accurate and up to date. However, in case you notice something wrong, please report snippet or leave a comment below.
View all Snippets
Louis Lazaris
Share:

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!