Layout Beginner

Auto-growing textarea without JavaScript

Making a textarea grow as the user types required a JS event listener that reset height to auto then set it to scrollHeight on every keystroke. field-sizing: content makes the field size itself to its content.

Modern
4 lines
1textarea {
2  field-sizing: content;
3  min-height: 3lh;
4}
Old 7 lines
1textarea { overflow: hidden; }
2// JS: reset height then set to scrollHeight on every input
3el.addEventListener('input', () => {
4  el.style.height = 'auto';
5  el.style.height = el.scrollHeight + 'px';
6});
type to see the textarea grow
Without field-sizing
With field-sizing: content
field-sizing: content — no JS needed
Field sizing
Newly available Since 2024 73% global usage
123+
130+
18.2+
123+

Since 2024 this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Zero JavaScript

No oninput listener, no scrollHeight, no height reset trick. The browser handles the resize.

Works on inputs too

field-sizing: content works on single-line text inputs as well. The input grows with the typed value.

Min and max still work

Set min-height for the default empty size and max-height to cap growth. CSS handles the range.

Lines Saved
7 → 4
No JS resize handler
Old Approach
scrollHeight + JS
Reset and re-measure on every input
Modern Approach
field-sizing: content
Native auto-resize in CSS

How it works

Auto-growing textareas required JavaScript on every keystroke: first set height to auto so the element could shrink, then immediately set height to scrollHeight to match the content. One listener per textarea, causing a forced reflow on every keypress.

field-sizing: content tells the browser to size the field to fit its content instead of a fixed box. Use min-height to set the default empty state and max-height to cap how tall it can grow. No JavaScript, no event listeners, no scrollHeight.

New CSS drops every month.

Get one old → modern comparison in your inbox every week.

ESC