HTML Reference ยท Forms
The <textarea> element
Reference for the HTML <textarea> element: multi-line text input, rows and cols, wrap, maxlength, placeholder, resizing, accessibility notes and technical details.
The <textarea> element is a multi-line text field for longer text: comments, messages, addresses, descriptions. Unlike <input type="text">, it keeps line breaks. Its initial value is the text between the start and end tags, not a value attribute.
<label for="message">Your message</label> <textarea id="message" name="message" rows="5" cols="40"></textarea>
Try it
Type a few lines and post the review: the line breaks are kept in the submitted value. resize: vertical lets users make the box taller but not wider.
Attributes
| Attribute | Values | Description |
|---|---|---|
name |
Text | Name submitted with the text. |
rows |
Positive integer | Visible height in lines. Default 2. |
cols |
Positive integer | Visible width in average characters. Default 20. |
wrap |
soft (default), hard |
With soft, line breaks added by visual wrapping are not submitted. With hard, the browser inserts real line breaks at the wrap points when submitting; cols is then required. |
maxlength |
Non-negative integer | Maximum length of the value in UTF-16 code units. |
minlength |
Non-negative integer | Minimum length, checked once the user has edited the text. |
placeholder |
Text | A hint shown while the field is empty. Line breaks in it are displayed. |
required |
Boolean | The form can't be submitted while the field is empty. |
readonly |
Boolean | The text can't be edited but is still focusable and submitted. |
disabled |
Boolean | The field can't be used and is not submitted. |
autocomplete |
on, off or an autofill token such as street-address |
Helps the browser autofill the field. |
dirname |
Text | Also submits the text direction (ltr or rtl) under this name. |
form |
ID of a <form> |
Associates the textarea with a form elsewhere in the document. |
It also accepts the global attributes, such as autofocus, spellcheck and autocapitalize.
Usage notes
- Content is the value. Write
<textarea>Initial text</textarea>, notvalue="โฆ". Whitespace inside is kept, so don't indent the closing tag on a new line unless you want the extra spaces. A single newline directly after the start tag is ignored. - No markup inside. The content is raw text: tags written inside it show up literally as text. Escape
&and<as usual if you prefill it. - Size with CSS.
rowsandcolsgive a sensible default, but CSSwidth,heightandfield-sizing: content(newer, auto-grows with its content) give more control. Useresizeto allow or restrict the user's resize handle. - Label it. A
<label>is required, just like any other control; a placeholder disappears once the user types. - Show limits. If you set
maxlength, show how many characters remain so users aren't surprised when typing stops. - One line? Use an input. For short single-line answers,
<input type="text">is the better fit.
Technical summary
| Content categories | Flow content, phrasing content, interactive content, palpable content; form-associated: listed, labelable, submittable, resettable |
| Permitted content | Text |
| Tag omission | None; both the start and end tags are required |
| Permitted parents | Any element that accepts phrasing content |
| Implicit ARIA role | textbox (multi-line) |
| DOM interface | HTMLTextAreaElement |
| Default display | inline-block |
Browser support
Supported in every browser. field-sizing: content for auto-growing text areas is newer and not yet available in every browser.
Related
<input>โ single-line fields<label>โ naming the field<form>โ sending the text- Lesson: Text Areas and Buttons
- Lesson: Form Validation