brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: field-sizing: content makes textareas grow with their text

Web-Development
~/tips/tip-css-field-sizing-content

textarea {

Lets textareas and text inputs resize to fit what is typed, without JavaScript.

Auto-growing textareas used to be a JavaScript job with a resize listener. One CSS property now does it.

textarea {
  field-sizing: content;
  min-height: 3lh;
  max-height: 15lh;
}

MDN describes field-sizing as controlling the sizing behavior of elements that have a default preferred size, such as form controls. The default value is fixed. With content the control adjusts to its contents, so a textarea grows taller as more text is typed and shrinks again when text is deleted. The lh unit in the example is the line height of the element, so the field starts at three lines and stops growing at fifteen, after which it scrolls.

It also works for single-line inputs. There you set a width range instead:

input {
  field-sizing: content;
  min-width: 50px;
  max-width: 350px;
}

MDN’s advice is to set a minimum, because without a minimum width a text control is only as wide as the text cursor. Do not set a fixed width or height as well, since they bring the fixed size back. The size attribute of an input has no effect, and the rows and cols of a textarea are ignored. A maxlength stops the growth once the limit is reached.

Caveat: MDN marks it as newly available since June 2026, so older browsers ignore the property. That is a fine fallback, because the textarea keeps its normal size there and still works.

Further reading: MDN: field-sizing

More Web-Development tips ab -n 200 -c 10 https://example.com/ console.assert(items.length > 0, 'cart is empty', items) document.designMode = "on" function save(item) { Save draft All Web-Development tips (67) All topics

Link