Skip to content
IndexRequest a UIAboutContact

Auto-sizing Textarea

A message box that grows with its content up to a maximum height, then scrolls, with a line counter.

Open alone ↗

AI prompt

PROMPT: PASTE INTO YOUR AI CODING TOOL
Clean UI mockup of a chat composer in two states side by side: a single-line message box with a send button, and the same box grown to four lines of text with the send button still aligned to the bottom right, a thin scrollbar visible in a third state at the maximum height with a small line counter 6 lines, light theme, rounded field, flat modern style. Negative: heavy shadows, clutter, text errors, device frames. Reduced motion: static design.

About this pattern

Definition

A fixed textarea hides what you wrote or wastes space. An auto-sizing one starts at one line, grows as you type up to a set maximum and then scrolls inside. It is the standard behaviour for chat boxes and comment fields. CSS field-sizing: content does this in newer browsers; the fallback measures scrollHeight.

Purpose & UX principles

  • Set rows to 1 and reset height to auto before reading scrollHeight, then set height to the result.
  • Cap with max-height and switch overflow to auto at the cap.
  • Recalculate on input and after programmatic changes such as clear.
  • Keep the send button aligned to the bottom so it does not jump.

Interaction states

  • Type or paste several lines; change the maximum line count.

Motion specification

  • Height changes without animation, or with a short 100ms transition.
  • Reduced motion: no transition.

Responsive behaviour

  • Keep the textarea a real labelled field and keep the Enter and Shift+Enter behaviour obvious.
  • Do not let the box grow past the screen on small devices.

Code

HTML
<div class="bx"><div class="cp"><textarea id="ta" rows="1" aria-label="Message" placeholder="Type a message. Shift+Enter adds a new line."></textarea><button class="mini send" type="button" aria-label="Send">↑</button></div><label class="rw">Maximum lines <b id="mv">5</b><input id="ms" type="range" min="2" max="8" value="5"></label><p class="out" id="out"></p></div>

Related patterns

AI Prompt Composer

↗
AI Prompt ComposerAI & Conversational

Character Counter & Limit

↗
Character Counter & LimitInputs & Forms

Text Input

↗
Text InputInputs & Forms