field-sizing

`<textarea>`·`<input>`이 내용 길이에 맞춰 CSS만으로 자동으로 커지고 줄어들게 하는 속성.

다른 이름: Auto-growing textareafield-sizing: content
···
html
<div class="wrap">
  <div class="badge" id="badge"><svg viewBox="0 0 10 10"><path id="bpath" d="M1.5 5.2l2.6 2.6L8.5 2.4" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"/></svg><span id="btext">확인 중</span></div>
  <textarea id="ta" class="ta" rows="1">메시지를 입력하면</textarea>
</div>
css
.wrap{position:relative;width:min(260px,92%)}
.ta{width:100%;min-height:38px;max-height:110px;padding:10px 12px;border:1px solid var(--line);border-radius:10px;
  background:var(--surface);color:var(--fg);font-size:12px;font-family:inherit;resize:none;field-sizing:content;overflow-y:auto}
.ta.fallback{field-sizing:auto}
.badge{position:absolute;top:-30px;right:0;display:flex;align-items:center;gap:5px;padding:4px 9px;border-radius:999px;font-size:10px;font-weight:700;background:var(--bg);border:1px solid var(--line);color:var(--accent-3)}
.badge.no{color:var(--accent-2)}
.badge svg{width:9px;height:9px}
js
const ok = CSS.supports('field-sizing', 'content');
const b=document.getElementById('badge'),p=document.getElementById('bpath'),t=document.getElementById('btext');
b.classList.toggle('no', !ok);
p.setAttribute('d', ok ? 'M1.5 5.2l2.6 2.6L8.5 2.4' : 'M2 2l6 6M8 2l-6 6');
t.textContent = ok ? 'field-sizing 지원됨' : '미지원 · scrollHeight 폴백';
const ta = document.getElementById('ta');
if (!ok) {
  ta.classList.add('fallback');
  const resize = () => { ta.style.height = 'auto'; ta.style.height = ta.scrollHeight + 'px'; };
  ta.addEventListener('input', resize); resize();
}
const lines = ['메시지를 입력하면', '메시지를 입력하면\n자동으로 높이가', '메시지를 입력하면\n자동으로 높이가\n늘어납니다', '메시지를 입력하면'];
let i = 0;
setInterval(() => { i = (i+1) % lines.length; ta.value = lines[i]; ta.dispatchEvent(new Event('input')); }, 1600);

채팅 입력창처럼 타이핑할수록 커지는 textarea는 오랫동안 JS 없이는 불가능했습니다. `textarea`는 `rows` 속성으로 고정 줄 수만 잡을 수 있고, 내용에 맞춰 자동으로 커지려면 매 입력마다 `scrollHeight`를 읽어서 `height`에 다시 써주는 리스너가 필요했습니다.

`field-sizing: content`를 주면 textarea·input이 `width`/`height` 같은 명시적 크기 대신 내용 크기를 따라갑니다. `min-height`/`max-height`와 함께 쓰면 "3줄까지는 자동으로 늘어나고 그 이상은 스크롤"도 CSS만으로 표현됩니다.

기본 지원은 caniuse/MDN baseline을 확인하세요. 미지원 브라우저에서는 input 이벤트마다 `el.style.height = el.scrollHeight + 'px'`로 재는 고전적인 자동 리사이즈 스크립트가 여전히 필요합니다.

언제 쓰나

채팅 입력창, 댓글창처럼 내용에 맞춰 자동으로 커지는 textarea를 JS 없이 만들 때.