Auto-sizing Textarea
A message box that grows with its content up to a maximum height, then scrolls, with a line counter.
AI prompt
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
<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>:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.cp{display:flex;align-items:flex-end;gap:8px;border:1px solid var(--line);border-radius:14px;padding:6px 6px 6px 12px;background:var(--surface)}.cp textarea{flex:1;min-width:0;border:0;outline:none;resize:none;background:transparent;font:inherit;font-size:13.5px;line-height:20px;padding:5px 0;max-height:var(--mh,120px);color:var(--text);overflow-y:hidden}.mini.send{width:32px;height:32px;padding:0;border-radius:50%;background:#171717;color:#fff;border-color:#171717;font-size:15px}.mini{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:12px;font-weight:600;cursor:pointer}.mini:disabled{opacity:.4;cursor:default}.out{font-size:12px;color:var(--muted);min-height:16px;margin:0}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}const ta=document.getElementById('ta'),ms=document.getElementById('ms'),out=document.getElementById('out');
function fit(){ta.style.height='auto';const lh=20,max=+ms.value*lh+10,h=Math.min(ta.scrollHeight,max);ta.style.height=h+'px';ta.style.overflowY=ta.scrollHeight>max?'auto':'hidden';ta.style.setProperty('--mh',max+'px');const lines=Math.max(1,Math.round((ta.scrollHeight-10)/lh));out.textContent=lines+' line'+(lines>1?'s':'')+(ta.scrollHeight>max?' \u2013 at the maximum, now scrolling':'');}
ta.addEventListener('input',fit);ms.addEventListener('input',()=>{document.getElementById('mv').textContent=ms.value;fit();});fit();One complete HTML file. Save it and open it in a browser.
<!doctype html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Auto-sizing Textarea — demo</title>
<style>
:root{--bg:#F1F1EF;--surface:#fff;--surface-2:#F7F7F5;--line:#E0E0DC;--text:#171717;--muted:#6f6f6f;--accent:#171717;--accent-ink:#fff;--danger:#d92d20}
*{box-sizing:border-box;margin:0}
html,body{height:100%}body{background:var(--bg);color:var(--text);font:15px/1.5 -apple-system,BlinkMacSystemFont,'Segoe UI',Inter,Roboto,sans-serif;-webkit-font-smoothing:antialiased}.stage{min-height:100%;display:grid;place-items:center;padding:24px}button,input,select,textarea{font-family:inherit}@media (prefers-reduced-motion:reduce){html{scroll-behavior:auto}}
button{font:inherit;color:inherit;background:none;border:0;padding:0;cursor:pointer}a{color:inherit;text-decoration:none}ul,ol,p,h4{margin:0;padding:0}.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
.pre{display:flex;gap:5px;flex-wrap:wrap}.pre button{height:28px;padding:0 11px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:11px;cursor:pointer}
.pre button[aria-pressed=true]{background:var(--accent);color:var(--accent-ink);border-color:var(--accent)}
.bx{width:min(330px,100%);display:grid;gap:9px}.rw{display:grid;gap:3px;font-size:11.5px;color:var(--muted)}.rw b{color:var(--text);font-weight:600}input[type=range]{width:100%;accent-color:#171717}
.out{font-size:12px;color:var(--muted);line-height:1.4}.out b{color:var(--text)}
button:focus-visible,input:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.cp{display:flex;align-items:flex-end;gap:8px;border:1px solid var(--line);border-radius:14px;padding:6px 6px 6px 12px;background:var(--surface)}.cp textarea{flex:1;min-width:0;border:0;outline:none;resize:none;background:transparent;font:inherit;font-size:13.5px;line-height:20px;padding:5px 0;max-height:var(--mh,120px);color:var(--text);overflow-y:hidden}.mini.send{width:32px;height:32px;padding:0;border-radius:50%;background:#171717;color:#fff;border-color:#171717;font-size:15px}.mini{height:30px;padding:0 14px;border-radius:99px;border:1px solid var(--line);background:var(--surface);font-size:12px;font-weight:600;cursor:pointer}.mini:disabled{opacity:.4;cursor:default}.out{font-size:12px;color:var(--muted);min-height:16px;margin:0}
@media (prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
</style></head>
<body><div class="stage">
<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>
</div>
<script>
const ta=document.getElementById('ta'),ms=document.getElementById('ms'),out=document.getElementById('out');
function fit(){ta.style.height='auto';const lh=20,max=+ms.value*lh+10,h=Math.min(ta.scrollHeight,max);ta.style.height=h+'px';ta.style.overflowY=ta.scrollHeight>max?'auto':'hidden';ta.style.setProperty('--mh',max+'px');const lines=Math.max(1,Math.round((ta.scrollHeight-10)/lh));out.textContent=lines+' line'+(lines>1?'s':'')+(ta.scrollHeight>max?' \u2013 at the maximum, now scrolling':'');}
ta.addEventListener('input',fit);ms.addEventListener('input',()=>{document.getElementById('mv').textContent=ms.value;fit();});fit();
</script>
</body></html>