# A different approach to elastic textareas

DevFeed: [A different approach to elastic textareas](<https://devfeed.tech/articles/a-different-approach-to-elastic-textareas-51963.md>)

Original publisher: [Read original article](<https://lea.verou.me/2009/11/a-different-approach-to-elastic-textareas/>)

Author: Lea Verou

Published: 2009-11-13T00:00:00Z

Content type: article

Language: en

Sources: [Lea Verou's blog](<https://devfeed.tech/sources/lea-verou-s-blog.md>)

Topics: [CSS](<https://devfeed.tech/topics/css.md>), [User Interfaces](<https://devfeed.tech/topics/user-interfaces.md>)

Tags: [approach](<https://devfeed.tech/tags/approach.md>), [blog](<https://devfeed.tech/tags/blog.md>), [browser](<https://devfeed.tech/tags/browser.md>), [browsers](<https://devfeed.tech/tags/browsers.md>), [chrome](<https://devfeed.tech/tags/chrome.md>), [css](<https://devfeed.tech/tags/css.md>), [css-properties](<https://devfeed.tech/tags/css-properties.md>), [elastic](<https://devfeed.tech/tags/elastic.md>), [end-user](<https://devfeed.tech/tags/end-user.md>), [facebook](<https://devfeed.tech/tags/facebook.md>), [firefox](<https://devfeed.tech/tags/firefox.md>), [hidden](<https://devfeed.tech/tags/hidden.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [internet-explorer](<https://devfeed.tech/tags/internet-explorer.md>), [js](<https://devfeed.tech/tags/js.md>), [opera](<https://devfeed.tech/tags/opera.md>), [original](<https://devfeed.tech/tags/original.md>), [padding](<https://devfeed.tech/tags/padding.md>), [safari](<https://devfeed.tech/tags/safari.md>), [script](<https://devfeed.tech/tags/script.md>), [scripts](<https://devfeed.tech/tags/scripts.md>), [scrollable](<https://devfeed.tech/tags/scrollable.md>), [styling](<https://devfeed.tech/tags/styling.md>), [usability](<https://devfeed.tech/tags/usability.md>), [ux](<https://devfeed.tech/tags/ux.md>)

## AI overview

The excerpt proposes an experimental elastic-textarea technique that tests whether the textarea scrolls, then adjusts its rows until the content fits. Unlike the hidden-div approach described, which copies selected CSS properties and measures a replica, this method is intended to work with any CSS styles and needs no library; the script is reported as 800 bytes minified. The author says it works flawlessly in Firefox and with slight resizing twitches in Safari, Chrome, and Opera. Internet Explorer repaints repeatedly and flickers while typing, so the author considers the approach experimental. The excerpt also argues that content-driven height adjustment should ideally be handled by CSS.

## Source excerpt

I loved elastic textareas since the very first moment I used one (at facebook obviously). They let you save screen real estate while at the same time they are more comfortable for the end user. It's one of the rare occasions when you can have your UI cake and eat it too! However, I never liked the implementation of the feature. In case you never wondered how it's done, let me explain it in a nutshell: All elastic textarea scripts (or at least all that I know of) create a hidden (actually, absolutely positioned and placed out of the browser window) div, copy some CSS properties from the textarea to it (usually padding, font-size, line-height, font-family, width and font-weight) and whenever the contents of the textarea change they copy them to the hidden div and measure it's dimensions. It might be good enough for facebook, where the styling of those textareas is fairly simple and consistent throughout the site, or any other particular site, but as a generic solution? I never liked the idea. So, I tried to explore a different approach. As Andrea Giammarchi recently wrote "This is almost intrinsic, as developers, in our DNA: we spot some interesting concept? We rewrite it from scratch pretending we are doing it better!" and I'm no exception (although in this case I don't think I did it better, I just think it has potential). The basic idea is quite naive, but it works quite well in most browsers (Internet Explorer being the black sheep as usual): Test if the textarea is scrollable, and if so, increase it's rows attribute and try again. If it's not scrollable initially, try decreasing it's rows attribute until it becomes scrollable (and then ++ it). It works flawlessly on Firefox and quite well on Safari, Chrome and Opera (it just slightly twitches when it enlarges in those). Stupid Internet Explorer though repaints too many times, causing a flicker at the bottom when the user is typing, something really disturbing, so I can't consider the script anything above experim