webdevpuneet.com

Auto-Growing Textarea with CSS field-sizing: content

Auto-Growing Textarea with CSS field-sizing: content · Forms · Plain HTML, CSS & JS · Live preview

CategoryForms

About this UI Snippet

A Textarea That Resizes Itself With One Line of CSS

Screenshot of the Auto-Growing Textarea with CSS field-sizing: content snippet rendered live

Every chat box, comment form and AI prompt field wants the same thing: start as one line, grow as the text grows, and stop before it takes over the screen. For a decade that needed JavaScript that reset the height and copied scrollHeight. The field-sizing: content property makes the browser do it, and this snippet wraps it in a polished message composer.

The one property

field-sizing: content tells a form control to size itself to its content instead of a default size. Combined with min-height: 1lh and max-height: 9lh, the box is one line tall when empty, grows to nine lines, then scrolls inside itself. The lh unit is one line height, so the limits stay correct if you change the font size.

Layout of the composer

The textarea sits in a flex row with a round send button aligned to the bottom, so the button stays put while the box grows upward. :focus-within on the wrapper draws the focus ring around the whole pill rather than the bare field.

Keyboard behaviour

Enter sends and Shift+Enter inserts a new line, handled in a short keydown listener that calls requestSubmit(). Sent messages are added as bubbles with white-space: pre-wrap so line breaks survive. A character counter updates on every input.

Fallback

The script tests CSS.supports('field-sizing', 'content'). In Firefox, which lacks it, the same cap is applied by setting height from scrollHeight, so the behaviour matches everywhere.

Using it

Keep the label, since a placeholder is not a label. For an AI-style input with a token counter see the AI prompt composer.

Accessibility

The field keeps a real label, the send button has an aria-label, the focus ring is visible on the whole composer, and the message list is an aria-live region so sent messages are announced. The max height keeps the page usable on small screens because the box scrolls instead of pushing the layout down.

What's included

Features

One-property resizing
field-sizing: content replaces the scrollHeight measuring code.
Line-based limits
min-height 1lh and max-height 9lh scale with the font size.
Bottom-aligned send button
The button stays at the bottom edge while the box grows upward.
Focus ring on the pill
:focus-within outlines the whole composer, not just the field.
Enter and Shift+Enter
Enter sends, Shift+Enter adds a line, like common chat apps.
Live character count
A counter updates as you type or paste.
Line breaks preserved
Sent messages use pre-wrap so multi-line text keeps its shape.
JavaScript fallback
Browsers without field-sizing get the same capped growth.

Step by step

How to Use

  1. 1
    Start with rows="1"The textarea begins one line tall. field-sizing: content then makes the browser add height as lines are typed or pasted.
  2. 2
    Set the limits in line heightsUse min-height: 1lh and max-height: 9lh so the box stops growing after nine lines and scrolls inside, whatever the font size.
  3. 3
    Align the send button to the bottomThe wrapper is a flex row with align-items: flex-end, so the button stays at the bottom edge while the box grows upward.
  4. 4
    Make Enter sendThe keydown handler calls requestSubmit() on Enter and leaves Shift+Enter alone for a new line.
  5. 5
    Keep the pre-wrap on sent bubbleswhite-space: pre-wrap on each message preserves the line breaks the writer typed.
  6. 6
    Leave the Firefox fallback inWhere CSS.supports("field-sizing", "content") is false, the script copies scrollHeight (capped at nine lines) to the height on every input.

Real-world uses

Common Use Cases

Chat and messaging inputs
Give a support widget or team chat a message box that starts compact and expands for long replies without a visible resize handle.
AI prompt fields
Let people paste a long prompt into a single box that grows to a sensible height, matching how current assistant interfaces behave.
Comment and review forms
Show a short field first so the form looks inviting, and let it grow once the reader starts writing.
Dropping the autosize helper
Delete the autosize package or the scrollHeight function copied between projects; one declaration covers the common case and the script remains only as a Firefox safety net.

Got questions?

Frequently Asked Questions

Chrome and Edge from version 123 and Safari from 26.2. Firefox does not support it yet. The snippet detects this with CSS.supports and uses a small scrollHeight fallback, so the behaviour is the same everywhere.

Set max-height on the textarea, as the snippet does with 9lh. Once the content passes that height the field stops growing and scrolls internally. Use a pixel value if you prefer, but lh keeps it tied to the font size.

Yes. field-sizing: content also sizes single-line inputs to their text and selects to the chosen option. For a textarea it is most useful, because the height follows the number of lines you have written.

A placeholder disappears when typing and is not a reliable accessible name. The snippet keeps a real label linked with for and id, so screen readers and voice control can find the field.

Copy & paste

Source Code

<main class="fs-page">
  <h1>Message box that grows as you type</h1>
  <p class="fs-sub">One CSS property replaces the scrollHeight hack. Type or paste several lines and watch it expand, then stop at a maximum height.</p>

  <form class="fs-composer" id="fsForm">
    <label class="fs-label" for="fsText">Message</label>
    <div class="fs-box">
      <textarea id="fsText" class="fs-text" rows="1" placeholder="Ask anything, paste notes, or write a long reply&hellip;"></textarea>
      <button class="fs-send" type="submit" aria-label="Send message">&#10148;</button>
    </div>
    <div class="fs-meta"><span id="fsCount">0 characters</span><span id="fsMode"></span></div>
  </form>

  <ul class="fs-log" id="fsLog" aria-live="polite"></ul>
</main>

Extend with AI

Take This Snippet Further With AI

Paste this snippet into an AI coding assistant and ask it to explain how field-sizing: content and the lh unit control the minimum and maximum height. Useful follow-ups: add an attach-file button, show a streaming reply bubble after sending, or add a slash-command menu. Ask it to keep the label, Enter and Shift+Enter handling, and the Firefox fallback.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build an auto-growing message composer in plain HTML, CSS and JavaScript using the CSS field-sizing property.

Requirements:
- A form with a visible label, a textarea (rows="1") and a round send button in a flex row aligned to the bottom, inside a rounded pill that shows a focus ring with :focus-within.
- Textarea CSS: field-sizing: content; min-height: 1lh; max-height: 9lh; overflow-y: auto; resize: none; no border or outline.
- A character counter under the box and a small label showing whether the native property or the fallback is active.
- Script: feature-detect with CSS.supports('field-sizing', 'content'); if unsupported, set height to min(scrollHeight, 9 line heights) on input. Enter submits via requestSubmit, Shift+Enter inserts a newline.
- On submit, trim the text, ignore empty input, append it to a list as a right-aligned bubble that preserves line breaks (white-space: pre-wrap), clear the field and reset the counter and height.
- Announce new messages with aria-live and work at phone width.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.