Back to Snippets

Auto-Growing Textarea with field-sizing CSS

A comment box that starts three lines tall and grows as you type, line by line, until it reaches a ceiling and starts to scroll. There is no script measuring scrollHeight and no hidden mirror element, only one CSS declaration.

field-sizing set to content tells the browser to size the textarea from its value instead of from the rows attribute, which is the thing every auto-grow script tries to fake. The min-height and max-height are written in lh units, so the limits are counted in lines of the current font rather than in pixels that drift when the font size changes. The resize handle is switched off because the box already resizes itself.

field-sizing shipped in Chrome and Edge in 2024 and in Firefox 152 in mid 2026, with Safari still in Technology Preview. In a browser without it the textarea behaves like a normal one at its minimum height, so nothing breaks, it just stops growing.

2
#css
#html
#textarea
#form
#input
#field-sizing
#ui