CSS Reference ยท Box Model and Sizing
The field-sizing property
Reference for the CSS field-sizing property: make inputs and textareas grow to fit what the user types, with no JavaScript. Values, example and browser support.
Form controls such as <textarea> and <input> normally have a fixed size, however much text they hold. The field-sizing property changes that: with field-sizing: content, the control resizes to fit its content as the user types. Auto-growing textareas no longer need JavaScript.
textarea { field-sizing: content; min-height: 3lh; max-height: 10lh; }
Try it
In a supporting browser, adding lines to the second box makes it taller (up to six lines), and typing in the input widens it. Elsewhere, both controls stay at their default size.
Syntax
field-sizing: fixed | content
- Initial value:
fixed - Applies to: elements with default preferred size, such as form controls
- Inherited: no
- Animatable: no (discrete)
- Computed value: as specified
Values
| Value | Description |
|---|---|
fixed |
The control uses its usual default size, based on attributes like size, cols and rows. |
content |
The control's size follows its content (or its placeholder when empty). The size, cols and rows attributes are ignored. |
Usage notes
- Set limits. An unbounded control can get huge. Use
min-width,max-width,min-heightandmax-height(thelhunit is handy for line counts). - Select elements. A
<select>sized by content matches the width of its currently selected option, so it may change width when the choice changes. - Works without scripts, so it is a good progressive enhancement: unsupported browsers simply keep the fixed size.
- Not just text. It also applies to number, date and other input types, where the size follows the displayed value.
- Block layout. Inputs are inline-block by default; in flex or grid containers they may stretch, so set
align-selforwidth: autoif the growth is not visible.
Browser support
Limited: it is supported in Chromium-based browsers, but not across all major browsers, so it is not yet part of Baseline. Use it as an enhancement and check current support tables.
Related
resizewidthmin-height- Lesson: Forms