textarea
Multi-line text input for longer content like messages, comments, or descriptions. Unlike input type='text', textarea allows line breaks and resizing.
When to Use
- Comments and messages
- Descriptions and bios
- Address fields (multi-line)
- Code or text snippets
- Any free-form text longer than a single line
Basic Usage
A simple textarea with label and placeholder.
Code
Attributes
| Attribute | Purpose | Example |
|---|---|---|
rows |
Initial visible lines | rows="5" |
cols |
Visible character width | cols="40" |
minlength |
Minimum character count | minlength="10" |
maxlength |
Maximum character count | maxlength="500" |
required |
Field must have a value | required |
disabled |
Prevents editing | disabled |
readonly |
Can select but not edit | readonly |
placeholder |
Hint text when empty | placeholder="..." |
wrap |
Text wrapping behavior | wrap="soft" |
spellcheck |
Enable/disable spell check | spellcheck="true" |
Setting Size
Using rows Attribute
Control the initial height with rows.
Code
Character Limits
Use minlength and maxlength for validation.
Code
Textarea States
Disabled
Cannot be edited or focused.
Read-only
Can be selected and copied but not edited.
Invalid
Shows validation error state.
Code
With form-field
Use the form-field custom element for CSS-only validation.
Code
Real-World Examples
Contact Form Message
Code/Snippet Input
Address Input
Comment with Character Counter
Resizing Behavior
By default, textareas can be resized vertically. CSS controls this behavior.
| CSS Property | Behavior |
|---|---|
resize: vertical |
Can resize height only (default) |
resize: horizontal |
Can resize width only |
resize: both |
Can resize in both directions |
resize: none |
Cannot be resized |
Character / Word Count (data-count)
Add data-count to any textarea to show a live character or word count. Works with native maxlength for character mode and data-maxwords for word mode. Color shifts at 80% (warning) and 100% (error).
Character Count
The default mode counts characters. Pair with maxlength to show “42 / 500” format.
Word Count
Set data-count="words" and data-maxwords for word-based counting.
| Attribute | Values | Description |
|---|---|---|
data-count |
"", "words" |
Enable counter. Empty for characters, "words" for word mode. |
data-maxwords |
number | Max word count (word mode only). |
Auto-Expand (data-grow)
Add data-grow to a textarea to make it automatically expand as the user types. Uses CSS field-sizing: content where supported, with a JS scrollHeight fallback. Set data-max-rows to cap the height.
Max Rows
Set data-max-rows to limit growth. Once the limit is reached, the textarea scrolls normally.
Combined with Count
data-grow and data-count work together seamlessly.
| Attribute | Values | Description |
|---|---|---|
data-grow |
— | Enable auto-expanding behavior. |
data-max-rows |
number | Maximum rows before scrolling. |
Accessibility Notes
- Always use a label: Every textarea needs an associated label
- Don't disable resize: Allow users to resize for their needs when possible
- Provide character limits: If using maxlength, show remaining characters
- Use aria-describedby: Link to help text or validation messages
- Placeholder is not a label: Placeholders disappear when typing
- Spell checking: Keep spellcheck enabled for prose, disable for code
CSS Reference
Related Elements
- input - Single-line text input
- label - Required for accessibility
- form - Container for textareas
- form-field - Enhanced textarea with validation