Let Form Controls Size Themselves With field-sizing
Making a <textarea> grow with its content has traditionally meant measuring it with JavaScript. field-sizing: content gives CSS a direct way to do it.
textarea,
select,
input {
field-sizing: content;
}
The browser sizes each control from its content rather than keeping its usual fixed size. It can apply to text inputs, number inputs, file inputs, textareas, selects, and related controls, though the exact result depends on the control.
Do not forget the limits
An input that grows forever is technically responsive and practically awkward. Add minimum and maximum sizes that fit the layout:
textarea {
field-sizing: content;
min-block-size: 4lh;
max-block-size: 12lh;
max-inline-size: 100%;
}
The default value is fixed, which keeps the familiar control sizing. Because field-sizing was still a new feature when this article was written, check current browser support before relying on it without a fallback. The Chrome platform status page tracks the original implementation.
For a small progressive enhancement, this is a nice property. Keep the control usable without it, add sensible limits, and let supporting browsers remove a bit of JavaScript.
Enjoyed reading this?
If you found this article helpful, consider supporting my writing. Every little bit helps keep this blog going.
Related articles
- Better Line Breaks With CSS balance and pretty
Use text-wrap balance for short headings and text-wrap pretty selectively for longer copy without hard-coded line breaks.
- Why 100vh Doesn’t Always Fit a Mobile Screen
Choose svh, lvh, dvh, or a flexible min-height when mobile browser controls make 100vh an awkward fit.
- Making Links Easy to Recognize
Make links recognisable with underlines, clear focus states, useful text, and behaviour that does not surprise the reader.