How to Make a Textarea Auto-Resize with JavaScript and CSS

Two ways to make a textarea grow with its content: the CSS field-sizing property and a JavaScript fallback, with the border and max-height details.

Mahzaib MirzaMahzaib MirzaNovember 16, 20233 min read0 comments
How to Make a Textarea Auto-Resize with JavaScript and CSS

The simplest way to make a textarea grow as you type is one line of CSS: field-sizing: content. It needs no JavaScript. For browsers that do not support it, a short input event handler that sets the height from scrollHeight does the same job.

textarea {
  field-sizing: content;
}

Option 1: CSS field-sizing

field-sizing: content lets a form control size itself to its contents. For a textarea, MDN says it grows in height to show more rows, and shows a scrollbar once it reaches a height limit. MDN marks the property as Baseline 2026, newly available since June 2026, so check your audience's browsers before relying on it alone.

textarea {
  field-sizing: content;
  width: 100%;
  min-height: 4rem;
  max-height: 20rem;
}
  • The rows and cols attributes have no effect once field-sizing: content is set.

  • Avoid a fixed width or height, which put back a fixed size. MDN recommends min-width, max-width, min-height and max-height instead.

In a quick test in a Chromium-based browser, a textarea with this property grew from 64px to 144px when its content went from 2 lines to 6.

Option 2: JavaScript

This works in every browser. Set the CSS first:

textarea {
  box-sizing: border-box;
  overflow: hidden;
  resize: none;
}

box-sizing: border-box makes the height calculation predictable, overflow: hidden stops a scrollbar flickering in while the height catches up, and resize: none removes the manual resize handle. Then add the script:

<textarea id="notes" rows="2"></textarea>
function autoResize(el) {
  el.style.height = 'auto';
  el.style.height = el.scrollHeight + el.offsetHeight - el.clientHeight + 'px';
}

const notes = document.querySelector('#notes');

notes.addEventListener('input', () => autoResize(notes));
autoResize(notes); // size it for any text that is already inside

How the JavaScript works

  • Reset to auto first. Setting height = 'auto' lets the textarea shrink back when text is deleted. Without it, scrollHeight never reports less than the current height.

  • Read scrollHeight. It is the height of the content, including padding but not the border.

  • Add the border. offsetHeight - clientHeight is the border thickness, because offsetHeight includes borders and clientHeight does not. With box-sizing: border-box, the height you set must include the border, otherwise the last line is clipped.

The border step is the one most tutorials leave out. In a test with a 4px border, setting the height to scrollHeight alone left the box 8px too short, so the content still overflowed. Adding the border made it fit exactly, and the textarea also shrank correctly when I removed text.

Set a maximum height

To stop the box growing forever, cap it in CSS and let it scroll after that:

textarea {
  max-height: 300px;
  overflow-y: auto;
}

The script still sets the height from the content, and max-height limits it. In a test, a textarea with 20 lines stopped at the cap and scrolled.

Use it on every textarea

Mark the textareas you want to resize with a data attribute and use one event listener for all of them:

function autoResize(el) {
  el.style.height = 'auto';
  el.style.height = el.scrollHeight + el.offsetHeight - el.clientHeight + 'px';
}

document.querySelectorAll('textarea[data-autoresize]').forEach(autoResize);

document.addEventListener('input', (event) => {
  if (event.target.matches('textarea[data-autoresize]')) {
    autoResize(event.target);
  }
});

Use field-sizing when available, JavaScript when not

if (!CSS.supports('field-sizing', 'content')) {
  document.querySelectorAll('textarea[data-autoresize]').forEach(autoResize);

  document.addEventListener('input', (event) => {
    if (event.target.matches('textarea[data-autoresize]')) {
      autoResize(event.target);
    }
  });
}

This keeps the JavaScript out of browsers that do not need it. Put field-sizing: content in your CSS for all of them, and browsers that do not understand it will ignore it.

React example

In React, resize after each render when the value changes:

import { useLayoutEffect, useRef } from 'react';

export function AutoTextarea({ value, onChange, ...props }) {
  const ref = useRef(null);

  useLayoutEffect(() => {
    const el = ref.current;
    el.style.height = 'auto';
    el.style.height = el.scrollHeight + el.offsetHeight - el.clientHeight + 'px';
  }, [value]);

  return <textarea ref={ref} value={value} onChange={onChange} {...props} />;
}

Things to watch for

  • Hidden textareas. An element that is not displayed has a scrollHeight of 0. Call autoResize again when you show it, for example when a tab or modal opens.

  • Width changes. If the textarea's width can change, text wraps differently. Call autoResize again on window resize.

  • Padding and border. Set box-sizing: border-box. Tailwind's base styles already do this for every element.

Frequently asked questions

Can I auto-resize a textarea with CSS only?

Yes, with field-sizing: content, in browsers that support it. For other browsers you need the JavaScript fallback.

Why does my textarea not shrink when I delete text?

Because the height was not reset first. Set el.style.height = 'auto' before reading scrollHeight.

Can users still resize it by hand?

Only if you leave out resize: none. For the JavaScript version it is usually better to remove the handle, because the script overwrites a manual height on the next keystroke.

Share:
Mahzaib Mirza

Written by

Mahzaib Mirza

Software developer & Founder of Coders Vibe.

Related Posts

Liked this post?

Get the next one in your inbox the moment it's published. No spam, unsubscribe anytime.

0 Comments

Leave a comment