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
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
javascript
import { useState, useEffect, useCallback, useRef } from 'react'; const cache = new Map(); const inflight = new Map();
Building a stale-while-revalidate hook in React
caching
request-deduplication
custom-hooks
Advanced
10 steps
javascript
import { useEffect, useRef, useState } from 'react'; export function useDelayedFlag(active, delay = 300) { const [visible, setVisible] = useState(false);
Delaying a loading spinner with a React hook
custom-hooks
debouncing
cleanup
Intermediate
8 steps
javascript
const SWIPE_THRESHOLD = 80; const MAX_TRANSLATE = 120; export function attachSwipeToDismiss(element, onDismiss) {
Building a swipe-to-dismiss gesture in JS
touch-events
gesture-detection
dom-manipulation
Intermediate
10 steps
python
from typing import Any, Sequence, Mapping def render_markdown_table(
Rendering an aligned Markdown table in Python
string formatting
data transformation
closures
Intermediate
8 steps
javascript
const { pool } = require('./db'); function withTransaction() { return async (req, res, next) => {
A per-request transaction middleware in Express
middleware
database-transactions
connection-pooling
Advanced
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.