7.2 · Forms & UI

Fields that grow: field-sizing

Inputs and textareas that grow with their content.

field-sizing: content sizes inputs, textareas and selects to fit what’s in them instead of to fixed rows and columns.

Set min- and max- sizes to keep it in bounds. A textarea with max-block-size: 10lh grows up to ten lines and scrolls after that.

Before
textarea.addEventListener('input', () => {
  textarea.style.height = 'auto'
  textarea.style.height =
    textarea.scrollHeight + 'px'
})
Now
textarea {
  field-sizing: content;
  min-block-size: 2lh;
  max-block-size: 10lh;
}