javascript
16 lines · 7 steps
Auto-resizing a textarea to fit its content
A textarea grows with its text by measuring scrollHeight on every input and setting its own height.
Explained by
highlit
1function autoResizeTextarea(textarea, { maxHeight = Infinity } = {}) {
2 const resize = () => {
3 textarea.style.height = 'auto';
4 const contentHeight = textarea.scrollHeight;
5 const nextHeight = Math.min(contentHeight, maxHeight);
6 textarea.style.height = `${nextHeight}px`;
7 textarea.style.overflowY = contentHeight > maxHeight ? 'auto' : 'hidden';
8 };
9
10 textarea.style.resize = 'none';
11 textarea.style.boxSizing = 'border-box';
12 textarea.addEventListener('input', resize);
13 resize();
14
15 return () => textarea.removeEventListener('input', resize);
16}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Resetting height to 'auto' before reading scrollHeight is what lets a textarea shrink as well as grow.
- 2Clamping with Math.min and toggling overflowY gives you a scrollbar only once a max height is reached.
- 3Returning a teardown function makes the listener easy to remove and prevents leaks.
Related explainers
javascript
const ROLE_PERMISSIONS = { admin: ['users:read', 'users:write', 'billing:read', 'billing:write'], manager: ['users:read', 'billing:read'], member: ['users:read'],
Role-based permissions middleware in Express
authorization
middleware
rbac
Intermediate
9 steps
javascript
function attachThousandSeparators(input, { locale = 'en-US' } = {}) { const formatter = new Intl.NumberFormat(locale); const groupSep = formatter.format(11111).replace(/\d/g, '')[0] || ','; const decimalSep = formatter.format(1.1).replace(/\d/g, '')[0] || '.';
Live thousand separators without losing the caret
dom
intl
caret-preservation
Advanced
8 steps
javascript
import { useReducer, useEffect } from "react"; const initialState = { status: "idle", data: null, error: null };
Building a data-fetching hook in React
custom-hooks
usereducer
data-fetching
Intermediate
9 steps
javascript
const express = require('express'); const app = express(); app.get('/health', (req, res) => res.json({ status: 'ok' }));
Graceful shutdown in an Express server
graceful-shutdown
signal-handling
connection-tracking
Advanced
9 steps
javascript
import { useState, useEffect, useCallback } from 'react'; function getColumnCount(width) { if (width < 640) return 1;
A responsive column hook in React
custom-hooks
debouncing
responsive-design
Intermediate
7 steps
javascript
function initCharacterCounter(textarea, options = {}) { const maxLength = options.maxLength ?? 280; const warnThreshold = options.warnThreshold ?? 0.9;
A live character counter for textareas
dom
closures
accessibility
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/auto-resizing-a-textarea-to-fit-its-content-explained-javascript-2fa1/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.